Below, you see a python program that acts as a server listening for connection requests to port 9999:
# server.py
import socket
NOTE : Answers are framed without having any background in Python, but, the questions are irrelevant to language, to be answered.
What are these queued connections?
In simple words, the backlog parameter specifies the number of pending connections the queue will hold.
When multiple clients connect to the server, the server then holds the incoming requests in a queue. The clients are arranged in the queue, and the server processes their requests one by one as and when queue-member proceeds. The nature of this kind of connection is called queued connection.
Does it make any difference for client requests? (I mean is the server that is running with
socket.listen(5)
different from the server that is running withsocket.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 is the minimum value zero? Shouldn't it be at least 1?
I have no idea about Python, but, as per this source, 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.
Is there a preferred value?
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 does it apply 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.