ServerSocket accept() method
The diagram is incorrect (and is listed in the unconfirmed errata on the O'Reilly site).
The client chooses its port at random (you don't need to do anything special in Java) and connects to the server on whichever port you specified. Using the netstat
commandline tool you can see this.
First, just the listening server socket with no clients:
simon@lucifer:~$ netstat -n -a Active Internet connections (including servers) Proto Recv-Q Send-Q Local Address Foreign Address (state) ... tcp46 0 0 *.5050 *.* LISTEN ...
(there are lots of other entries, I've just removed the unrelated ones)
Now with one client connecting from localhost (127.0.0.1):
simon@lucifer:~$ netstat -n -a Active Internet connections (including servers) Proto Recv-Q Send-Q Local Address Foreign Address (state) ... tcp4 0 0 127.0.0.1.64895 127.0.0.1.5050 ESTABLISHED <- 1 tcp4 0 0 127.0.0.1.5050 127.0.0.1.64895 ESTABLISHED <- 2 tcp46 0 0 *.5050 *.* LISTEN <- 3 ...
Since the client is connecting from the same machine, we see two established connections - one from client to server (1), the other from server to client (2). They have opposite local and foreign addresses (since they're talking to each other) and you can see the server is still using port 5050 while the original server socket (3) continues to listen on the same port.
(this output is from a Mac, but Windows/Linux also have netstat
giving similar output)
You chose the port, when you said new ServerSocket(5050). All that stuff about using a different port for the accepted socket is 100% BS.