Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listen() queue length in socket-programing in c?

I have written two pair of codes(server.c and client.c) in Linux. One for UNIX-domain AF_UNIX other for INTERNET-domain AF_INET. Both are working fine!

listen() is called for backlog queue length = 3 in both servers

listen(sockfd, 3);  

In UNIX domain (AF_UNIX): While one client is connected with server, If I try to connect more clients to server. Three are kept in queue, and request of fourth is declined. (as I desired - 3 in waiting queue).

In INTERNET domain (AF_INET): Request of more than three are kept in a pending queue.

Why isn't a request from a fourth client rejected, even when the backlog queue length is three? And why is the behavior of listen() (and others) protocol dependent?

like image 236
Grijesh Chauhan Avatar asked Oct 15 '12 10:10

Grijesh Chauhan


People also ask

What does listen () do in C?

The listen() call indicates a readiness to accept client connection requests. It transforms an active socket into a passive socket. Once called, socket can never be used as an active socket to initiate connection requests. Calling listen() is the third of four steps that a server performs to accept a connection.

What is socket () bind () listen () accept () and connect ()?

The steps involved in establishing a TCP socket on the server side are as follows: Create a socket with the socket() function; Bind the socket to an address using the bind() function; Listen for connections with the listen() function; Accept a connection with the accept() function system call.

How the Listen function maintains the request in queue?

If a connection request arrives before the server can process it, the request is queued until the server is ready. When you call listen, you inform TCP/IP that you intend to be a server and accept incoming requests from the IP network. By doing so, socket status is changed from active status to passive.

How does a listening socket work?

The server socket listens for incoming connections. A server creates a socket, binds the socket to an IP address and port number (for TCP and UDP), and then listens for incoming connections. When a client connects to the server, a new socket is created for communication with the client (TCP only).


2 Answers

Operating systems actually use larger queues for incoming TCP connections than the one specified to listen(). How much larger depends on the operating system.

 listen(int socket_fd, int backlog)  

For a given listening socket kernal maintains two queue.

  1. An incomplete connection queue - for which SYN has been come but three-way handshaking (TCP) is not done completely. (SYN_RCV state)
  2. A complete connection queue - Three-way handshaking done. (ESTABLISHED state)

backlog argument historically specify sum of both queues. But there is no formal definition of what backlog means.

Berkeley-derived implementation add a fudge factor to the backlog. So total queue length = factor * backlog.

A very detailed and deep explanation given in a book by W. Richard Stevens. Also a table showing the values for seven operating systems can be found in Stevens, Fenner, Rudoff, "Unix Network Programming: The Sockets Network API", Volume 1, Third Edition, Page 108.

like image 95
Seg Fault Avatar answered Oct 13 '22 22:10

Seg Fault


The platform is entitled to adjust the specified backlog up or down, according to its minimum and its default. These days the default is more like 500 than five, which is where it started in about 1983. You can't rely on it being what you specified, and there is no API for finding out what it really is, and there is no apparent valid application reason for wanting it to be shorter than the default.

like image 20
user207421 Avatar answered Oct 14 '22 00:10

user207421