On the server side we use
Socket server = serverSocket.accept();
to create a socket. After creating the socket, we can create a new stream to handle the input / output stream of this socket. Thus, we can return to listening on the same port and create a new socket if further connection requests appear. Since we already created ServerSocket on a specific port, of course, we were not able to create another server server in that port again.
So, from my understanding, can I conclude that on the server side we can create several sockets under one port? (similar to what the web server does)
Actually, my question is: on the client side, when we create the socket, we can specify the local port that we want to use. After we have successfully created a client socket in this local port, can we reuse this port for another client socket? Does this port support socket binding until the socket is closed (or close the port)? Since there is no “Listening” concept on the client side, can we do the same as ServerSocket (see ServerSocket can create several sockets under one port)?
I am seriously confused how the client side handles the port and socket, because I am comparing ServerSocket with the client socket.
Please point me in the right direction, I know that my thinking is somehow wrong. Thank you very much.
GMsoF source share