简体   繁体   English

服务器仅打印来自最近连接的客户端的消息

[英]Server only prints messages from the most recently connected client

I'm trying to create a server that accepts multiple clients. 我正在尝试创建一个接受多个客户端的服务器。 The clients can send messages to the server, and the server prints these messages. 客户端可以将消息发送到服务器,服务器将打印这些消息。 My code below produces a weird result where only the most recent connection has its messages accepted and printed by the server. 我的下面的代码产生了一个奇怪的结果,其中只有最新的连接的消息才被服务器接受并打印。

server.py server.py

import socket
import _thread

HOST = "192.168.1.84"
PORT = 5000


def client_thread(conn):
    welcome_message = "Connected to server " + HOST + ":" + str(PORT) + "\n"
    conn.send(welcome_message.encode("ascii"))
    while True:
        data = connection.recv(2048)
        message = data.decode("ascii")
        if not data:
            break
        print("(" + address[0] + ":" + str(address[1]) + ") " + message)
    connection.close()


with socket.socket() as server_socket:
    try:
        server_socket.bind((HOST, PORT))
        server_socket.listen(10)
        print("Server hosted on " + HOST + ":" + str(PORT) + "\n")
        while True:
            connection, address = server_socket.accept()
            _thread.start_new_thread(client_thread, (connection,))
            print("Connection from " + address[0] + ":" + str(address[1]))

    except socket.error as error_message:
        print("Error: " + str(error_message))

client.py client.py

import socket

HOST = "192.168.1.84"
PORT = 5000

with socket.socket() as client_socket:
    try:
        client_socket.connect((HOST, PORT))
        data = client_socket.recv(2048)
        print(data.decode("ascii"))
        while True:
            message = input()
            data = message.encode("ascii")
            client_socket.send(data)
    except socket.error as error_message:
        print("Error: " + str(error_message))

Problem Output Behavior: 问题输出行为:

Client 1 can connect to the server and send messages. 客户端1可以连接到服务器并发送消息。 The server receives and prints these messages fine. 服务器可以正常接收和打印这些消息。

Now when Client 2 connects to the server, Client 1's messages no longer is sent to the server. 现在,当客户端2连接到服务器时,客户端1的消息不再发送到服务器。 Now only Client 2 can send messages to the server and have it printed fine. 现在,只有客户端2可以将消息发送到服务器,并可以正常打印。

To further carry this out, Client 3 connects to the server. 为了进一步执行此操作,客户端3连接到服务器。 Now both Client 1's and Client 2's messages aren't sent to the server, only Client 3's messages are. 现在,客户端1和客户端2的消息都不会发送到服务器,只有客户端3的消息才发送到服务器。

Your client_thread function has some issues. 您的client_thread函数有一些问题。 conn is the connection passed to the thread, but connection and address are being read from the global namespace. conn是传递给线程的连接,但是正在从全局名称空间读取connectionaddress Instead, use the connection passed in and pass the address as well, so each thread is only referring to local variables for the connection/address pair it is responsible for: 相反,请使用传入的连接并同时传递address ,因此每个线程仅引用其负责的连接/地址对的局部变量:

def client_thread(conn,addr):
    welcome_message = "Connected to server " + HOST + ":" + str(PORT) + "\n"
    conn.send(welcome_message.encode("ascii"))
    while True:
        data = conn.recv(2048)
        message = data.decode("ascii")
        if not data:
            break
        print("(" + addr[0] + ":" + str(addr[1]) + ") " + message)
    conn.close()

In the main code: 在主代码中:

_thread.start_new_thread(client_thread, (connection,address))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM