Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ServerSocket create a new socket for each connection? [duplicate]

I read following statement in the Socket tutorial.

If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.

Below are my questions:

  1. Please correct my understanding from above - I open a socket (ServerSocket) by specifying a port, lets say 8001, now my server will start listening to requests on this port, now it gets a request from a client, something like http://abc:8001/test, so once it accepts the connection it will create a local port lets say 10001, so now this client request will be entertained over 10001 (or in other words connection with client would be established over port 10001) while 8001 will again be free and listening for new requests?
  2. So, does it means that original port I specified with ServerSocket will never be used for establishing connections?
  3. So, does it mean that if I having a connection with a client, let say over port 9001, then there can never be one more communication happening (or in other words connection established) on the same 9001 port until the original request I am catering is completed?

Please feel free to detailed light on the concepts of these questions, this will not only help me but also future visitors.

like image 653
pjj Avatar asked Jul 16 '26 14:07

pjj


1 Answers

Does ServerSocket create a new socket for every connection?

Yes.

I open a socket (ServerSocket) by specifying a port, lets say 8001, now my server will start listening to requests on this port, now it gets a request from a client, something like http://abc:8001/test, so once it accepts the connection it will create a local port lets say 10001,

No.

so now this client request will be entertained over 10001

No.

(or in other words connection with client would be established over port 10001)

No.

while 8001 will again be free and listening for new requests?

Yes.

So, does it means that original port I specified with ServerSocket will never be used for establishing connections?

No.

So, does it mean that if I having a connection with a client, let say over port 9001, then there can never be one more communication happening (or in other words connection established) on the same 9001 port until the original request I am catering is completed?

No.

The accepted socket uses the same local port as the listening socket, exactly as per the text you quoted.

You are confusing new sockets, as per your title, with new ports, as per the body of your question.

like image 144
user207421 Avatar answered Jul 19 '26 02:07

user207421