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:
socket.listen(5)
different from the server that is running with socket.listen(1)
in accepting connection requests or in receiving data?)1
?backlog
defined for TCP connections only or does it apply for UDP and other protocols too?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.