接收的方向有问题

This commit is contained in:
lsy2246 2024-04-20 18:42:47 +08:00
parent bc65338973
commit 4e1b559648
4 changed files with 14 additions and 97 deletions

View File

@ -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):

View File

@ -16,7 +16,6 @@ class ProcessServer:
except:
print("进程通信端口绑定失败")
def Process_client_link(self):
while True:
client_connect = self.Process_server_listener.accept()
@ -25,6 +24,7 @@ class ProcessServer:
def Process_client_recv(self, client_Thread_recv):
while True:
try:
data_json = client_Thread_recv.recv()
data = json.loads(data_json)
if data['target'] == 'Server':
@ -32,6 +32,8 @@ class ProcessServer:
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]

View File

@ -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'])

View File

@ -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