[英]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.