Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has server opened socket for each client process?

I am confused regarding the sockets. As far as I know socket is combination of ip address and port number. Its just programming abstraction to allow to write to, or read from a stream (in case of TCP). Now what I am not absolutely sure of, is whether the server has ONE or MORE sockets when serving the client? Lets say http at port 80.

Does all data from various clients got sent to one socket (server:80) and some UBER server process distinguishes them based on incoming address or are more sockets based on combination of client address and port number created by TCP layer?. Can someone describe this thoroughly with step-by-step algorithm (for multiple clients served at the same time) and not just with Server binds socket to port, Server listens to socket, Server serves data.

like image 994
ps-aux Avatar asked Sep 03 '13 13:09

ps-aux


People also ask

Can multiple clients connect to same server socket?

Supporting Multiple Clients However, multiple client requests can come into the same port and, consequently, into the same ServerSocket . Client connection requests are queued at the port, so the server must accept the connections sequentially.

Does each process have a socket?

yes , a process could have more that one socket (combination of ip address and port address) but at a particular instance of time it can be only one..

How many sockets can a server open?

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

How many clients can a server socket connect to?

Ports are 16-bit numbers, therefore the maximum number of connections any given client can have to any given host port is 64K.


1 Answers

You're confusing TCP connections with sockets. A socket is not a network-level concept. Is is an OS concept. A TCP connection exists on the network as a unique combination of (source-ip, source-port, dest-ip, dest-port). A socket is a handle to an open port or an open connection (this statement is slightly simplified). When I got started I also thought this was confusing and an OS design error (but it is what it is and we are stuck with it). The design error is that the allowed operation for each of the different sockets are very much different. These use cases should have been two independent concepts with different names and different APIs.

As you can see there is no 1:1 relationship between sockets and connections.

Can someone describe this thoroughly with step-by-step algorithm

A server opens a socket to let the OS know that it wants to listen or connect. Then, every accepted connection will result in a new, independent socket. Every new connection is on the same server-ip and server-port though. Just the client-ip and/or client-port are different. The server reads and writes on the per-connection sockets. The open-port socket is just for accepting new connections.

The server conceptually goes like this:

var openPortSocket = Open(80); //HTTP port
while(true) {
 var connectionSocket = openPortSocket.Accept();
 ServeConnectionOnNewThread(connectionSocket);
}

This is a logical model. The actual API calls are different. Most servers use async IO for example. That is not relevant to your question though.

Clients must use a different client port for each connection. This is exactly what your browser does.

like image 189
usr Avatar answered Sep 18 '22 13:09

usr