Search code examples
pythonpython-3.xsocketstcptcpclient

How to correctly manage a maximum number of connected clients - Python Client Server TCP


I'm working on a tcp server and a tcp client application developed in Python 3.6.
Once connection has been established, the server sends data to the client and receive data from the client itself.

The server should accept a maximum number of clients. What i'd like is that when the maximum number of connected clients is reached, the server does not accept any other connections and the client is notified and aborted.

Here the server code:

class ThreadedServer(object):

    def __init__(self, host, port, max_clients):
        self.host = host
        self.port = port
        self.max_clients = max_clients
        self.connected_clients = 0
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((self.host, self.port))

    def listen(self):
        self.sock.listen(5)
        
        while True:
        
            if self.connected_clients >= self.max_clients:
                print("Maximum number of clients reached")
                continue
        
            client, address = self.sock.accept()
        
            # keep track connected clients
            self.connected_clients += 1

            # start a new thread to send data to the connected client
            # start a new thread to receive data to the connected client


if __name__ == "__main__":
    HOST = "xxx.xxx.xxx.xxx"
    PORT = xxxx
    MAX_CLIENTS = x

    ThreadedServer(HOST, PORT, MAX_CLIENTS).listen()

The client code is the following:

class ThreadedClient(object):

    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def send(self):
        self.sock.connect((self.host, self.port))
        
        # start a new thread to receive data to the server
        # start a new thread to send data to the server

if __name__ == "__main__":
    HOST = "xxx.xxx.xxx.xxx"
    PORT = xxxx

    ThreadedClient(HOST, PORT).send()

Everything works fine until the maximum number of connected clients is reached.
When an "extra" client is launched, it does not (correctly) receive anything from the server but it starts to try to send data. Data are not received because the server did not accept the connection.

What I'd like is find a way to understand when the server did not accept the client connection before starting new threads in order to manage this scenario correctly.


Solution

  • You're calling client.close() before actually retrieving the client. This will mean that the last client that was accepted will still be in the client variable. This connection will be closed, not the new one.

    def listen(self):
        self.sock.listen(5)
        
        while True:
        
            client, address = self.sock.accept() # this line needs to be before the if
    
            if self.connected_clients >= self.max_clients:
                print("Maximum number of clients reached")
                client.close()
                continue
        
            # keep track connected clients
            self.connected_clients += 1
    
            # start a new thread to send data to the connected client
            # start a new thread to receive data to the connected client