gpt4 book ai didi

python - 不允许发送数据? [WinError 10057]

转载 作者:行者123 更新时间:2023-12-03 12:02:35 27 4
gpt4 key购买 nike

我知道这是服务器部分的错误,当应使用s变量时正在使用conn变量,但是我已经坐在这里2个小时了,看不到错误。错误:[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
from _thread import *

server = '123.456.78.9'
port = 5555


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
s.bind((server, port))
except socket.error as e:
str(e)

s.listen()
print("Waiting for connections, server has been started")

def threaded_client(conn):
reply = ""
conn.sendall(str.encode("[Server, Server]Mis:200:Connected"))
while True:
try:
data = conn.recv(2048)
reply = data.decode("utf-8")

if not data:
print("Disconnected from", addr[0])
break

print("Received: ", reply)
print("Sending: ", reply)
conn.sendall(str.encode(reply))
except:
break
print("Connection to", addr[0], "has been lost!")
conn.close()

while True:
conn, addr = s.accept()
banlist = open('bannedip.bipf')
if addr[0] in banlist.read():
conn.sendall(str.encode("[Server, Server]Err:401:Banned"))
conn.close()
print("Banned ip", addr[0], ", was disconnected as their ip (", addr[0], ") is listed in the ban file")
else:
print("Connected to:", addr[0])
start_new_thread(threaded_client, (conn,))```

最佳答案

我想到了!根据其他人的[WinError 10057]问题,服务器拒绝使用某个sock变量。大概6个小时了,我决定检查我的客户端代码,结果我首先在client函数中声明了__init__变量为普通套接字。然后在我的connect函数中将此变量修改为连接变量(因此send和recv函数在该函数中起作用)。然后我的send函数重新读取原始套接字(非连接)并使用它发送(失败)。所以我做了非常复杂的等效版本(以服务器为单位),即conn, addr = s.accept()然后使old_s = s然后s = conn然后愚蠢重置conn = old_s。最好的办法是我能解释一下,如果您听不懂它,对不起。

关于python - 不允许发送数据? [WinError 10057],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62493077/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com