17

Actually I am learning Computer Networks and while at it, I am confused in how a web server maintains multiple connections?

Simply to state, I learned trough some googling is that sockets handles a each client request.

So lets say there is a web server and lets say 2 clients with IPs

Client A: 5.5.5.5
Client B: 10.10.10.10 

They both try to connect to the server at port 80.

Now, from googling what I got is, the server listens at port 80 for incoming requests. Then lets say a client A tries to connect to the server (make TCP/IP connection). During which a socket is created between these two. Then it is executed as separate thread for its further communication making the server to again listen for other client requests at that particular port. And Client B connects the same way.

Now my first question is:

1. How does server communicate with these two clients simultaneously  
   after the connection has been established?

Now practically not only 2 clients but thousands and millions user may connect to a server.

Then my next question is:

2. Now, how do those thousands of clients get connected to a single server?

   If we assume every client is connected to the server through wire, it is not 
   practically possible to maintain that many sockets on a hardware for
   connection. How those thousands connections are made and handled?

Lastly, my 3rd question is:

3. Above I said (actually heard) how **client A** connected to the the server
   and similarly the client B.

   But I didn't get the part stating "after a TCP/IP connection is made they
   continue separately in a separate socket and making server to listen for 
   other client requests." What does that mean? If one client is communicating
   to the server, how come other can communicate at the same time to same server.

   Isn't it like while a student is asking question to a teacher, other can't
   ask at the same time since that particular student is busy or occupying the
   teacher at the moment so others should wait, which we compare than client B 
   should wait when client A is communicating.

These are my basics questions that I'm not getting. Please correct me if I'm getting it all wrong. You can suggest me some books/pdfs to read if the answers are detailed or not partiularly focused on specific part. Thanks

  • 1
    Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you could provide and accept your own answer. – Ron Maupin Aug 17 '17 at 4:18
7

Now, from googling what I got is, the server listens at port 80 for incoming requests.

Indeed, more specifically there is a special type of socket called a "listening" socket.

Normally a socket is associated with a combination of local IP, local port, remote IP and remote port.

A listening socket is different. It is not associated with any specific remote IP and port. It is associated with a specific local port. It may or may not be associated with a specific local IP.

Normally your web server will have a listening socket with a local port of 80

Then lets say a client A tries to connect to the server (make TCP/IP connection). During which a socket is created between these two.

A pair of sockets actually one on the client, one on the server.

The client application creates a socket and asks the client OS to connect it to the server.

The client OS allocates a random local port, chooses a local IP (normally based on which interface the packet will be sent out on) and fills in the remote IP and port requested by the client application. Then it starts the process of connecting to the server.

The server OS notifies the holder of the listening socket that a new connection is coming in. The server application accepts the connection and a new socket is created to handle it.

Multiple threads or even processes may monitor the same listing socket. The OS will ensure that exactly one of them gets to accept a given connection.

Then it is executed as separate thread for its further communication

That is up to the implementer of the server application. They may choose to use multiple threads or they may choose to use an API like "select" or "poll" that allows a single thread to monitor multiple sockets for activity.

  1. How does server communicate with these two clients simultaneously
    after the connection has been established?

The server OS will match the packets against sockets by the combination of source IP, source port, destination IP and destination port and deliver them to the appropriate socket.

  1. Now, how do those thousands of clients get connected to a single server?

    If we assume every client is connected to the server through wire, it is not practically possible to maintain that many sockets on a hardware for connection. How those thousands connections are made and handled?

"Socket" in this context does not refer to a physical socket, just to a data structure within the operating system.

Still there are limits, thousands is easilly doable on a modern server, millions gets difficult.

Isn't it like while a student is asking question to a teacher, other can't ask at the same time since that particular student is busy or occupying the teacher at the moment so others should wait, which we compare than client B should wait when client A is communicating.

Computers are much better at splitting their attention than people are. Obviously if the server has only one processor core it can only be doing one thing at a time but if it can switch between things quickly enough the clients won't notice that.

And of course many servers nowadays do have multiple processor cores.


A new connection, not socket, gets created. I think you are extending the OP's confusion about what a socket is.

It seems the problem is that the rfc differs from actual practice in it's definition of socket.

I just went and looked up the documentation for "accept" for three major operating systems, all of them talk about accept creating a new socket.

http://man7.org/linux/man-pages/man2/accept.2.html

https://www.freebsd.org/cgi/man.cgi?query=accept&sektion=2

https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526%28v=vs.85%29.aspx

| improve this answer | |
  • For the answer of 1. I know about the socket uniqueness. What I meant to ask is if there are 2 unique sockets and server knows they are different. Then does server give each socket a time slice to run by handling the request at same time? Like if i connect to google and is processing my request it doesnot mean other million users request are in pending? – Sagaryal Mar 8 '17 at 7:54
  • Also 2. I meant the ports like actual physical one when you want to connect to a network, like in LAN. I might be misinterpreting the words here. Lets say I connect to google again. And like millions of other people are connecting to that single google server. I connect to google wirelessly from my home and similarly those million people. How are these connections handled by the server. Like for my computer to use internet i need to connect to a port on my router or wirelessly. Do they have millions of such ports physically? Now i think its not about server but wireless connection is it? – Sagaryal Mar 8 '17 at 8:13
  • "Normally a socket is associated with a combination of local IP, local port, remote IP and remote port." A socket, by definition of the RFC is the local IP address and TCP port, and it has nothing to do with the remote addresses. A connection is the combination of the local and remote sockets, so the local IP address, local TCP port, remote IP address, and remote TCP port. You have confused the issue. – Ron Maupin Mar 8 '17 at 21:25
  • 2
    "The server OS notifies the holder of the listening socket that a new connection is coming in. The server application accepts the connection and a new socket is created to handle it." A new connection, not socket, gets created. I think you are extending the OP's confusion about what a socket is. – Ron Maupin Mar 8 '17 at 21:27
  • "The server OS will match the packets against sockets by the combination of source IP, source port, destination IP and destination port and deliver them to the appropriate socket." Again, you are confusing socket with connection. – Ron Maupin Mar 8 '17 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.