ServerSocket(port) 和多线程对于 5000 个并发客户端点击是否能正常工作?
我正在使用serversocket类并在新客户端点击serversocket时打开线程:
serverSocket = new ServerSocket(port);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(this).start();//opening multiple threads
}
但是当5000个客户端点击此serversocket时,客户端会显示错误:java.net.ConnectException:连接被拒绝:连接
请告诉我打开服务器套接字以便它可以接受所有客户端连接的更好方法是什么?同时客户端连接之间有 1 毫秒的时间间隔。
I am working with the serversocket class and opening threads when a new client hits the serversocket:
serverSocket = new ServerSocket(port);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(this).start();//opening multiple threads
}
but when 5000 clients hits on this serversocket an error shows up on the client side: java.net.ConnectException: Connection refused: connect
Please tell me what is the better way to open a serversocket so that it can accept all the client connections? there is a 1 millisecond time gap between simultaneous client connections.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事你应该检查一下。
文件描述符限制。在基于 Linux 的系统上,您可以使用 ulimit 进行检查。
其次,Serversockets有等待队列大小,如果等待的连接数超过队列大小,它们将立即被拒绝。您应该尝试 ServerSocket(port, backlog) 构造函数。
There are 2 things you should be checking out definately.
File descriptor limit. On a Linux based system, you can check that using ulimit.
Secondly, Serversockets have waiting queue size, if you have more connections waiting than queue size, they will be refused immediately. You should try ServerSocket(port, backlog) constructor.