简体   繁体   中英

Python Multicast Not Receiving Data on BusyBox

We're trying to get our Python app to respond to a multicast request over UDP. We have this Python code which runs quite nicely on our Raspberry Pi running Raspian, but we've tried to port this to our BusyBox environment but it doesn't ever respond to requests (unless it's on the local subnet).

import socket
import struct

host = '172.20.0.56'
MCAST_GRP = '224.1.1.1'
MCAST_PORT = 49999
IS_ALL_GROUPS = True

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if IS_ALL_GROUPS:
    # on this port, receives ALL multicast groups
    sock.bind(('', MCAST_PORT))
else:
    # on this port, listen ONLY to MCAST_GRP
    sock.bind((MCAST_GRP, MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)

sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(host))

sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, 
                   socket.inet_aton(MCAST_GRP) + socket.inet_aton(host))
while True:
  print(sock.recv(1024))

Netstat is showing the membership group is being setup:

$ netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      224.0.0.1
eth0            1      224.1.1.1

IP A shows that MULTICAST is supported on the board. We can see that other machines in the group receive the messages. Is there anything else we can try?

Found the answer just after posting this entry.

We had to change net.ipv4.conf.all.rp_filter = 2 to net.ipv4.conf.all.rp_filter = 0 in the file /usr/lib/sysctl.d/50-default.conf

It now works as intended!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM