ServerSocket 和 Socket 可以使用同一个端口吗?

发布于 2024-12-15 21:31:22 字数 174 浏览 2 评论 0原文


现在我想使用一个serversocket来监听端口x,同时我想使用端口x与其他serversocket建立一个套接字,这意味着有一个serversocket和一个socket将使用同一个端口,这可能吗?
我知道对于套接字,它是四元组,一个端口可以有多个套接字,但是对于套接字和服务器套接字?
感谢您的关注!

Now I want to use a serversocket to listen port x, in the same time I want to use the port x to establish a socket to other serversocket, that means there is a serversocket and a socket will use one same port, is that possible?
I know for socket, it is four tuple, one port can have multiple socket, but for a socket and serversocket?
thank you for your attention!

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

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

发布评论

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

评论(1

谎言 2024-12-22 21:31:22

很难理解你的问题,因为当你说“端口”时不清楚你是指源端口还是目标端口。

如果您的问题是,“我可以在端口 X 上接受入站连接,并在端口 X 上建立出站连接吗?”答案是肯定的。 (您可以绑定到连接到同一端口。)

如果您的问题是,“我可以在端口 X 上监听并进行出站操作吗? 来自端口 X 的连接?”答案是否定的——除非您使用两个不同的本地 IP 地址。对于单个 IP 地址上的 TCP,您只能绑定到该端口一次。如果您绑定到侦听,从那时起,该端口将保留用于您将接受的传入连接 - 您无法再次绑定到它以从它建立传出连接。

It's hard to understand your question because it's not clear when you say "port" whether you mean source port or destination port.

If your question is, "Can I accept inbound connections on port X and also make outbound connections to port X?" The answer is yes. (You can bind to and connect to the same port.)

If your question is, "Can I listen on port X and also make outbound connections from port X?" The answer is no -- unless you use two different local IP addresses. For TCP on a single IP address, you can only bind to the port once. If you bind to listen, from then on, the port is reserved for the incoming connections you will accept -- you cannot bind to it again to make outgoing connections from it.

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