Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server vs Client Socket (Low level details)?

Tags:

sockets

General Programming: In a server socket accept() method what exactly happens. At low level how server sockets are different from client sockets?

like image 937
S M Kamran Avatar asked Apr 21 '09 20:04

S M Kamran


People also ask

What is the difference between server socket and client socket?

a server listens on a host and a port, receives requests (e.g. through a socket connection), and then sends a response to the client who initiated the socket connection. The client is what sends a request to that server socket, and waits for a response.

Is socket programming low level?

socket — Low-level networking interface.

What is socket and server socket?

One socket (node) listens on a particular port at an IP, while other socket reaches out to the other in order to form a connection. The server forms the listener socket while the client reaches out to the server. Socket and Server Socket classes are used for connection-oriented socket programming.

What is server socket explain in detail with an example?

Sockets are bound to the port numbers and when we run any server it just listens on the socket and waits for client requests. For example, tomcat server running on port 8080 waits for client requests and once it gets any client request, it responds to them.


2 Answers

At a low level sockets are just sockets regardless of whether they are being used in a server or client application. The difference between the two lies in the system calls each kind of application makes.

Server sockets will call bind() to be associated with a port. They want to be associated with a port so that other programs know where to reach them. Client sockets can call bind() but almost never do because there is not much point. If a socket doesn't call bind() the OS will just choose an ephemeral port for it, which is fine for clients because they are doing the calling; no one needs to call them.

Server sockets call listen(). This was explained pretty well in the other answers.

Server sockets call accept() and I think this is the crux of your question because it is a bit mysterious at first. The important thing to grasp is that in calling accept() the kernel will pass back a new socket. It is now separate from the original listening socket and is what your server will use to communicate with its peer(s).

The key in understanding how the listening socket continues to listen while the accepted connection is doing its thing is in understanding that tcp connections depend on a 4-tuple of (1) local address (2) local port (3) foreign address (4) foreign port. These define a unique connection. Before accept() passed back the new socket the kernel used these values to create various structures so that in collaboration with the tcp/ip stack all traffic with this tuple will go to the connected socket. Even though your server may have a thousand connections with local address 192.168.1.100 port 80, the client combination of address and port will always be different and thus the tuple is always unique.

like image 107
Duck Avatar answered Mar 03 '23 12:03

Duck


Firstly, server sockets are generally bound to well known names (ports, in this case) and they establish themselves with listen(). That is where the real difference happens, as client sockets establish themselves with connect(). Calling listen() on a socket causes the kernel's tcp/ip implementation to begin accepting connections sent to the socket's bound name (port). This will happen whether or not you ever call accept().

accept() simply gives your server a way to access and interact with the client sockets that have connected to your listening socket.

like image 21
Jason Coco Avatar answered Mar 03 '23 11:03

Jason Coco