如何使用Indy TIdTCPServer跟踪客户端数量

Rod*_*ddy 4 delphi indy tcpserver

我想知道Indy 9 TIdTCPServer的当前客户端连接数(在Delphi 2007上)

我似乎无法找到一个给出这个的属性.

我已尝试在服务器OnConnect/OnDisconnect事件上递增/递减计数器,但当客户端断开连接时,该数字似乎永远不会减少.

有什么建议?

Rem*_*eau 9

当前活动的客户端存储在服务器的Threads属性中,即属性TThreadList.只需锁定列表,阅读其Count属性,然后解锁列表:

procedure TForm1.Button1Click(Sender: TObject);
var
  NumClients: Integer;
begin
  with IdTCPServer1.Threads.LockList do try
    NumClients := Count;
  finally
    IdTCPServer1.Threads.UnlockList;
  end;
  ShowMessage('There are currently ' + IntToStr(NumClients) + ' client(s) connected');
end;
Run Code Online (Sandbox Code Playgroud)

在Indy 10,该Threads物业被该物业取代Contexts:

procedure TForm1.Button1Click(Sender: TObject);
var
  NumClients: Integer;
begin
  with IdTCPServer1.Contexts.LockList do try
    NumClients := Count;
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;
  ShowMessage('There are currently ' + IntToStr(NumClients) + ' client(s) connected');
end;
Run Code Online (Sandbox Code Playgroud)