Hello. Wrote a simple client-server application using sockets. The client sends the string to the server, the server translates this string to upper case and sends it to the client, and the client simply prints this string.

#Сервер import socket from threading import Thread def handler(client_socket, cli_address): print(client_address, 'was connected') while 1: recieve_message = client_socket.recv(1024) client_socket.send(recieve_message.upper()) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('127.0.0.1', 5000)) print('server starts') server_socket.listen(1) while 1: client_socket, client_address = server_socket.accept() Thread(target=handler, args=(client_socket, client_address)).start() #----------------------------------------- #Клиент import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(('127.0.0.1', 5000)) while 1: message = input() client_socket.send(message.encode()) recieve_message = client_socket.recv(1024) print(recieve_message.decode()) 

The question calls the listen method. As a parameter, it takes the maximum number of connections. In this case, it equals 1. But having started the server and five clients, they all work safely, why?

    2 answers 2

    You do not quite understand this parameter in listen. How does the server socket work:

    • The client connects to the server.
    • it is queued (and this parameter at listen determines the size of the queue)
    • if there is no space in the queue, the client is refused connection.
    • separately the server itself calls accept for the socket. This way, he picks up one client from the queue of sockets waiting to be connected.

    Now the legitimate question, and how much to put the size of the queue? It should be such that the code has time to take all customers. That is, if you test and connect with one client and the next connection will be only after processing the previous one, even the size of 1 will be sufficient. If you have a heavily loaded server and customers are dropping there by the hundreds, and the code can have plug-ins according to clients' accept, then you need to install more, I have seen 100 and 150. But if the customers are dropping so fast that the code does not have time to rake them, then no queue size will help.

    • on Linux, even if the queue of ready-made tcp sockets is full (with the size defined by the backlog), new connections can be accepted while the queue for incomplete ( SYN RECEIVED ) connections (with the size /proc/sys/net/ipv4/tcp_max_syn_backlog , if syncookies is not enabled) will not fill up. - jfs

    From the documentation for socket.listen([backlog]) :

    Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections. If not specified, a default reasonable value is chosen. (my selection)

    The highlighted part says that the backlog parameter determines the number of uncommitted connections, after which new connections are rejected. What is an unaccepted connection and / or number of queues associated with new connections may depend on the system, for example, see man listen (2) and if you want details: How TCP backlog works in Linux .

    A “missed” connection — this is probably a connection for which the socket.accept() method was not called. Therefore, even with server_socket.listen(1) your server can maintain the simultaneous clients for as long as the number of running threads, if there is a pause between opening new connections so that the new stream starts and the next server_socket.accept() could return.