如何使用 Indy TIdTCPServer 跟踪客户端数量
我想知道当前客户端连接到 Indy 9 TIdTCPServer(在 Delphi 2007 上)的数量,
我似乎找不到提供此信息的属性。
我尝试在服务器 OnConnect/OnDisconnect 事件上增加/减少计数器,但当客户端断开连接时,该数字似乎永远不会减少。
有什么建议吗?
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 tried incrementing/decrementing a counter on the server OnConnect/OnDisconnect events, but the number never seems to decrement when a client disconnects.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当前活动的客户端存储在服务器的
Threads
属性中,该属性是一个TThreadList
。只需锁定列表,读取其Count
属性,然后解锁列表:在 Indy 10 中,
Threads
属性已替换为Contexts
属性:The currently active clients are stored in the server's
Threads
property, which is aTThreadList
. Simply lock the list, read itsCount
property, and then unlock the list:In Indy 10, the
Threads
property was replaced with theContexts
property:不确定为什么使用OnConnect和OnDisconnect对您不起作用,但是我们所做的就是创建tidcustomtcpserver的后代;覆盖其doconnect和dodisconnect方法,并创建和使用我们自己的tidservercontext的后代(线程后代将“服务”连接)。
您使tidcustomtcpserver意识到自己的tidservercontext类,作者:(
编辑添加的条件定义以显示如何使其适用于Indy9)
在我们的tidcustomtcpserver descondant的doconnect覆盖中对于一个独特的价值:
我们的dodisconnect覆盖可以清除ConnectionID:
现在可以随时获得当前连接的计数:
Not sure why using OnConnect and OnDisconnect wouldn't work for you, but what we have done is to create a descendant of TIdCustomTCPServer; to override its DoConnect and DoDisconnect methods and create and use our own descendant of TIdServerContext (a thread descendant that will "serve" a connection).
You make the TIdCustomTCPServer aware of your own TIdServerContext class by:
(Edit Added conditional defines to show how to make it work for Indy9)
In the DoConnect override of our TIdCustomTCPServer descendant we set the ConnectionID of our context class to a unique value:
Our DoDisconnect override clears the ConnectionID:
Now it is possible to get a count of the current connections at any time:
如何从
OnExecute
(或DoExecute
如果您覆盖它)增加/减少计数器?那不会出错的!如果您使用
Interlockedincrement
和InterlockedDecrement
您甚至不需要关键部分来保护计数器。How about incrementing / decrementing a counter from
OnExecute
(orDoExecute
if you override that)? That can't go wrong!If you use
InterlockedIncrement
andInterlockedDecrement
you don't even need a critical section to protect the counter.这应该适用于 Indy 9,但现在它已经相当过时了,也许您的版本中出现了某些问题,请尝试更新到最新的 Indy 9。
我使用 Indy 10 做了一个简单的测试,它与 OnConnect/OnDisconnect 事件处理程序中的简单互锁增量/减量配合得很好。这是我的代码:
然后,使用 telnet 打开几个客户端:
然后,关闭一个客户端
就是这样。
INDY 10 可用于 Delphi 2007,我的主要建议是无论如何都要升级。
This should work on Indy 9, but it is pretty outdated nowadays, and maybe something is broken in your version, try to update to the latest Indy 9 available.
I made a simple test using Indy 10, which works very well with a simple interlocked Increment/Decrement in the OnConnect/OnDisconnect event handlers. This is my code:
then, opening a couple of clients with telnet:
then, closing one client
That's it.
INDY 10 is available for Delphi 2007, my main advise is to upgrade anyway.