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.