Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python多客户端聊天不';行不通_Python_Python 3.x_Sockets_Networking - Fatal编程技术网

python多客户端聊天不';行不通

python多客户端聊天不';行不通,python,python-3.x,sockets,networking,Python,Python 3.x,Sockets,Networking,当我运行main和client的py文件时,不显示错误,但不显示发送消息的输入。如何修复 main.py结果-->正在等待。。。并连接192.168.1.26 58767 clients.py结果-->无 这是main.py import threading import socket server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO

当我运行main和client的py文件时,不显示错误,但不显示发送消息的输入。如何修复

main.py结果-->正在等待。。。并连接192.168.1.26 58767

clients.py结果-->无

这是main.py

import threading
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

host="192.168.1.26"
port=1111
threadcount=0
server.bind((host,port))
print("Waiting...")
server.listen(3)


def clients(connection):
    connection.send(str.encode("welcome\n"))
    while True:
        data=connection.recv(2048)
        reply="server says" + data.decode("utf-8")
        if not data:
            break
        connection.sendall(str.encode(reply))
    connection.close()

while True:
    client,adress=server.accept()
    print("connected"+ str(adress[0]) + " " + str(adress[1]))
    threading.Thread(target=clients,args=(client,))
    threadcount+=1
server.close()
这是clients.py

import socket

client_socket=socket.socket()
host="192.168.1.26"
port=1111
client_socket.connect((host,port))

response=client_socket.recv(1024)
print("hello")

while True:
    input=input("say")
    client_socket.send(str.encode(input))
    Response=client_socket.recv(1024)
    print(Response.decode("utf-8"))

client_socket.close()

看起来你的线程从未真正启动过。一种解决方案是定义一个类,该类在
运行
例程中实现线程逻辑。类应该继承
线程。线程
。然后,您可以简单地将类初始化为对象,并调用其
start
例程。请尝试以下原始服务器代码的变体

import threading
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

host="192.168.1.26"
port=1111
threadcount=0
server.bind((host,port))
print("Waiting...")
server.listen(3)

class ServerThread(threading.Thread):
    def __init__(self, threadsocket):
        threading.Thread.__init__(self)
        self.threadsocket = threadsocket

    def run(self):
        self.threadsocket.send(str.encode("welcome\n"))
        while True:
            data=self.threadsocket.recv(2048)
            reply="server says" + data.decode("utf-8")
            print(reply)
            if not data:
                break
            self.threadsocket.sendall(str.encode(reply))
        self.threadsocket.close()

while True:
    client,adress=server.accept()
    print("connected"+ str(adress[0]) + " " + str(adress[1]))
    thread = ServerThread(client)
    thread.start()
    threadcount+=1

server.close()

对于客户端,正如我在评论中提到的,您不应该使用
input
作为变量名来存储
input
函数的结果。将其更改为类似于
data
的内容。

不是根本问题,而是在
client.py
code中,绝对不应使用
input
作为变量来存储
input
函数的结果。请使用不同的变量名,例如
data
@h0r53 not fix:/server无法发送消息为什么?可能是客户端的发送/读取被阻止。确保每个
send
都以
\n
结尾,看看这是否有用。在我的机器上,原始客户端与我提供的新服务器一起工作。