1

When listening on a socket, I would ideally like to limit the backlog to zero, i.e.

listen( socket, 0 );

However, based on the following post, listen() ignores the backlog argument?, this wouldn't work. Is there any way I can reliably achieve a backlog of 0?

Community
  • 1
  • 1
Matt Dunn
  • 5,106
  • 6
  • 31
  • 55
  • 2
    Duplicate? http://stackoverflow.com/questions/5111040/listen-ignores-the-backlog-argument – HyderA Feb 28 '11 at 17:35
  • 3
    But why backlog of 0? If your backlog is 0, you can not receive any connection, bacause every incomming connection must pend on the backlog before awakening the listening process... – lvella Feb 28 '11 at 20:03
  • what do you think you would achieve if you did managed to do this? – Len Holgate Feb 28 '11 at 21:41
  • 2
    To everyone asking why, it's like coming to a restaurant and upon ordering food hearing from the waiter how much salt, pepper and chili you want in your food, in grams and whether you want the Northern Atlantic trout or the one from Barentz Sea. If you need to answer those kind of question you might as well make your own food at home. Encapsulation, people -- a backlog isn't a universal property of the TCP/IP stack, it just happens to be a variable used in Linux. One simply wants to defer to somewhere else. – Armen Michaeli Sep 09 '21 at 11:28

1 Answers1

3

The closest you can get is to listen(), accept() and close() in one step. This should provide the same overall effect as a backlog of zero, except that you must re-create and bind the socket each time.

int accept_one(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
    int result;

    result = listen(sockfd, 1);

    if (result >= 0)
        result = accept(sockfd, addr, addrlen);

    close(sockfd);

    return result;
}

I am not sure why you would want this, though.

caf
  • 233,326
  • 40
  • 323
  • 462
  • 2
    If you close the `sockfd` you won't be able to `listen` to it again without starting the whole `bind()` process over. – Ariel Dec 07 '11 at 11:49