How to track number of clients with Indy TIdTCPServer

后端 未结 4 523
Happy的楠姐
Happy的楠姐 2021-01-03 07:37

I want to know the number of current client connections to an Indy 9 TIdTCPServer (on Delphi 2007)

I can\'t seem to find a property that gives this.

I\'ve tr

4条回答
  •  感动是毒
    2021-01-03 08:22

    The currently active clients are stored in the server's Threads property, which is a TThreadList. Simply lock the list, read its Count property, and then unlock the list:

    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;
    

    In Indy 10, the Threads property was replaced with the Contexts property:

    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;
    

提交回复
热议问题