加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 10:12:05 所属栏目:大数据 来源:网络整理
导读:我想知道Indy 9 TIdTCPServer的当前客户端连接数(在Delphi 2007上) 我似乎无法找到一个给出这个的属性. 我已尝试在服务器OnConnect / OnDisconnect事件上递增/递减计数器,但当客户端断开连接时,该数字似乎永远不会减少. 有什么建议么? 解决方法 当前活动的
我想知道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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读