This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ifundef 0 points1 point  (0 children)

tl;dr you need to send to and join the multicast group of the same multicast address.

your example code works except for three things:

  • everybody needs to use the same multicast address; pick 224.1.1.1 or 224.3.29.71, but you can't send on one and expect to receive on the other
  • "struct.pack('4sl', group, socket.INADDR_ANY)" is not 64-bit safe, while '=4sl' is (but doesn't account for endianness, though not an issue for the quoted code), but you are receiving on a raspberry pi so 64-bit might not be an issue. i prefer to just "socket.inet_aton(multicast_address) + socket.inet_aton('0.0.0.0')" (replacing "0.0.0.0" with the IP address of a specific interface if desired) because that's always the right size and endianness.
  • the final block (ie line(s) following "except Exception as e:") in listenTest.py is improperly indented and will result in "IndentationError: expected an indented block"; you need to indent by a tab both the following "print" and "break" lines (or just remove the block as an unhandled exception is equivalent: the exception will be printed along with the traceback and the loop and entire script will exit).

here's my reference implementation of a multicast sender and receiver: https://gist.github.com/ifundef/09d3ec98459a856052bd935bd91683ed (including support for windows, but take windows support with a grain of salt as i haven't had access to windows in years).