要不你看看这个???
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2289566
TClient/ServerSocket下的通讯程序,客户端无故自动关闭(简直就是一个自杀文件!)
还有这些个
可以考虑用TCP自己的保活定时器,他的作用是每隔一段时间检查一下连接是否正常,如果不正常则报告给应用层。
用下面的方法试试,理论上应该是对的,偶没有实际测试先
uses WinSock;
在TCPServer的OnConnection事件里加上如下的片段:
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
type
TCP_KeepAlive = record
OnOff: Cardinal;
KeepAliveTime: Cardinal;
KeepAliveInterval: Cardinal
end;
var
Val: TCP_KeepAlive;
Arg: Integer;
begin
Val.OnOff:=1;
Val.KeepAliveTime:=xxx;
Val.KeepAliveInterval:=xxx;
Arg:=Integer(@Val);
ioctlsocket(AThread.Connection.Socket.Binding.Handle,
$80000000 or $18000000 or 4, Arg);
end;
TCP_KeepAlive里的参数的意义是显而易见的,就不多说了。
然后连接将自动按指定间隔测试是否正常,作为应用程序可以在TCPServer的OnException里守着就行了。
来自:LeeChange, 时间:2004-6-9 11:00:30, ID:2653438
心里没底,怕WinSock 1不支持,你最好还是用类似下面的片段吧。
uses WinSock2;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
type
TCP_KeepAlive = record
OnOff: Cardinal;
KeepAliveTime: Cardinal;
KeepAliveInterval: Cardinal
end;
var
Val: TCP_KeepAlive;
Ret: DWord;
begin
Val.OnOff:=1;
Val.KeepAliveTime:=xxx;
Val.KeepAliveInterval:=xxx;
WSAIoctl(AThread.Connection.Socket.Binding.Handle, IOC_IN or IOC_VENDOR or 4, @Val, SizeOf(Val), nil, 0, @Ret, nil, nil)
end;
sorry,我错了,刚才测试第二种方法没有给参数赋值,现在赋值正确了,6000=6秒,拔掉网线6秒立即报告断开。成了了。