23

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 can post and accept your own answer.
    – Ron Maupin
    Commented Dec 20, 2020 at 1:45

2 Answers 2

10

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

7
  • 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
    Commented Mar 8, 2017 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
    Commented Mar 8, 2017 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
    Commented Mar 8, 2017 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
    Commented Mar 8, 2017 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
    Commented Mar 8, 2017 at 21:29
0

The questions can be answered with the actual network programming commands the network software executes:

1.) Network server calls socket to create a socket, gets a file descriptor (fd) for the socket

2.) Network server calls bind passing the fd from step 1 and the ip address:port info (this should be the ip address:port where the network server will listen to) to bind the fd to

3.) Network server calls listen passing fd from step 1 and the maximum number of client connection requests to queue (client connection requests that are not yet accepted). This command just set this fd to listen mode and this command does not block which means execution proceeds right away

4.) From here it is up to the network server programming whether to service 1 client connection at a time or service more than 1 client connection at a time. Here the network server calls accept passing the fd from step 1. accept command accepts the first client connection request in the queue and if a client connection request is successfully accepted this command returns a new socket fd. This new fd is used to send (message), recv (receive message), and eventually close this connection. Generally there are two ways for a network server to service more than 1 client connection simultaneously. One is to create a new thread (multi-threading) for each accepted client connection. The other is to take advantage of the non-blocking mode of accept, send and recv commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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