TCP 写入连接超时(netstat 显示 ESTABLISHED)

发布于 2024-12-11 13:37:29 字数 382 浏览 3 评论 0原文

我做了一个实验:
服务器监听8804端口,接受客户端的连接,然后不断地向客户端发送数据。我关闭网络。

  1. 当我运行 netstat -anotp | grep 8804 ,显示服务器和客户端的连接均已“建立”,但没有数据传输。
  2. 过了一会儿,服务器抛出一个错误:“连接超时”
  3. netstat -anotp | grep 8804 并发现客户端仍然是“ESTABLISHED”

所以:
1. 为什么在系统调用“write”上被阻塞的服务器会抛出“连接超时”错误。为什么不是客户?
2. 如何让客户端发现连接实际上已关闭。
3. 网络不通时,为什么服务器和客户端的状态都是“ESTABLISHED”?

感谢您的回答!

I made an experiment:
A server listens on port 8804 accepts a connection of a client and then send data to the client endless. I shutdown the network.

  1. When I run netstat -anotp | grep 8804 ,it shows that the connection is "ESTABLISHED" on both server and client , but there is no data transmission.
  2. After a while , the server throw an error : "Connection time out"
  3. netstat -anotp | grep 8804 and found that the client is still "ESTABLISHED"

So:
1. Why does the server which is blocked on the system call "write" throw the "Connection timeout" error. Why not the client ?
2. How to let the client find the connection is shutdown actually.
3. Why are the server and client's statuses both "ESTABLISHED" when the network does not work ?

Thanks for your answer !

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

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

发布评论

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

评论(1

心舞飞扬 2024-12-18 13:37:29
  1. 您的服务器正在等待发送给客户端的各个数据段的 TCP ACK;然而,客户端不知道服务器的数据有多长。由于您关闭了网络,服务器不再从客户端获取 ACK。结果:服务器上的连接超时(请参阅注释 1)
  2. 在套接字上使用 TCP Keepalives(请参阅注释 2)
  3. 您尚未启用 TCP Keepalives。如果您使用的是 python,则可以这样做(假设您的套接字名为 s):
# Do this before you accept() anything on the socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

编辑:
由于您使用的是 C,因此链接到 Linux TCP Keepalives Howto

注释

  1. RFC 1122:第 4.2.3.5 节“TCP 连接失败”
  2. < a href="https://www.rfc-editor.org/rfc/rfc1122#page-101" rel="nofollow noreferrer">RFC 1122:第 4.2.3.6 节“TCP Keepalives”
  1. Your server is expecting TCP ACKs for individual data segments that it sends to the client; however, the client has no idea how long the server's data is. Since you shutdown the network the server no longer gets ACKs from the client. Result: Connection timeout on the server (See Note 1)
  2. Use TCP Keepalives on your socket (See Note 2)
  3. You have not enabled TCP Keepalives. If you are using python, you can do so like this (assuming your socket is named s):
# Do this before you accept() anything on the socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

EDIT:
Since you're using C, a link to the Linux TCP Keepalives Howto

NOTES

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