簡體   English   中英

不允許發送數據? [Win 錯誤 10057]

[英]Data send disallowed? [WinError 10057]

我知道這是服務器部分的錯誤,當應該使用conn變量時正在使用s變量,但我已經在這里坐了 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 小時,我決定檢查我的客戶端代碼,結果發現我首先在__init__ function 中將client變量聲明為普通套接字。 然后在我的connect function 中獲取此變量並將其修改為連接變量(因此 send 和 recv 函數在該函數中工作)。 然后我的發送 function 重新讀取原始套接字(非連接)並使用它發送(失敗)。所以我做了非常復雜的等效版本(用服務器的話)做conn, addr = s.accept()然后使old_s = s然后 s = conn 然后愚蠢地重置 conn = old_s。 這是我能解釋的最好的了,如果你一個字都沒聽懂,我很抱歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM