[英]Indy TCP Server - Handle OnDisconnect already deleted?
我有一个带有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,但以下内容适用于D2010和Indy10.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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.