我想知道当前到 Indy 9 TIdTCPServer 的客户端连接数(在 Delphi 2007 上)
我似乎找不到提供此功能的属性。
我尝试在服务器 OnConnect/OnDisconnect 事件上增加/减少计数器,但当客户端断开连接时,该数字似乎永远不会减少。
有什么建议吗?
最佳答案
当前事件的客户端存储在服务器的 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;
在 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;
关于delphi - 如何使用 Indy TIdTCPServer 跟踪客户端数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5513637/