Basing on MC receiver from stack: How do you UDP multicast in Python?
I would like to completely understand what is going on. Here is what I understand and what not:
As far as I understand: socket_name = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
=> creating socket with IP proto ver 4, that will receive MC datagrams using UDP.
socket_name.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
this is responsible for setting parameter that socket could use the same address
(and in this case, port, because SO_REUSEADDR = SO_REUSEPORT for Multicasts). (Paragraph multicast ~ How do SO_REUSEADDR and SO_REUSEPORT differ?)
if IS_ALL_GROUPS: socket_name.bind(('', MCAST_PORT))
means that if IS_ALL_GROUPS
is true, bind socket to any addres, and else: socket_name.bind((MCAST_GRP, MCAST_PORT))
means bind socket to specific given IP address.
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
means convert IP address to binary form and socket_name.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
means that we add membership of group and after this line packets starting to arrive.
However I don't understand some things.
Why when I used IS_ALL_GROUPS = true
where MCAST_GRP = '239.0.1.104'
program could start and when the parameter was false, it didn't bound to specific multicast address? My logic is that when the parameter is true it binds to any MCast addres he gets from IGMP join messages and when parameter is false, it binds to specific given address. Am I correct?
I have multithread program that analyses bitrate to which i provide more than one address in form of list. When I set IS_ALL_GROUPS
to false, program works correctly printing out e.g. 10.5, 4.5, 5.0
where each result is bitrate of one stream from unique address, however all of addresses share the same port 12345
. When I set IS_ALL_GROUPS
to true, program sums up results giving 20.0, 20.0, 20.0
, do you know what may be the cause?
import socket
import struct
MCAST_GRP = '239.0.1.104'
MCAST_PORT = 12345
IS_ALL_GROUPS = True
socket_name = 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
socket_name.bind(('', MCAST_PORT))
else:
# on this port, listen ONLY to MCAST_GRP
socket_name.bind((MCAST_GRP, MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
while True:
print socket_name.recv(10240)