'flaky python multicast reception on 3 machines

I have three machines on my network: a Win10 box, a Raspberry Pi/Debian, and a Win11 box. I want any machine to be able to generate multicast UDP packets, and all three machines should receive them. Here is the receiver test code I'm using.

    import sys
    import socket
    import struct

    multicast_group = '239.3.29.71'
    server_address = ('', 10000)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(server_address)
    group = socket.inet_aton(multicast_group)
    mreq = struct.pack('4sL', group, socket.INADDR_ANY)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)


    while (True):
        print("waiting on pkts")
        d, addr = sock.recvfrom(4096)
        print("Received packet, d=", d)

Here's the behavior I'm seeing. I run the above code on all three machines simultaneously. If the mcast sender code is running on machine X, then the above receive code on machine X runs correctly, but the receiver code on Y and Z may or may not see those packets. And if the receiver code on Y does happen to see the mcast packets coming from X, and I suspend/resume X's sender, Y will usually stop receiving. Doesn't seem to matter which machine I put the sender code on, the same "local always works, otherwise maybe not" behavior manifests.

Any clues on where I should go digging? Is my network somehow not setup for handling mcast traffic reliably? (sorry in advance if I've screwed up the question protocol -- first timer here.)

The "X doesn't see Y's packets" problem also seems related to how I exit either the multicast sender program or the receiver (or both). Maybe I'm somehow leaving the OS's packet handling system in an inconsistent state by not exiting gracefully?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source