ServerSocket 和 Socket 可以使用同一个端口吗?
现在我想使用一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难理解你的问题,因为当你说“端口”时不清楚你是指源端口还是目标端口。
如果您的问题是,“我可以在端口 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.