加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – Indy TCPClient OnDisconnect事件无法正常工作

发布时间:2020-12-15 09:09:20 所属栏目:大数据 来源:网络整理
导读:type TForm8 = class(TForm) idtcpclnt1: TIdTCPClient; idtcpsrvr1: TIdTCPServer; procedure FormCreate(Sender: TObject); procedure idtcpsrvr1Execute(AContext: TIdContext); procedure idtcpclnt1Disconnected(Sender: TObject); private { Private d
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的服务器端断开连接,您必须定期读取套接字,例如在计时器或单独的线程中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读