我想知道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;