Java Socket and ServerSocket differences in port usage

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.

+4
source share
3 answers

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)

You mix yourself with terminology. ServerSocket.accept() accepts the connection and terminates the endpoint in Socket . The endpoint has the same local port number as ServerSocket , as defined by RFC 793 , and therefore Socket ServerSocket .

Actually, my question is: on the client side, when we create the socket, we can specify the local port that we want to use.

We can, but we rarely do it.

After we have successfully created a client socket in this local port, can we reuse this port for another client socket?

No.

Does this port support socket binding until the socket is closed (or the port is closed)?

Yes, or rather, vice versa: the socket is bound to a 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)?

No.

+3
source

A ServerSocket can simply be thought of as a Socket factory for inbound connections. For each incoming client connection, the ServerSocket.accept() method returns a new Socket to communicate with it and only with this client.

In other words, any number of connections (limited only by the OS) can be made for a single ServerSocket , and each client connection will receive a separate Socket for communication, all communications using the same server side TCP port.

+3
source

You should imagine the socket as a two-pair array of information:

  • {Self Port, Self Addr}
  • {Dest Port, Dest Addr}

therefore, one server can have many connections connected to it, which differ in their {Dest Port, Dest Addr}

 example: Server port 10000 addr 10.0.0.1 Socket 1: - {10000,10.0.0.1} - {10001,10.0.0.2} Socket 2: - {10000,10.0.0.1} - {10002,10.0.0.1} - address may seem the same but as a whole its a different destination 

hope this helps.

+1
source

Source: https://habr.com/ru/post/949771/


All Articles