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.


In reply to Re: TCP server: How to reject connections when busy? by zwon
in thread TCP server: How to reject connections when busy? by squirrel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.