Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: TCP server: How to reject connections when busy?

by zwon (Abbot)
on Nov 30, 2011 at 15:53 UTC ( #940885=note: print w/replies, xml ) Need Help??

in reply to TCP server: How to reject connections when busy?

Backlog is the maximum number of established connections (i.e. handshake was completed) that may be placed in accept(2) queue. On BSD (and Linux) value 0 is actually means 1, so here you already have a problem, there's always one place in the queue. But even if all places in queue are taken TCP continues accepting connection requests, it doesn't reject them. Consider following example server:

use 5.010; use strict; use warnings; use Socket; socket my $sock, PF_INET, SOCK_STREAM, getprotobyname("tcp"); bind $sock, sockaddr_in(7777, INADDR_ANY); listen $sock, 0; while (accept my $cli, $sock) { sleep 60; }

Now, if I try to establish four simultaneous connections to this server using netcat from different terminals:

nc localhost 7777

all this connections will be accepted. Here's what netstat shows me:

$ sudo netstat -tanp | grep -v LISTEN | grep 7777 tcp 0 0 SY +N_RECV - tcp 0 0 SY +N_RECV - tcp 0 0 ES +TABLISHED 4384/nc tcp 0 0 ES +TABLISHED 3992/perl tcp 0 0 ES +TABLISHED 4382/nc tcp 0 0 ES +TABLISHED - tcp 0 0 ES +TABLISHED 4385/nc tcp 0 0 ES +TABLISHED 4383/nc

As you can see on the client side all connections are in ESTABLISHED state. From the server side one connection (from port 44390) was ESTABLISED and accepted, another connection (from port 44392) ESTABLISHED but not yet accepted, it is in accept queue, and two more connections are in SYN_RECV state, that means that server sent SYN-ACK to client, but ignoring ACKs from client till there will be a place in the accept queue (just wait a minute).

PS: ISBN 0201633469 is highly recommended.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://940885]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2018-04-25 18:48 GMT
Find Nodes?
    Voting Booth?