Multicast listing between two computer is NOT working

188 Views Asked by At

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))
1

There are 1 best solutions below

4
Stanisław Kawulok On

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))