本文介绍了连接到服务器的套接字客户端数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NIO TCP服务器,该服务器可以接收和读取来自客户端的数据.我想定期了解已连接客户端的数量,以便在 java 中进行监控(如netstat或lsof ).有什么方法可以在不维护计数器的情况下实现此目的,而每次客户端连接时都要增加计数器?

I have a NIO TCP server which accepts and reads data from clients. I would like to know the count number of clients connected at regular intervals, for monitoring purpose (like netstat or lsof) in java. Is there any way to achieve this without maintaining a counter and increment it every time a client connects?

类似的东西:

int clientsConnected = serverSocket.getClientsConnected();

推荐答案

我想不出什么办法,您只需要拥有一个名为count的变量,并每次对其进行递增.

There is no way that I can think of, you would just have to have a variable called count and increment it each and every time.

int count = 0;
server.accept();
count++;

这篇关于连接到服务器的套接字客户端数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 11:57