套接字意外关闭
我正在为服务器编写一个测试程序。 在测试应用程序中,我尝试将大量客户端连接到服务器,但一段时间后会出现以下各种错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,您没有提供有关服务器性质的详细信息。 我想你正在编写一个典型的 TCP 服务器。 在这个答案中,我将不会讨论任何特定于 Java 的细节。
简短的建议是:在客户端连接之间插入延迟。 如果没有它,您就会主动模拟对服务器的 DoS 攻击。
对于较长的内容,请阅读下文。
通常,TCP 服务器通过调用(在可爱的 C 接口中)
int sockfd = socket(...)
函数并传递结果 (在我们的例子中,sockfd
)连接到bind()
和listen()
函数。 准备工作完成后,服务器将调用accept()
,这将使服务器进入休眠状态(如果套接字被标记为阻塞),并且地球另一端的客户端将开始调用connect()
函数,然后accept()
(在服务器端)在操作系统内核的支持下将创建连接的套接字。通过查看
listen()
函数可以了解可能的待处理连接的实际数量。listen()
有一个 backlog 参数,它定义操作系统内核应排队到套接字的最大连接数(这基本上是所有处于SYN_RCVD
和ESTABLISHED
状态的连接)。 从历史上看,20 世纪 80 年代 backlog 的推荐值约为 5,这在我们这个时代显然是悲惨的。 例如,在 FreeBSD 7.2 中,可以通过键入以下内容来猜测 积压 的硬限制:在 Fedora 10 中:
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) tobind()
andlisten()
functions. After this preparations, the server would call anaccept()
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 aconnect()
function, thanaccept()
(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 inSYN_RCVD
andESTABLISHED
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:and in Fedora 10:
P.S.
Sorry for my terrible English.
您的网络/应用程序服务器一次只能为有限数量的客户端提供服务。
当达到此限制时,您将收到连接被拒绝/重置的消息。
希望这能回答你的问题。
干杯
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
操作系统和网络服务器限制了您可以启动/接受的连接速度和连接数量。 如果您想在服务器上进行性能测试,请尝试 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.