1

I want to programmatically find the size of the socket waiting queue of my computer i.e. the number of socket that can be put in the queue before being refused (while the server is listening for clients).

On the server side, I simply create a socket, bind it, listen and enter in an infinite loop in order to wait client sockets.
Then I thought that this little script would help me to find the queue size:

pids=""
for i in {1..8}; do
     netcat localhost -w 3 19677 &
     pids+=" $!"
done

for p in $pids; do
        if wait $p; then
                echo "Process $p success"
       else
                echo "Process $p fail"
        fi
done

If there is more than 4 rounds in the loop then I always get a timeout exception,(and never for 4 or less) but increasing the number of rounds doesn't always lead to more errors (I don't always get 2 errors for 6 rounds, 3 for 7, etc.). Why?

Moreover, if I launch netcat directly in 5 terminals (netcat localhost -w 5 19677), I don't get any error.

What am I missing?

Simon
  • 6,025
  • 7
  • 46
  • 98
  • `listen` has an argument, do you know what it means? – n. m. could be an AI Jun 06 '16 at 08:35
  • I presume that you're talking about the backlog argument. I set this argument at 1, but as it is explained here: http://stackoverflow.com/questions/5111040/listen-ignores-the-backlog-argument it is only advisory. (If I have well understood obviously.) – Simon Jun 06 '16 at 08:44
  • If you are not talking about the backlog, then what? – n. m. could be an AI Jun 06 '16 at 08:49
  • Hum, I'm sorry but I'm not sure to understand your question. I set a backlog of 1, but I still can accept more than one connection (at least 4) in the queue. Unless my script is badly testing it (likely). – Simon Jun 06 '16 at 08:53
  • 1
    You set the backlog to *at least* one, and can establish *at least* one connection. No problem here. – n. m. could be an AI Jun 06 '16 at 09:12
  • Ok, it is a minimum, I now understand better. But what I would like to do is find the maximum, how can I do that? – Simon Jun 06 '16 at 09:16
  • I don't think a reliable maximum exists. – n. m. could be an AI Jun 06 '16 at 09:19
  • According to the EJP response it does not seem that the backlog argument is a minimum since it can be adjusted down by the platform. Am I missing something? – Simon Jun 06 '16 at 09:28
  • I have never seen it adjusted down, but perhaps this can happen. Then you cannot obtain any reliable information about the queue size. Why do you need it in the first place? – n. m. could be an AI Jun 06 '16 at 09:30
  • @n.m. Try setting it to 0x7fffffff and you will see it adjusted down all right, although the only way to discover it would be by peeking the kernel somehow. – user207421 Jun 06 '16 at 09:32
  • @EJP well the man page does say it will be truncated to `/proc/sys/net/core/somaxconn`, I mean anything beyond that. – n. m. could be an AI Jun 06 '16 at 09:44
  • @n.m. I don't know what 'anything beyond that' means. It will certainly be truncated to the value given in that location. What makes you think there is any other limit? – user207421 Jun 06 '16 at 10:17
  • @EJP I mean it will be truncated to /proc/sys/net/core/somaxconn as per the man page, but I haven't seen it truncated more than that. – n. m. could be an AI Jun 06 '16 at 10:23
  • @n.m. I am unable to understand your point. Did somebody say it would be truncated more than that? – user207421 Jun 06 '16 at 10:25

1 Answers1

0

There is no API for this. You could remember the argument you supplied to listen(), but as the platform can adjust it either up or down it won't do you much good.

What happens when the backlog is exceeded is platform-dependent. Windows will issue connection refusals; other platforms will do nothing, which will lead to retries and possible connection timeouts.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • That's why I would like to test how many connections can get on the waiting queue. Why the way I tested it don't always show the same results on the same platform? – Simon Jun 06 '16 at 09:27
  • I'm running the script on Debian and with a timeout so why don't I always get the same number of connection timeout with the same number of connection tries? – Simon Jun 06 '16 at 09:35
  • What happens in the server's infinite loop? – user207421 Jun 06 '16 at 09:39
  • Nothing, just a `for(;;){ }` in order to avoid the end of the program. – Simon Jun 06 '16 at 09:41