环回接口上的 TIME_WAIT

发布于 2024-07-09 09:55:00 字数 658 浏览 8 评论 0原文

为什么与环回接口的 TCP 连接以 TIME_WAIT 结束(套接字以 SO_DONTLINGER 设置关闭),但与不同主机的相同连接不会以 结束>TIME_WAIT(它们立即重置/销毁)?

以下是用于说明的场景:

(A) 两个应用程序(客户端和服务器)都运行在同一台 Windows 计算机上。 客户端通过服务器的环回接口(127.0.0.1,端口 xxxx)连接到服务器,发送数据,接收数据,并关闭套接字(设置了SO_DONTLINGER)。

假设连接非常存在时间很短,因此客户端应用程序每秒都会建立和销毁大量连接。 最终结果是套接字最终陷入TIME_WAIT,并且客户端最终耗尽其最大套接字数(在 Windows 上,默认情况下约为 3900,我们假设该值不会在注册表中更改)。

(B) 与场景 (A) 相同的两个应用程序,但服务器位于不同的主机上(客户端仍在 Windows 上运行)。 这些连接在各方面都是相同的,只是它们的目的地不是 127.0.0.1,而是其他一些 IP。 这里,客户端计算机上的连接不会进入TIME_WAIT,并且客户端应用程序可以无限期地继续建立连接。

为什么会出现差异?

Why is it that TCP connections to a loopback interface end up in TIME_WAIT (socket closed with SO_DONTLINGER set), but identical connections to a different host do not end up in TIME_WAIT (they are reset/destroyed immediately)?

Here are scenarios to illustrate:

(A) Two applications, a client and a server, are both running on the same Windows machine. The client connects to the server via the server's loopback interface (127.0.0.1, port xxxx), sends data, receives data, and closes the socket (SO_DONTLINGER is set).

Let's say that the connections are very short-lived, so the client app is establishing and destroying a large number of connections each second. The end result is that the sockets end up in TIME_WAIT, and the client eventually exhausts its max number of sockets (on Windows, this is ~3900 by default, and we are assuming that this value will not be changed in the registry).

(B) Same two applications as scenario (A), but the server is on a different host (the client is still running on Windows). The connections are identical in every way, except that they are not destined for 127.0.0.1, but some other IP instead. Here the connections on the client machine do NOT go into TIME_WAIT, and the client app can continue to make connections indefinitely.

Why the discrepancy?

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

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

发布评论

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

评论(1

时光礼记 2024-07-16 09:55:00

TIME_WAIT 状态仅发生在连接的一端——首先关闭的一端。 对于环回接口,两端都在同一台机器上,因此您将始终看到 TIME_WAIT。

在您的其他情况下,请尝试查看另一台机器。 我想你会在那里看到 TIME_WAIT 套接字。

The TIME_WAIT state only occurs at one end of the connection -- the end that closes first. For the loopback interface both ends are on the same machine so you will always see TIME_WAIT.

In your other case, try looking at the other machine. I think you'll see the TIME_WAIT sockets there.

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