type
TForm8 = class(TForm)
idtcpclnt1: TIdTCPClient;
idtcpsrvr1: TIdTCPServer;
procedure FormCreate(Sender: TObject);
procedure idtcpsrvr1Execute(AContext: TIdContext);
procedure idtcpclnt1Disconnected(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form8: TForm8;
implementation
{$R *.dfm}
procedure TForm8.FormCreate(Sender: TObject);
begin
idtcpclnt1.Connect;
end;
procedure TForm8.idtcpsrvr1Execute(AContext: TIdContext);
begin
AContext.Connection.Disconnect(true); //this gets called
end;
procedure TForm8.idtcpclnt1Disconnected(Sender: TObject);
begin
ShowMessage('true'); //but this does not
end;
OnDC永远不会得到处理。为什么?
答案 0 :(得分:11)
Indy客户端组件不是事件驱动的(有一些例外,例如TIdTelnet
)。当服务器在其结束时断开时,不会触发TIdTCPClient.OnDisconnect
事件,就像您假设的那样。这是设计的。 TIdTCPClient
在尝试再次访问套接字之前不会知道断开连接,此时它会引发异常,例如EIdConnClosedGracefully
。只有在客户端调用TIdTCPClient.OnDisconnect
方法时才会触发TIdTCPClient.Disconnect()
事件,而您没有这样做。
为了检测与TIdTCPClient
的服务器端断开连接,您必须定期读取套接字,例如在计时器或单独的线程中。
答案 1 :(得分:1)
好的,我的错误。您的代码不起作用,但这是正确的。让我解释一下原因:
AContext.Connection.Disconnect(true)
方法调用DisconnectNotifyPeer
,但未在TCPServer中实现。为什么?因为它不应该。
当您在服务器中断开连接时,indy所做的是使套接字无效并关闭它。客户端只会在尝试发送某些请求时才会注意到服务器已断开连接。你的代码不会这样做。这是indy的默认行为。
为了通知客户端服务器已断开连接,indy和任何其他套件应实现我们称之为heartbeat
的功能。 Heartbeat是一种技术,它不时尝试将小数据包发送到套接字以检测它是否仍然存活。检测套接字断开的唯一方法是尝试在该套接字中写入内容。谷歌关于heartbeat
你会理解我的意思。
修改强>
检查this。
答案 2 :(得分:1)
您可以通过向客户端添加计时器例程来使客户端 POLL断开连接 - 这是SIMPLEST方式。
procedure TForm1.Timer1Timer(Sender: TObject);
begin
idTCPClient1.Connected; // Works in Indy for Delphi XE4
// Be aware this is a property read with side effects
// It shouldn't get optimized out, but if it does,
// then add the appropriate directives to prevent that.
end;
这应该使代码的行为与旧的TClientSocket一样(和TidTelnet一样)。如果服务器突然消失(即一旦发射计时器例程检测到这一点),它就会向OnStatus事件产生一个hsDisconnected标志。但是,这种导致断开连接的服务器丢失的特殊情况不会触发OnDisconnect事件 - 只是OnStatus。因此,最好始终使用OnStatus来捕获所有断开连接,无论是客户端还是服务器引发的。我使用了一个设置为100毫秒的计时器,但我想你可以根据需要频繁或缓慢地进行 - 它似乎没有任何伤害。
注意:对于DELPHI 7(以及可能在D7和XE4之间的其他版本),您必须略有不同:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
// This no longer works this way in Indy for XE4, but works in Indy for D7 ...
idTCPClient1.CheckForGracefulDisconnect(FALSE);
end;
顺便说一下 - 如果你使用的是Delphi 6,那就忘了Indy吧,那时候它太多了。