binding a UDP port and listening for connections

When I try to ping a Minecraft server via LAN, the documents say the following:

In Singeplayer there is a function called "Open to LAN". Minecraft (in the serverlist) binds a UDP port and listens for connections to (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.

I tried to implement this in Python in the following way:

import socket

UDP_IP = ""
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)

Which gave me an OSError: [WinError 10049] error.

Please help :( I have no idea what's wrong with my code.

By the way, sending packets to that port works, and the fake server shows up on the Minecraft app.

1 answer

  • answered 2021-04-21 15:19 inof

    You cannot bind to a multicast address like that. It is somewhat more involved. I recommend to read this article that explains all the details of using multicast with Python.