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

标签 python sockets tcp server

我知道这是服务器部分的错误,当应使用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/

相关文章:

java - 如何将客户存储在列表中

sockets - 套接字对和成对的未命名管道之间有什么区别吗?

java - 基于字符串变量路由ip数据包

php - 我需要一个脚本/应用程序来保持 PHP 前端的 TCP 连接

Python netifaces 模块 Windows 安装

python - xmltodict 将非类型输出转换为列表

python - Salt-Stack:执行与模式匹配的状态

python - 无法在 SocketServer.TCPServer 中重用套接字

c - 我需要分别接收两个数据包 - linux将它们合并为一个并发送给应用程序

python - 当 scipy.integrate.odeint 中输出达到 0 时停止积分