0

I am trying to write a listener , which will listen to multicast data from different systems. As per my understanding, any system which is present in the same subnet will be able to get any data that will be sent to a particular muticast_grp and multicast port. But in my case, the below code is working fine for any data which will be sent to the same PC, but is NOT able to capture the data which will be sent from another PC.

I am able to see the data in Wireshark. But not in the reciver. I don't have control over the server(sender)

import socket
import struct
import sys

MCAST_GRP  = '239.255.255.250'
MCAST_PORT  = 3702
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)
sock.bind(('', 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))
Sreevathsabr
  • 649
  • 7
  • 23

1 Answers1

0

You have to listen on host '0.0.0.0', but send to the reciver IP

example i want to send string "hi" to computer with ip "1.1.1.1"

then for sending I use


import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

s.sendto(bytes("hi", 'utf-8'), (1.1.1.1, 3702))

and to recive

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

sock.bind(('0.0.0.0', 3702))

print(sock.recv(3))
  • when I want to listen to multicast ip , how can it be '0.0.0.0' – Sreevathsabr Jul 23 '22 at 16:12
  • I don't have control over the sending part , as it will server which will sending multicast packets – Sreevathsabr Jul 23 '22 at 16:32
  • if you have multiple servers sending data to your client, it should work at least it worked for me on local network (runned 2 instances of sending part and one of reciving) – Stanisław Kawulok Jul 23 '22 at 16:38
  • The above code works when from server , i have given the client IP . But in case of multicast , we will be sending to `239.255.255.250` something of this kind of IP . For this it is NOT working – Sreevathsabr Jul 23 '22 at 18:21