Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by zwon (Abbot)
on Nov 30, 2011 at 15:53 UTC ( [id://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 127.0.0.1:7777 127.0.0.1:44394 SY +N_RECV - tcp 0 0 127.0.0.1:7777 127.0.0.1:44396 SY +N_RECV - tcp 0 0 127.0.0.1:44394 127.0.0.1:7777 ES +TABLISHED 4384/nc tcp 0 0 127.0.0.1:7777 127.0.0.1:44390 ES +TABLISHED 3992/perl tcp 0 0 127.0.0.1:44390 127.0.0.1:7777 ES +TABLISHED 4382/nc tcp 0 0 127.0.0.1:7777 127.0.0.1:44392 ES +TABLISHED - tcp 0 0 127.0.0.1:44396 127.0.0.1:7777 ES +TABLISHED 4385/nc tcp 0 0 127.0.0.1:44392 127.0.0.1:7777 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://940885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found