Delphi Indy TCPClient的OnDisconnect事件为何总是无法正常触发?

2026-04-10 19:242阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计215个文字,预计阅读时间需要1分钟。

Delphi Indy TCPClient的OnDisconnect事件为何总是无法正常触发?

plaintexttype TForm8=class(TForm) idtcpclnt1: TIdTCPClient; idtcpsrvr1: TIdTCPServer; procedure FormCreate(Sender: TObject); procedure idtcpsrvr1Execute(AContext: TIdContext); procedure idtcpclnt1Disconnected(Sender: TObject);private { Private declarations }end;

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永远不会得到处理.为什么?

Indy客户端组件不是事件驱动的(有一些例外,例如TIdTelnet).当服务器在其结束时断开时,不会触发TIdTCPClient.OnDisconnect事件,就像您假设的那样.这是设计的. TIdTCPClient在尝试再次访问套接字之前不会知道断开连接,此时它会引发异常,例如EIdConnClosedGracefully.只有在客户端调用TIdTCPClient.Disconnect()方法时才会触发TIdTCPClient.OnDisconnect事件,而您没有这样做.

为了检测与TIdTCPClient的服务器端断开连接,您必须定期读取套接字,例如在计时器或单独的线程中.

Delphi Indy TCPClient的OnDisconnect事件为何总是无法正常触发?

本文共计215个文字,预计阅读时间需要1分钟。

Delphi Indy TCPClient的OnDisconnect事件为何总是无法正常触发?

plaintexttype TForm8=class(TForm) idtcpclnt1: TIdTCPClient; idtcpsrvr1: TIdTCPServer; procedure FormCreate(Sender: TObject); procedure idtcpsrvr1Execute(AContext: TIdContext); procedure idtcpclnt1Disconnected(Sender: TObject);private { Private declarations }end;

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永远不会得到处理.为什么?

Indy客户端组件不是事件驱动的(有一些例外,例如TIdTelnet).当服务器在其结束时断开时,不会触发TIdTCPClient.OnDisconnect事件,就像您假设的那样.这是设计的. TIdTCPClient在尝试再次访问套接字之前不会知道断开连接,此时它会引发异常,例如EIdConnClosedGracefully.只有在客户端调用TIdTCPClient.Disconnect()方法时才会触发TIdTCPClient.OnDisconnect事件,而您没有这样做.

为了检测与TIdTCPClient的服务器端断开连接,您必须定期读取套接字,例如在计时器或单独的线程中.

Delphi Indy TCPClient的OnDisconnect事件为何总是无法正常触发?