如何正确管理最大连接客户端数 - Python Client Server TCP
How to correctly manage a maximum number of connected clients - Python Client Server TCP
我正在研究在 Python 3.6 中开发的 tcp 服务器和 tcp 客户端应用程序。
建立连接后,服务器向客户端发送数据并从客户端本身接收数据。
服务器应接受最大数量的客户端。我想要的是,当达到最大连接客户端数时,服务器不接受任何其他连接,并通知客户端并中止。
这里是服务器代码:
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()
客户端代码如下:
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()
在达到最大连接客户端数之前,一切正常。
当启动“额外”客户端时,它不会(正确地)从服务器接收任何内容,但会开始尝试发送数据。未收到数据,因为服务器未接受连接。
我想要的是在启动新线程之前找到一种方法来了解服务器何时不接受客户端连接,以便正确管理这种情况。
您在实际检索客户端之前调用 client.close()
。这意味着最后一个被接受的客户仍然在客户变量中。此连接将被关闭,而不是新连接。
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
我正在研究在 Python 3.6 中开发的 tcp 服务器和 tcp 客户端应用程序。
建立连接后,服务器向客户端发送数据并从客户端本身接收数据。
服务器应接受最大数量的客户端。我想要的是,当达到最大连接客户端数时,服务器不接受任何其他连接,并通知客户端并中止。
这里是服务器代码:
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()
客户端代码如下:
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()
在达到最大连接客户端数之前,一切正常。
当启动“额外”客户端时,它不会(正确地)从服务器接收任何内容,但会开始尝试发送数据。未收到数据,因为服务器未接受连接。
我想要的是在启动新线程之前找到一种方法来了解服务器何时不接受客户端连接,以便正确管理这种情况。
您在实际检索客户端之前调用 client.close()
。这意味着最后一个被接受的客户仍然在客户变量中。此连接将被关闭,而不是新连接。
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