Windows 中Python UDP 服务器启动失败,错误代码为 WinError 10045

问题描述 投票:0回答:1

我正在尝试使用以下代码构建Python UDP服务器。


import socket

if __name__ == '__main__':
    bind_ip = "0.0.0.0"
    bind_port = 30335
    
    server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP, Server setting
    server.bind((bind_ip, bind_port))
    
    print("[*] Listening on %s:%d " % (bind_ip, bind_port))
    
    while True:
        client, addr = server.accept()
        print('Connected by ', addr)
    
        while True:
            data = client.recv(1024)
            print("Client recv data : %s " % (data))
    
            client.send("ACK!")

我在下面遇到了一个错误

Traceback (most recent call last):

  File C:\ProgramData\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\jimmy\desktop\github\worldlongcompany\python\main.py:20
    client, addr = server.accept()

  File C:\ProgramData\anaconda3\Lib\socket.py:294 in accept
    fd, addr = self._accept()

OSError: [WinError 10045] The attempted operation is not supported for the type of object referenced

我的代码有什么问题吗?我是 Python 新手,请告诉我如何修复它。

我的Python版本:

> ! python --version
Python 3.11.8
python windows networking udp
1个回答
0
投票
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP, Server setting
...
    client, addr = server.accept()

accept
是针对TCP套接字的操作。不过,您正在创建一个 UDP 套接字 (SOCK_DGRAM)。

© www.soinside.com 2019 - 2024. All rights reserved.