Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is new socket created for every request?

I am trying to wrap my head around network sockets. So far my understanding is that a server creates a new socket that is bound to the specific port. Then it listens to this socket to deal with client requests.

I've read this tutorial http://docs.oracle.com/javase/tutorial/networking/sockets/definition.html and it says

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.

Here are a few things that I don't quite understand

If everything goes well, the server accepts the connection.

  1. Does it mean that a client request successfully arrived at the listening socket?

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

  1. The new socket is created. It also gets bound to the same port but it doesn't listen for incoming requests. After server processed client request resonse is written to this socket and then it gets closed. Is it correct?

  2. Does it mean that request is somehow passed from the first socket to the second socket?

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.

  1. So, the new socket is created then that listens for incoming request. Are there different type of sockets? Some kind of "listening" sockets and other?

  2. Why does the server have to create a new listening socket? Why can't it reuse the previous one?

like image 306
user1745356 Avatar asked Jan 05 '14 21:01

user1745356


People also ask

Why does accept () return a new socket for each accepted connection?

Because the initial socket is used to wait for communication while the second is used to communicate.

How many sockets does TCP create?

Why does TCP socket programming need two sockets(one welcome socket and one connection socket) but UDP only needs one?

How many sockets can an application have?

Maximum number of sockets. For most socket interfaces, the maximum number of sockets allowed per each connection between an application and the TCP/IP sockets interface is 65535.

Can an application have multiple sockets?

You ask: Can a process have multiple sockets? In general yes, mostly in the same way a process can have multiple files opened simultaneously. On UNIX and UNIX-like systems the limit is basically given by the limits on the maximum number of opened file descriptors per process and the availability of system resources.


1 Answers

  1. No. It means that an incoming connection arrived at the server.
  2. No. It gets closed if the server closes it. Not otherwise.
  3. No. It means that the incoming connection causes a connection to be fully formed and a socket created at the server to represent the server-end endpoint of it.
  4. (a) No. A new socket is created to receive requests and send responses. (b) Yes. There are passive and active sockets. A passive socket listens for connections. An active socket sends and receives data.
  5. It doesn't have to create a new listening (passive) socket. It has to create a new active socket to be the endpoint of the new connection.

Is new socket created for every request?

Most protocols, for example HTTP with keep-alive, allow multiple requests per connection.

like image 162
user207421 Avatar answered Dec 01 '22 18:12

user207421