0

Basing strictly on:

How do you UDP multicast in Python?

import socket
import struct

MCAST_GRP = '233.3.3.3'
MCAST_PORT = 12312
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.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
  print sock.recv(10240)

I stream video using VLC. However I can bind using only IS_ALL_GROUPS = True. Any time I would like to bind by IS_ALL_GROUPS = False I got WinError 10049: The requested address is not valid in its context. When I try to use the same sender and receiver scripts but on the linux there is no such error. What can I do in this situation?

  • AFAIK `bind()` expects you to pass in the unicast IP address of a local network interface; passing it the IP address of a multicast group instead may not be supported. – Jeremy Friesner Jan 07 '20 at 14:43
  • Actually, the name may be confusing. In my version of code, MCAST_GRP is list of strings(unicast addresses) which then should be binded separately in threads and im reffering to them by MCAST_GRP[X]. On my linux machine, everything works fine, each unicast address is reffering to responsible for it group, then I see packets coming and analysed corretly. Parameter IS_ALL_GROUPS is responsible for treating addresses separately or to treat them as sum of things going to sockets. On my windows machine I can use only binding using "" not specific address, which any way gives 0 Mbps output – kaszankabanka Jan 07 '20 at 15:03
  • In the code as shown, the multicast IP address `233.3.3.3` is being passed in both to `sock.bind()` and to the `sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP)` call. If the code that is shown is different from what your actual program is doing, that will make it difficult for people to reproduce the fault using the code that is shown. – Jeremy Friesner Jan 07 '20 at 15:10
  • I quite don't understand if the thing i'm doing is correct or somehow on linux it is foolproof and works beside my error. I got one windows machine and one debian machine, both of them connected with ethernet providing video packets. When code(my version just calculates bitrate) is run on linux, where MCAST_GRP is list and i execute above code let's say 3 times for MCAST_GRP[x] when IS_ALL_GROUPS = False and bitrates for those 3 addreses are let say 3,5,7 mbps, program will print out 3,5,7 mbps each second, however running program with True option, will result in printing 15Mbps three times/s – kaszankabanka Jan 08 '20 at 08:48
  • When code is run on windows, it executes only when IS_ALL_GROUPS is true, giving anyway wrong result of 0mbps, like it doesn't get packets, but it's weird because I see packets comming on wireshark. When code is run with False option, it gives winsock error. I thought it may be the reason of differences of SO_REUSEADDR for linux and windows machines, I read that that parameter for linux enables SO_REUSEPORT at the same time, where there is no such option in windows. – kaszankabanka Jan 08 '20 at 08:53

0 Answers0