服务器和代理服务器之间的连接添加心跳机制

This commit is contained in:
ranchuan
2024-10-09 11:10:16 +08:00
parent 3e4d145920
commit 7cabaa0479
3 changed files with 45 additions and 17 deletions

View File

@@ -21,13 +21,13 @@ SERVER_PORT = 5345
# 定义个函数,使其专门重复处理客户的请求数据(也就是重复接受一个用户的消息并且重复回答,直到用户选择下线)
def dispose_client_request(tcp_client_1,tcp_addr):
def dispose_client_request(tcp_client,tcp_addr):
myprint(f"客户端:{tcp_addr} 已连接")
recv_data=bytearray()
target =tg.tcp_target(tcp_client_1)
target =tg.tcp_target(tcp_client)
while True:
try:
recv = tcp_client_1.recv(4096)
recv = tcp_client.recv(4096)
except Exception as e:
myprint(str(e))
break
@@ -44,7 +44,6 @@ def dispose_client_request(tcp_client_1,tcp_addr):
break
myprint(f"客户端:{tcp_addr} 已下线")
target.close()
tcp_client_1.close()
@@ -57,8 +56,8 @@ def start_service():
tcp_server.listen(128)
while True:
tcp_client_1 , tcp_client_address = tcp_server.accept()
thd = threading.Thread(target = dispose_client_request, args = (tcp_client_1,tcp_client_address))
tcp_client , tcp_client_address = tcp_server.accept()
thd = threading.Thread(target = dispose_client_request, args = (tcp_client,tcp_client_address))
# 设置守护主线程 即如果主线程结束了 那子线程中也都销毁了 防止主线程无法退出
# thd.setDaemon(True)
thd.start()