ServerSocket(port) 和多线程对于 5000 个并发客户端点击是否能正常工作?

发布于 2024-09-10 06:22:22 字数 417 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2024-09-17 06:22:22

有两件事你应该检查一下。

文件描述符限制。在基于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文