listen() ignoring backlog value

5.9k views Asked by At

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.

1

There are 1 answers

3
Francis Upton IV On BEST ANSWER

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.