UDP Broadcast Network Programming with Python

I have written a small Python program that needs to continuously listen on port 2237.

The packets are received from one program, which I will call P1. There is also another program, P2, that receives commands from P1. Both P1 and P2 use port 2237.

My problem is that when I start my Python script, it successfully receives data from P1, but it fails to receive data from P2. The functionality only works again after I stop my program.

The Python script for receiving the packets is:

def run(udp_port: int): logger.info("Starting listener on port %d", udp_port) client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) client.setsockopt()

client.bind(("", udp_port))
while True:
    data, addr = client.recvfrom(4096)
    src_ip, src_port = addr

    logger.debug("Received %d bytes from %s:%d", len(data), src_ip, src_port)
    hexdump(data, logging.DEBUG)

    try:
        payload = parse_message(data)
        if payload is not None:
            logger.info("%r", payload)
    except Exception as e:
        logger.error("Failed to parse header: %s", e)

Does anyone know what the root cause of this issue is, and how we can fix it?

Answered by DTS Engineer in 905101022

In Extra-ordinary Networking you’ll find a link to Broadcasts and Multicasts, Hints and Tips which has specific advice on how to tackle problems like this. I wrote it for folks creating native code, but much of the same advice applies to Python as well. I encourage you to review it and apply the techniques it suggests.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The call should be client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1), rather than just client.setsockopt().

In my initial post, I may have been imprecise regarding the setup. Program P2 is supposed to receive data from P1 and generate datasets from it. However, P2 is not receiving the necessary data input. The Python script, however, is successfully receiving the data.

Attached is the source code I am currently using, which contains the described error.

def run(udp_port: int): logger.info("Starting listener on port %d", udp_port)

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

client.bind(("", udp_port))
while True:
    data, addr = client.recvfrom(4096)
    src_ip, src_port = addr

    logger.debug("Received %d bytes from %s:%d", len(data), src_ip, src_port)
    hexdump(data, logging.DEBUG)

    try:
        payload = parse_message(data)
        if payload is not None:
            logger.info("%r", payload)
    except Exception as e:
        logger.error("Failed to parse header: %s", e)

In Extra-ordinary Networking you’ll find a link to Broadcasts and Multicasts, Hints and Tips which has specific advice on how to tackle problems like this. I wrote it for folks creating native code, but much of the same advice applies to Python as well. I encourage you to review it and apply the techniques it suggests.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

UDP Broadcast Network Programming with Python
 
 
Q