0

我有一个带有 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,所以最后一个整数)。

想法?

4

1 回答 1

5

您没有提及您使用的是哪个版本的 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;
于 2010-05-18T09:33:15.907 回答