TCP - 是否可以将相同的客户端端口同时用于不同应用程序的不同连接? [英] TCP - possible for same client-side port to be used for different connections by different applications simlutaneously?
问题描述
在 TCP 中,不共享相同可执行映像的不同进程是否有可能(例如,没有 fork()
)在 Windows、Linux 或 OSX 上使用相同的客户端端口?我相信这与使用 setsockopt()
设置的套接字选项 SO_REUSEADDR
和 SO_REUSEPORT
特别相关.
Is it possible in TCP for different processes not sharing the same executable image (so no fork()
for example) to use a same client-side port on Windows, Linux or OSX? This is specifically related to the socket options SO_REUSEADDR
and SO_REUSEPORT
set using setsockopt()
I believe.
据我所知,我相信同一个进程/映像可以执行此操作,但我还没有找到有关多个进程/映像的信息.我想这在理论上是可能的,因为每个套接字都是由 5 值元组 [IP_PROTO, src_ip:src_port, dst_ip:dst_port] 定义的.所以我会假设,只要共享一个客户端端口的多个 TCP 连接没有建立到同一个 dst_ip:dst_port,这在理论上是可能的.
As far as I've read, I believe it is possible for the same process/image to do this, but I haven't found information as to multiple processes/images. I would imagine it is theoretically possible since each socket is defined by the 5-valued tuple [IP_PROTO, src_ip:src_port, dst_ip:dst_port]. So I would assume that, as long as multiple TCP connections sharing a client-side port are not made to the same dst_ip:dst_port, this would be theoretically possible.
推荐答案
UDP 不是面向连接的,客户端和服务器之间没有真正的区别,所以对于 UDP 这个问题没有多大意义.
UDP is not connection-oriented and has no real distinction between client and server, so for UDP this question doesn't make a lot of sense.
对于 TCP,您可以使用 SO_REUSEADDR 将多个客户端绑定到同一个端口,但为什么要这样做?通常,您在建立连接之前让客户端处于未绑定状态,让内核为您选择一个未使用的端口.
For TCP, you can use SO_REUSEADDR to bind mulitple clients to the same port, but why would you want to? Normally you leave the client unbound before making a connection and let the kernel pick an unused port for you.
这篇关于TCP - 是否可以将相同的客户端端口同时用于不同应用程序的不同连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!