listen() ignoring backlog value
Asked Answered
U

1

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.

Underage answered 25/5, 2012 at 6:36 Comment(5)
You should put read, write and close in a concurrent environment. In your code, when the next connection is accepted 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.Easley
i was testing backlog sometime back and didn't get anywhere. hope to see answers now.Himself
OP, can you post your sample code. so that people can copy/paste and test it.Himself
@ 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).Underage
Possible duplicate of listen() ignores the backlog argument?Serum
J
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.

Jeuz answered 25/5, 2012 at 6:40 Comment(3)
In my system SOMAXCONN is set to 128. So what would be the behaviour if I start more than 128 clients at a time.Underage
Yes, the specification says that values up to 128 shall be supported, but there is no obligation to actually limit the outstanding connections. And I'm assuming this specification is similar to what you are using.Jeuz
See the revision of my answer.Jeuz

© 2022 - 2024 — McMap. All rights reserved.