简体   繁体   中英

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. 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:

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. 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. 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). 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. That's the best I can explain it i'm sorry if you didn't understand a word of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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