每个人都必须同意 SO_REUSEADDR 吗?

发布于 2024-12-21 05:19:11 字数 826 浏览 0 评论 0原文

我在 SO_REUSEADDR 上看到的所有讨论都假设它是在已知端口上创建并绑定到 TCP 套接字的同一个程序。

我有两个不同的程序使用相同的端口,我很好奇该机制是如何工作的 - 为了让程序 2 分配端口,程序 1 刚刚关闭,它们是否都必须指定 SO_REUSEADDR 在他们创建套接字之后?

或者对于其中一个人来说就足够了?如果是这样,当套接字徘徊在 TIME_WAIT 状态时,是先获取套接字还是随后尝试打开它?

这是一个 Python 中的小例子,希望能让情况更清楚;

# one.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 5050)) # Assuming 5050 is available
sys.exit(1)        # Assuming s enters TIME_WAIT

# two.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 5050))
s.listen()

将 one.py 和 Two.py 视为两个独立的代码库。

one.py 和 Two.py 是否都需要设置 SO_REUSEADDR 套接字选项,以便 Two.py 能够容忍来自 one.py 的挥之不去的 TIME_WAIT 套接字?

谢谢。

All discussion I've seen on SO_REUSEADDR assumes that it's the same program creating and binding to a TCP socket on a known port.

I have two different programs using the same port, and I'm curious about how the mechanism works -- in order for program 2 to allocate a port program 1 has just closed, do they both have to specify SO_REUSEADDR after they create the socket?

Or is it enough for one of them? If so, the one taking the socket first or the one trying to open it afterwards, when it's lingering in TIME_WAIT state?

Here's a small example in Python to hopefully make the case clearer;

# one.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 5050)) # Assuming 5050 is available
sys.exit(1)        # Assuming s enters TIME_WAIT

# two.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 5050))
s.listen()

Think of one.py and two.py as two separate codebases.

Does both one.py and two.py need to set the SO_REUSEADDR socket option in order for two.py to tolerate a lingering TIME_WAIT socket from one.py?

Thank you.

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

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

发布评论

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

评论(4

孤蝉 2024-12-28 05:19:11

来回答你的问题。我相信在Linux上,您必须在想要重用端口的程序中ONLY指定SO_REUSEADDR。很简单。然而在 Microsoft Windows 上,情况就不同了。 Microsoft 在 MSDN 上有一个页面< /a> 涵盖 SO_REUSEADDR 和相关功能。

To answer your question. I believe that on Linux you have to specify SO_REUSEADDR ONLY in the program that wants to reuse the port. Very simple. On Microsoft Windows however, this is a different story. Microsoft has a page on MSDN that covers SO_REUSEADDR and related features.

我爱人 2024-12-28 05:19:11

我认为是的,所有使用相同端口的程序都应该使用SO_REUSEADDR

因为重用端口的超时是内核的事情。

(但我可能是错的)。

I think that yes, all programs using the same port should use SO_REUSEADDR.

Because the timeout for reusing ports is a kernel thing.

(but I may be wrong).

莳間冲淡了誓言ζ 2024-12-28 05:19:11

监听套接字背后的系统调用是bind(),并且您不能同时绑定到同一地址上的同一端口:操作系统禁止这样做。 SO_REUSEADDR 告诉程序退出后其他套接字可能会监听。但只要它没有退出,它就是这个端口的专有。

The system call behind listening sockets is bind(), and you cannot bind to the same port on the same address(es) at the same time: the OS forbids that. SO_REUSEADDR tells that other sockets may listen after the program exits. But as long as it has not exited, it is proprietary of this port.

丑疤怪 2024-12-28 05:19:11
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))

您可以通过将 SO_LINGER 套接字选项设置为零来突然终止 TCP 协商流的 TIME_WAIT 状态。

当您允许两种状态(已建立或什么都没有)时,这很好。

s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))

You can abruptly terminate the TIME_WAIT state of TCP negotiation flow by setting SO_LINGER socket option to zero.

Good when you allow two states, ESTABLISHED or nothing.

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