Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listen() ignoring backlog value

As I understand, backlog determines the size of the connection queue. Any extra requests greater this size at that time will be dropped off(is this statment right??).

Now I have very simple program server.c

socket()
bind()
listen(..., 5)
while(1)
{
  accept()
  read()
  write()
  sleep(3)
  close()
}

Now, I start 8 clients at a time to connect to this server. Surprisingly, the server serves all the 8 clients but instead it should queue only 5 clients & remaining 3 clients requests should be refused. Another interesting point is even if I put this backlog value as 0, the result is still same. Then I tried commenting listen() call, with this all 8 clients connections get refused.

Can somebody provide any inputs on this.

like image 317
user1409528 Avatar asked May 25 '12 06:05

user1409528


People also ask

What does listen () do in TCP?

The listen() function applies only to stream sockets. It indicates a readiness to accept client connection requests, and creates a connection request queue of length backlog to queue incoming connection requests. Once full, additional connection requests are rejected.

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.

What is TCP connection backlog?

TCP listen() BacklogThe backlog has an effect on the maximum rate at which a server can accept new TCP connections on a socket. The rate is a function of both the backlog value and the time that connections stay on the queue of partially open connections.

What is a backlog queue?

1. The length of requests/packets/bits that are waiting in a queue for service.


1 Answers

The backlog argument is a hint about the size of the queue. So you can't count on it to do what you are asking.

listen()

This answer seems to cover it.

And more information, a quote from the listen(2) man page on my Ubuntu system:

The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.

Note that it says "may" everywhere.

like image 96
Francis Upton IV Avatar answered Oct 30 '22 11:10

Francis Upton IV