【问题标题】:Indy TCP Server - Handle OnDisconnect already deleted?Indy TCP 服务器 - 处理 OnDisconnect 已删除?
【发布时间】:2011-02-20 18:51:06
【问题描述】:

我有一个带有 Indy TCPServer 和 TCPClient 的 Delphi 应用程序。我使用AContext.Bindind.Handle 来识别每个连接(错误?)。

所以我有一个显示连接的网格,我将在断开连接后删除条目:

procedure TfrmMain.serverIndyDisconnect(AContext: TIdContext);
var I:Integer;
begin
for I := 0 to gridClients.RowCount - 1 do
begin
  if gridClients.Cells[0, I] = IntToStr(AContext.Binding.Handle) then
  begin
     gridClients.Rows[I].Delete(I);
  end;
end;

WriteLogEntry('Connection closed... (' + AContext.Binding.PeerIP+')');
end;

但是在断开连接事件中,句柄已经是空的(它曾经是 401xxxxx,所以最后一个整数)。

想法?

【问题讨论】:

    标签: delphi indy


    【解决方案1】:

    您没有提及您使用的是哪个版本的 Delphi 或 Indy,但以下内容适用于 D2010 和 Indy 10.x。

    我使用“AContext.Data”属性来识别客户端。我通常在那里创建一个对象并在断开事件发生时释放它。

    新的 OnConnect() 代码:

    procedure TfrmMain.serverIndyConnect(AContext: TIdContext);
    begin
      AContext.Data := TMyObject.Create(NIL);
      // Other Init code goes here, including adding the connection to the grid
    end;
    

    修改 OnDisconnect() 代码如下:

    procedure TfrmMain.serverIndyDisconnect(AContext: TIdContext);
    var I:Integer;
    begin
      for I := 0 to gridClients.RowCount - 1 do
      begin
        if gridClients.Cells[0, I] = IntToStr(AContext.Data) then
        begin
          gridClients.Rows[I].Delete(I);
        end;
     end;
     WriteLogEntry('Connection closed... (' + AContext.Binding.PeerIP+')');
    end;
    

    【讨论】:

    • 好建议。始终使用自己的标识符,不要使用 Indy 的内部对象和句柄作为 ID。
    猜你喜欢
    • 2012-12-21
    • 2023-03-07
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多