绑定 UDP 端口并监听连接

binding a UDP port and listening for connections

当我尝试通过 LAN ping Minecraft 服务器时,documents 说如下:

In Singeplayer there is a function called "Open to LAN". Minecraft (in the serverlist) binds a UDP port and listens for connections to 224.0.2.60:4445 (Yes, that is the actual IP, no matter in what network you are or what your local IP Address is)" ....... client side, bind a UDP socket and listen for connections. You can use a MulticastSocket for that.

我试图通过以下方式在 Python 中实现它:

import socket

UDP_IP = "224.0.2.60"
UDP_PORT = 4445

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print("received message: %s" % data)

这给了我一个 OSError: [WinError 10049] 错误。

请帮忙 :( 我不知道我的代码有什么问题。

顺便说一下,向那个端口发送数据包是有效的,并且假服务器出现在 Minecraft 应用程序上。

您不能绑定到这样的多播地址。它涉及更多。 我建议阅读 this article,它解释了使用 Python.

的多播的所有细节