我有一个带有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;
Run Code Online (Sandbox Code Playgroud)
但是在“断开事件”中,句柄已经为空(它曾经是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;
Run Code Online (Sandbox Code Playgroud)
修改后的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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1554 次 |
| 最近记录: |