套接字意外关闭

发布于 2024-07-24 12:05:28 字数 410 浏览 9 评论 0原文

我正在为服务器编写一个测试程序。 在测试应用程序中,我尝试将大量客户端连接到服务器,但一段时间后会出现以下各种错误:

Connection reset by peer: socket write error                   

或者

java.net.SocketException: Connection reset                     

或者

java.net.ConnectException: Connection refused: connect

我为连接到服务器的每个客户端使用一个新的套接字。

有人可以告诉我这种奇怪的行为吗?

I am writting a test program for a server. At the test app, I try to connect a great number of clients to the server, but after a while a get all kind of errors like these :

Connection reset by peer: socket write error                   

or

java.net.SocketException: Connection reset                     

or

java.net.ConnectException: Connection refused: connect

I use a new socket for every client I connect to the server.

Could someone enlighten me about this strange behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

鸠书 2024-07-31 12:05:28

不幸的是,您没有提供有关服务器性质的详细信息。 我想你正在编写一个典型的 TCP 服务器。 在这个答案中,我将不会讨论任何特定于 Java 的细节。

简短的建议是:在客户端连接之间插入延迟。 如果没有它,您就会主动模拟对服务器的 DoS 攻击。

对于较长的内容,请阅读下文。

通常,TCP 服务器通过调用(在可爱的 C 接口中)int sockfd = socket(...) 函数并传递结果 (在我们的例子中,sockfd)连接到 bind()listen() 函数。 准备工作完成后,服务器将调用 accept() ,这将使服务器进入休眠状态(如果套接字被标记为阻塞),并且地球另一端的客户端将开始调用connect() 函数,然后 accept()(在服务器端)在操作系统内核的支持下将创建连接的套接字

通过查看listen() 函数可以了解可能的待处理连接的实际数量。 listen() 有一个 backlog 参数,它定义操作系统内核应排队到套接字的最大连接数(这基本上是所有处于 SYN_RCVDESTABLISHED 状态的连接)。 从历史上看,20 世纪 80 年代 backlog 的推荐值约为 5,这在我们这个时代显然是悲惨的。 例如,在 FreeBSD 7.2 中,可以通过键入以下内容来猜测 积压硬限制

% sysctl kern.ipc.somaxconn
kern.ipc.somaxconn: 128

在 Fedora 10 中:

% cat /proc/sys/net/core/somaxconn
128

PS
抱歉我糟糕的英语。

Unfortunately you haven't provided much details of your server's nature. I suppose you are writing a typical TCP server. In this answer I will not talk about any Java-specific details.

The short advice is: insert a delay between clients connections. Without it you are actively simulating a DoS attack to your server.

For the longer one, read below.

Usually a TCP server creates only 1 listening socked by calling (in lovely C interface) int sockfd = socket(...) function, and passing the result (sockfd in our case) to bind() and listen() functions. After this preparations, the server would call an accept() which will steep the server in slumber (if the socket was marked as blocking) and if a client on the other side of the Earth will start calling a connect() function, than accept() (on the server side) with the support of the OS kernel will create the connected socket.

The actual number of possible pending connectins can be known by looking at the listen() function. listen() has a backlog parameter which defines the maximum number of connection the OS kernel should queue to the socket (this is basically a sum of all connections in SYN_RCVD and ESTABLISHED states). Historically the recommended value for backlog in 1980s was something like 5 which is obviously miserable in our days. In FreeBSD 7.2, for example, a hard limit for backlog may be guessed by typing:

% sysctl kern.ipc.somaxconn
kern.ipc.somaxconn: 128

and in Fedora 10:

% cat /proc/sys/net/core/somaxconn
128

P.S.
Sorry for my terrible English.

忘你却要生生世世 2024-07-31 12:05:28

您的网络/应用程序服务器一次只能为有限数量的客户端提供服务。

当达到此限制时,您将收到连接被拒绝/重置的消息。

希望这能回答你的问题。

干杯

Your web/app server can only service a finite amount of clients at a time.

You will receive a connection refused/reset when this limit is reached.

Hope that answers your question.

Cheers

无妨# 2024-07-31 12:05:28

操作系统和网络服务器限制了您可以启动/接受的连接速度和连接数量。 如果您想在服务器上进行性能测试,请尝试 Apache JMeter,因为它可以解决这些限制。

There are OS and webserver limits how fast and how many connection you can start/accept. If you want to do performance testing on the server, try Apache JMeter as it has solutions to these limits.

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