7

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.

user1409528
  • 123
  • 2
  • 7
  • You should put `read`, `write` and `close` in a concurrent environment. In your code, when the next connection is `accept`ed it seems the previous one has already been closed. Put the code in a thread and make sure each connection last sufficient long ensuring the situation 8 clients requesting your server simultaneously really occur. – Summer_More_More_Tea May 25 '12 at 06:47
  • i was testing **backlog** sometime back and didn't get anywhere. hope to see answers now. – tuxuday May 25 '12 at 06:48
  • OP, can you post your sample code. so that people can copy/paste and test it. – tuxuday May 25 '12 at 06:49
  • @ Summer_More_More_Tea The previous connection couldn't be closed immediately as a sleep of 3 seconds is present. Here all the 8 requests comes before 3 secs, so requests are already exceeding pending queue(5). – user1409528 May 25 '12 at 07:23
  • Possible duplicate of [listen() ignores the backlog argument?](http://stackoverflow.com/questions/5111040/listen-ignores-the-backlog-argument) – ks1322 Oct 10 '16 at 10:41

1 Answers1

4

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.

Community
  • 1
  • 1
Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57