简体   繁体   English

Indy TCP服务器-处理OnDisconnect已删除?

[英]Indy TCP Server - Handle OnDisconnect already deleted?

I have a Delphi application with a Indy TCPServer and TCPClient. 我有一个带有Indy TCPServer和TCPClient的Delphi应用程序。 I use the AContext.Bindind.Handle for the identification of each connection (wrong?). 我使用AContext.Bindind.Handle标识每个连接(错吗?)。

So I have a grid which displayed the connections and I will remove the entry after disconnection: 所以我有一个显示连接的网格,断开连接后我将删除该条目:

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;

But in the Disconnect Event, the Handle is already empty (it's ever 401xxxxx, so the last Integer number). 但是在“断开事件”中,句柄已经为空(它曾经是401xxxxx,所以是最后一个整数)。

Ideas? 想法?

You do not mention which version of Delphi or Indy you are using, but the following holds for D2010 and Indy 10.x. 您没有提到使用的是哪个版本的Delphi或Indy,但以下内容适用于D2010和Indy10.x。

I've used the "AContext.Data" property for identification of the client. 我已经使用“ AContext.Data”属性来标识客户端。 I usually Create an object there and release it when the disconnect event happens. 我通常在此处创建一个对象,并在断开连接事件发生时释放它。

New OnConnect() code: 新的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;

Modified OnDisconnect() code below: 修改后的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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM