python - server.send(join.encode ('utf-8' )) 给了我这个错误

标签 python

当我连接到客户端时,我收到此错误。

felix+
Traceback (most recent call last):
  File "c:\Users\felix\Documents\CODE\Uno02\uno02_server.py", line 23, in <module>
    server.send(join.encode('utf-8'))
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

客户端只是从用户那里接收一些信息,然后连接到服务器。我不确定我输入的错误是什么 客户端代码:

import socket

server_ip = input("Enter the server IP: ")
client =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((server_ip,42069))
name = input("Please enter a username: ") 
client.send(name.encode())
while True:
    server_msg = client.recv(1024)
    print(server_msg.decode())

和服务器代码:

name_list = []
ip_list = []
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("127.0.0.1",42069))
server.listen()

while(True):

    (clientConnected, clientAddress) = server.accept()

    print("connection gained %s:%s"%(clientAddress[0], clientAddress[1]))

   

    clientdata = clientConnected.recv(1024)

    name = clientdata.decode()
    join = name + " joined"
    name_list.append(clientdata)
    ip_list.append(clientAddress[0])
    print(name+"+")
    server.send(join.encode('utf-8'))

最佳答案

替换

server.send(join.encode('utf-8'))

clientConnected.send(join.encode('utf-8'))

检查Python TCP Communication了解更多详情

关于python - server.send(join.encode ('utf-8' )) 给了我这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70749153/

相关文章:

python - 如何匹配该值并根据其他列字符串为它们分配一个新列

python - 检查 Homebrew 安装以安装 Python

python - 更改多索引数据帧较低级别中的多个值

python - 匹配正则表达式,其中要匹配的字符串是从变量构建的

python 字典设置最小大小

python - 如何在Python上使用RSA私钥和SHA256解密

Python pygame窗口不断崩溃

python - 将同名 pandas 数据框列的值聚合到单列

python - 为什么对象类定义会产生影响

python - 属性计算在来自 openCV for python 的 ORB 中不起作用