What is "backlog" in TCP connections?
Question: Below, you see a python program that acts as a server listening for connection
requests to port _9999_ :

    
    
    # server.py 
    import socket                                         
    import time
    
    # create a socket object
    serversocket = socket.socket(
                socket.AF_INET, socket.SOCK_STREAM) 
    
    # get local machine name
    host = socket.gethostname()                           
    
    port = 9999                                           
    
    # bind to the port
    serversocket.bind((host, port))                                  
    
    # queue up to 5 requests
    serversocket.listen(5)                                           
    
    while True:
        # establish a connection
        clientsocket,addr = serversocket.accept()      
    
        print("Got a connection from %s" % str(addr))
        currentTime = time.ctime(time.time()) + "\r\n"
        clientsocket.send(currentTime.encode('ascii'))
        clientsocket.close()
    

The questions is what is the function of the parameter of `socket.listen()`
method (i.e. `5`).

Based on the tutorials around the internet:

> The backlog argument specifies the maximum number of queued connections and
> should be at least 0; the maximum value is system-dependent (usually 5), the
> minimum value is forced to 0.

But:

  1. What is these _queued connections_? 
  2. Does it make any change for clients requests? (I mean is the server that is running with `socket.listen(5)` different from the server that is running with `socket.listen(1)` in accepting connection requests or in receiving data?)
  3. Why the minimum value is zero? Shouldn't it be at least `1`?
  4. Which value is preferred?
  5. Is this `backlog` defined for TCP connections only or we have it for UDP and other protocols too?


Answer: **NOTE : Answers are framed without having any background in Python, but, the
questions are irrelevant to language, to be answered.**

> What is these queued connections?

In simple words, **_BACKLOG_** is equal to how many pending connections the
queue will hold.

When multiple clients would like to connect to the server, then server holds
the incoming requests in a queue. The clients are arranged in a queue, and the
server processes their requests one by one as and when queue-member proceeds.
This nature of connection is called queued connection.

> Does it make any change for clients requests? (I mean is the server that is
> running with socket.listen(5) different from the server that is running with
> socket.listen(1) in accepting connection requests or in receiving data?)

Yes, both cases are different. The first case would allow only 5 clients to be
arranged to the queue; whereas in the case of backlog=1, only 1 connection can
be hold in the queue, thereby resulting in the dropping of the further
connection request!

> Why the minimum value is zero? Shouldn't it be at least 1?

I have no idea about Python, but, [as per this
source](http://pubs.opengroup.org/onlinepubs/009695399/functions/listen.html),
in C, a backlog argument of 0 may allow the socket to accept connections, in
which case the length of the listen queue may be set to an implementation-
defined minimum value.

> Which value is preferred?

This question has no well-defined answer. I'd say this depends on the nature
of your application, as well as the hardware configurations and software
configuration too. Again, as per the source, `BackLog` is silently limited to
between 1 and 5, inclusive(again as per C).

> Is this backlog defined for TCP connections only or we have it for UDP and
> other protocols too?

NO. Please note that there's no need to listen() or accept() for unconnected
datagram sockets(UDP). This is one of the perks of using unconnected datagram
sockets!

But, do keep in mind, then there are `TCP based datagram socket
implementations`(called TCPDatagramSocket) too which have backlog parameter.

