简体   繁体   中英

Java client socket returned by ServerSocket.accept()

This is more of a general socket question. In Java, if I have a ServerSocket bound to a specific port, say 4444, I understand that it's listening for connection requests. The accept() method blocks until a connection request is received and it returns a new Socket to communicate with the client. The remote endpoint of the returned Socket is the IP address and port of the client.

However, where I'm getting confused is that the local port of the returned Socket is the same as the port number of the ServerSocket that's listening for requests (in this case 4444). I thought two sockets couldn't be bound to the same port? Wouldn't the returned Socket be a different port on the server?

Two ServerSockets cannot listen to the same IP address and port. Its possible to have two ServerSockets listening on different IPs and the same port.

Two sockets cannot be have the same IP and port for the local AND remote. The local port can be the same for many Sockets provide the remote IP & ports are different.

You cannot use multiple sockets to listen on a given IP:port combination, but multiple accepted connections to the same combination can exist.

The connection is identified by the combination of the local IP address and port and the remote IP address and port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM