I am using UDP multicast to send messages to an embedded host (Raspberry Pi) in my home network but I am running into issues receiving multicast traffic on Ventura 13.4.
I have a simple Python receiver/sender script:
receiver.py (with from_nic_ip set as the local address related to the port that I want to bind to, multicast addr: 224.0.0.0, multicast port: 42073)
def receive_loop(from_nic_ip, multicast_group_ip, multicast_port):
receiver = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM,\
proto=socket.IPPROTO_UDP, fileno=None)
multicast_group = (multicast_group_ip, multicast_port)
receiver.bind(multicast_group)
if from_nic_ip == '0.0.0.0':
mreq = struct.pack("=4sl", socket.inet_aton(multicast_group_ip), socket.INADDR_ANY)
else:
mreq = struct.pack("=4s4s",\
socket.inet_aton(multicast_group_ip), socket.inet_aton(from_nic_ip))
receiver.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
while True:
buf, _ = receiver.recvfrom(BUFSIZE)
msg = buf.decode()
print(msg)
reciver.close()
sender.py
def send_loop(host_ip_addr, multicast_group_ip, multicast_port):
sender = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM,\
proto=socket.IPPROTO_UDP, fileno=None)
multicast_group = (multicast_group_ip, multicast_port)
sender.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 1)
sender.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF,\
socket.inet_aton(host_ip_addr))
while True:
print("Sending data!")
sender.sendto("test".encode(), multicast_group)
time.sleep(1)
sender.close()
If I run macOS as a sender and the Raspberry Pi as the receiver, I get the expected output: "test" printed continuously. If I reverse the roles, I see no "test" output.
I have verified that the script works as a sender/receiver by duplicating the test on a Linux desktop and I see that the two nodes can communicate with each other as both a sender or receiver.