listen() queue length in socket-programing in c?
Solution 1
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.
- An incomplete connection queue - for which SYN has been come but three-way handshaking (TCP) is not done completely. (SYN_RCV state)
- 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.
Solution 2
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.
Related videos on Youtube
Grijesh Chauhan
I am a web backend developer, mostly write codes in Python, C and Go. I am a postgraduate in Computer Engineering I am a new contributor on Github and HackerRank. I also enjoy teaching and research work. Interesting Reads: Bit Twiddling Hacks Use reentrant functions for safer signal handling SARGable I always read some programming book. Top books in my rack are: Beginning Linux Programming, 4th Edition Python Cookbook, 3rd Edition Python-DataScience-Handbook Text Processing in Python by David Mertz, Amazon.com I think "IPython Cookbook" would be the next book to place on the stack!
Updated on September 15, 2022Comments
-
Grijesh Chauhan over 1 year
I have written two pair of codes(
server.c
andclient.c
) in Linux. One for UNIX-domainAF_UNIX
other for INTERNET-domainAF_INET
. Both are working fine!listen()
is called for backlog queue length = 3 inboth 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?-
Grijesh Chauhan over 11 years@KarolyHorvath: I mean it should be rejected if I given waiting queue length. And I want to know about this.
-
aman.gupta over 11 yearsI think any API parameter that doesn't act the way we expect should bother us, because it implies we don't understand it.
-
-
Seg Fault over 11 yearsFor example on Linux 2.4.7 a
backlog
of 3 given tolisten()
results in up to 6 connections being queued. -
Grijesh Chauhan over 11 yearsEdited some more detail in you answer. Thanks!
-
adrianlzt about 9 yearsCentOS 6.6 2.6.32-504.el6.x86_64 The backlog queue is backlog parameter +1
-
user207421 almost 3 yearsSome people try to use it as a limit on the number of concurrent connections. It isn't.