Python 为什么客户只有在发送邮件后才收到?
为什么客户只有在发送邮件后才收到?当我启动服务器并连接两个客户端时,它们应该能够相互发送消息。他们会这样做,但只有当他们当前已向服务器发送消息时才会这样做。不应该如此,因为发送和接收在不同的线程中 客户端:Python 为什么客户只有在发送邮件后才收到?,python,sockets,python-multithreading,Python,Sockets,Python Multithreading,为什么客户只有在发送邮件后才收到?当我启动服务器并连接两个客户端时,它们应该能够相互发送消息。他们会这样做,但只有当他们当前已向服务器发送消息时才会这样做。不应该如此,因为发送和接收在不同的线程中 客户端: import socket import threading HEADER = 64 PORT = 5050 FORMAT = 'utf-8' DISCONNECT_MESSAGE = "!DISCONNECT" SERVER = "my local IP&q
import socket
import threading
HEADER = 64
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = "my local IP"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
def events():
while True:
msg = input()
if msg == "quit":
send(DISCONNECT_MESSAGE)
break
else:
send(msg)
def recieve():
while True:
msg_length = client.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = client.recv(msg_length).decode(FORMAT)
print(msg)
thread1 = threading.Thread(target=events)
thread2 = threading.Thread(target=recieve)
thread1.start()
thread2.start()
服务器端:
import socket
import threading
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
messages_to_send = {}
client_pairs = []
new_client = False
def send(msg, conn):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
conn.send(send_length)
conn.send(message)
def handle_client(conn, addr):
global new_client
if new_client:
print(f"[NEW CONNECTION] {addr} connected to {new_client}")
client_pairs.append((new_client, addr))
new_client = False
else:
print(f"[NEW CONNECTION] {addr} waiting")
new_client = addr
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
print(f"[DISCONNECTED] {addr}")
connected = False
if new_client == addr:
new_client = False
else:
for pair in client_pairs:
if addr == pair[0]:
if new_client:
client_pairs.append((new_client, addr))
new_client = False
else:
new_client = pair[1]
break
elif addr == pair[1]:
if new_client:
client_pairs.append((new_client, addr))
new_client = False
else:
new_client = pair[0]
break
else:
for pair in client_pairs:
if addr == pair[0]:
messages_to_send[pair[1]] = msg
elif addr == pair[1]:
messages_to_send[pair[0]] = msg
print(f"[{addr}] {msg}")
if addr in messages_to_send.keys():
send(messages_to_send[addr], conn)
del messages_to_send[addr]
conn.close()
def start():
server.listen()
print(f"[LISTENING] Server is listening on {SERVER}")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"\n[ACTIVE CONNECTIONS] {threading.activeCount()-1}")
print("[STARTING] Server is starting...")
start()