简体   繁体   English

如何正确管理最大连接客户端数 - Python Client Server TCP

[英]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.我正在使用 tcp 服务器和 tcp 客户端应用程序,这些应用程序是在 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.我想要的是在启动新线程之前找到一种方法来了解服务器何时不接受客户端连接,以便正确管理这种情况。

You're calling client.close() before actually retrieving the client.您在实际检索客户端之前调用client.close() 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python freeopcua 服务器:如何知道连接的客户端数量? - Python freeopcua server: how to know number of connected clients? 如何检查连接到C TCP服务器套接字的python TCP客户端套接字的状态? - How to check the state of python TCP client socket connected to C TCP server socket? 我如何知道连接到服务器的客户端数量,并将连接的客户端数量返回给用户? - How can I know the number of clients connected to the server and return the number of the connected clients to the user? 如何获取python tcp服务器/客户端以允许多个客户端同时在ant - How to get python tcp server/client to allow multiple clients on ant the same time Python TCP Server,正在写入客户端? - Python TCP Server, writing to clients? 带套接字的TCP客户端/服务器,服务器向客户端发送文件,客户端挂起,Python - TCP client/server with sockets, server sending files to clients, client hangs, Python Python中的最大TCP连接数? - Maximum number of TCP connections in Python? 如何使用Python epoll获取服务器中已连接客户端的列表 - How to get a list of connected clients in a server using Python epoll 如何在TCP Python聊天服务器上拥有多个客户端? - How can I have multiple clients on a TCP Python Chat Server? 如何远程连接python tcp客户端和服务器? - How to remotely connect python tcp client and server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM