[英]Data send disallowed? [WinError 10057]
I know this is an error on the server part where the s
variable is being used when the conn
variable should be used but I've been sitting here for 2 hours and cant see the mistake.我知道这是服务器部分的错误,当应该使用
conn
变量时正在使用s
变量,但我已经在这里坐了 2 个小时,看不到错误。 The error: [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
My code:错误:
[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,))```
I figured it out!我想到了! According to other people's
[WinError 10057]
problems its the server refusing to use a certain sock variable.根据其他人的
[WinError 10057]
问题是服务器拒绝使用某个 sock 变量。 Its been like 6 hrs and i decided to check my client code and it turns out that I first declared the client
variable to be a normal socket in the __init__
function.已经过了 6 小时,我决定检查我的客户端代码,结果发现我首先在
__init__
function 中将client
变量声明为普通套接字。 Then in my connect
function takes this variable and modifies it to be a connected variable (so send and recv functions work in that function).然后在我的
connect
function 中获取此变量并将其修改为连接变量(因此 send 和 recv 函数在该函数中工作)。 Then my send function re-reads the original socket (non-connection) and uses that to send (which fails).So i did the very complicated equivalent version (in server words) of doing conn, addr = s.accept()
then making old_s = s
then s = conn then stupidly resetting conn = old_s.然后我的发送 function 重新读取原始套接字(非连接)并使用它发送(失败)。所以我做了非常复杂的等效版本(用服务器的话)做
conn, addr = s.accept()
然后使old_s = s
然后 s = conn 然后愚蠢地重置 conn = old_s。 That's the best I can explain it i'm sorry if you didn't understand a word of it.这是我能解释的最好的了,如果你一个字都没听懂,我很抱歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.