2010-05-18 24 views
0

我有一个使用Indy TCPServer和TCPClient的delphi应用程序我使用AContext.Bindind.Handle来识别每个连接(错误?)。Indy TCP服务器 - 处理OnDisconnect allready已删除?

所以我有显示网格的连接,我会删除断开连接后,输入:

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

你没有提到你正在使用的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; 
+0

很好的建议。始终使用自己的标识符,不要将Indy的内部对象和句柄用作ID。 – 2010-05-18 20:58:22