From 4e1b559648efc6b2e494906c99451937bdf48918 Mon Sep 17 00:00:00 2001 From: lsy2246 Date: Sat, 20 Apr 2024 18:42:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E6=94=B6=E7=9A=84=E6=96=B9=E5=90=91?= =?UTF-8?q?=E6=9C=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Client/Transmission/Process_Client.py | 4 +- .../Client/Transmission/Process_Server.py | 18 ++-- .../Client/Transmission/Session_server.py | 85 ------------------- .../Server/Transmission/Session_client.py | 4 +- 4 files changed, 14 insertions(+), 97 deletions(-) delete mode 100644 python/test/Client/Transmission/Session_server.py diff --git a/python/test/Client/Transmission/Process_Client.py b/python/test/Client/Transmission/Process_Client.py index 9145001..fda809d 100644 --- a/python/test/Client/Transmission/Process_Client.py +++ b/python/test/Client/Transmission/Process_Client.py @@ -22,8 +22,8 @@ class ProcessClient: data_json = self.Process_client_Client.recv() data = json.loads(data_json) self.Process_client_pick(data) - except EOFError: - print("连接已关闭") + except EOFError as e: + print("连接已关闭"+e) break def Process_client_pick(self, data): diff --git a/python/test/Client/Transmission/Process_Server.py b/python/test/Client/Transmission/Process_Server.py index a2609da..9ef2dd9 100644 --- a/python/test/Client/Transmission/Process_Server.py +++ b/python/test/Client/Transmission/Process_Server.py @@ -16,7 +16,6 @@ class ProcessServer: except: print("进程通信端口绑定失败") - def Process_client_link(self): while True: client_connect = self.Process_server_listener.accept() @@ -25,13 +24,16 @@ class ProcessServer: def Process_client_recv(self, client_Thread_recv): while True: - data_json = client_Thread_recv.recv() - data = json.loads(data_json) - if data['target'] == 'Server': - if data['function'] == 'Name': - self.Process_client_link_dick[data['content']] = client_Thread_recv - else: - self.Process_client_pick(data) + try: + data_json = client_Thread_recv.recv() + data = json.loads(data_json) + if data['target'] == 'Server': + if data['function'] == 'Name': + self.Process_client_link_dick[data['content']] = client_Thread_recv + else: + self.Process_client_pick(data) + except Exception as e: + print("进程关闭" + e) def Process_client_send(self, target, function, content): connect = self.Process_client_link_dick[target] diff --git a/python/test/Client/Transmission/Session_server.py b/python/test/Client/Transmission/Session_server.py deleted file mode 100644 index ff833f0..0000000 --- a/python/test/Client/Transmission/Session_server.py +++ /dev/null @@ -1,85 +0,0 @@ -import multiprocessing -import os -import time -import socket -import json -import threading -from .Process_Client import ProcessClient - - -class Session_server(ProcessClient): - def __init__(self): - ProcessClient.__init__(self) - self.socker_ip = "127.0.0.1" - self.socker_port = 7868 - self.server_socket = None - self.server_status = False # 服务器状态 - self.link_server_Thread = threading.Thread(target=self.link_server) - self.link_server_Thread.start() - self.receive_server_Thread = threading.Thread(target=self.receive_server) - - current_file_path = __file__ - current_file_name = os.path.basename(current_file_path).split('.')[0] - self.Process_client_send("Server", "Name", current_file_name) - - def link_server(self): - while True: - if not self.server_status: - time.sleep(1) - if self.server_socket is not None: - self.server_socket = None - try: - self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.server_socket.connect((self.socker_ip, self.socker_port)) - self.server_status = True - if not self.receive_server_Thread.is_alive(): - self.receive_server_Thread.start() - except Exception as a: - self.server_status = False - print("连接错误:" + str(a)) - finally: - self.Process_client_send("ALL", "server_status", self.server_status) - pass - - def receive_server(self): - while self.server_status: - try: - receive_content_json = self.server_socket.recv(1024).decode('utf-8') - receive_content = json.loads(receive_content_json) - self.content_pick(receive_content) - except Exception as a: - print("客户端接收错误:" + str(a)) - self.server_status = False - self.Process_client_send("ALL", "server_status", self.server_status) - - def send_server(self, genre, target, content): - if self.server_status: - try: - data = {"genre": genre, "target": target, "data": content, - "datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} - data_json = json.dumps(data) + '\n' - self.server_socket.send(data_json.encode("utf-8")) - except Exception as a: - print("发送错误:" + str(a)) - self.server_status = False - self.Process_client_send("ALL", "server_status", self.server_status) - - def Process_client_pick(self, data): - if data['target'] in ['ALL', 'Session_server']: - match data['function']: - case 'server_status': - self.server_status = data['content'] - case 'send_server': - self.send_server(data['content']['genre'], data['content']['target'], data['content']['content']) - - def content_pick(self, data): - match data['genre']: - case '注册' | '登录': - self.Process_client_send("Login", "login_page_receive", data) - case '数据更新': - self.Process_client_send("File_operate", "save_data", data['data']) - case '聊天记录': - if data['data']['Type'] == 'text': - self.Process_client_send("File_operate", "save_data", {"History": data['data']}) - if data['target'] == '接收': - self.Process_client_send("Chat_main", "Chat_screen_show", data['data']) diff --git a/python/test/Server/Transmission/Session_client.py b/python/test/Server/Transmission/Session_client.py index 21e9ede..ed44d5e 100644 --- a/python/test/Server/Transmission/Session_client.py +++ b/python/test/Server/Transmission/Session_client.py @@ -11,7 +11,7 @@ from .Process_Client import * class link_client(ProcessClient): def __init__(self): ProcessClient.__init__(self) - self.server_host = "127.0.0.1" + self.server_host = "192.168.137.1" self.server_port = 7868 self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.bind((self.server_host, self.server_port)) @@ -33,7 +33,7 @@ class link_client(ProcessClient): self.client_max_id += 1 client_id = ''.join(random.choice(string.ascii_letters) for _ in range(10)) + str(self.client_max_id) self.client_socket_dict[client_id] = client_socket - recv_client_Thread = threading.Thread(target=self.recv_client(client_socket), daemon=True) + recv_client_Thread = threading.Thread(target=self.recv_client, args=(client_socket,), daemon=True) recv_client_Thread.start() except: pass