簡體   English   中英

Indy TCP服務器-處理OnDisconnect已刪除?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM