Client端突然的断开,如断电,TServerClientThread确是无法得知,而我们一般将Server的TimeOut
没有去设它,所以在Server的ClientThread中一直阻塞的线程,占用资源。我们能做的事,就是加一个
TTime时间控件,并且在Server端口的TimeOut中加入一个确定的时间,当客户端在这个时间内没有响应的话
就把它给Kill.
//当时间到了,就调用RefreshClient来刷新客户端长时间(TimeOut要设置)的连接。
procedure TSocketForm.RefreshClient;
var
I: Integer;
Thread: TServerClientThread;
begin
for I := 0 to ConnectionList.Count - 1do
begin
Thread := ConnectionList.Items.Objects;
if Assigned(Thread) then
PostThreadMessage(Thread.ThreadID, WM_USER, 0, 0);
//唤醒MsgWaitForMultipleObjects
end;
end;
//在这增加两行代码,来响应SockForm的PostThreadMessage
procedure TSocketDispatcherThread.ClientExecute;
.....
begin
..................
WAIT_OBJECT_0 + 1:
begin
//加入以下代码,确定是否连接超时。
if (FTimeout > 0) and ((Now - FLastActivity) > FTimeout) then
FTransport.Connected := False;
//断开连接
//
while PeekMessage(msg, 0, 0, 0, PM_REMOVE)do
DispatchMessage(msg);
end;
WAIT_TIMEOUT:
....
...
end;
一点小见,说错莫怪。
还有我写了个TSocketConnectionEx的组件,可以直接进行下载和上传文件,希望大家帮忙测试一下,谢谢。
http://www.csdn.net/filebbs/read_topic.asp?id=992