2

I have a device connected to a windows 10 machine and this device is sending out various multicast UDP packages. I can see them on the windows machine using wireshark:

enter image description here

Now to get them in python (3.10.10) I am using the following script:

import struct
import socket
import select

HOST = "192.168.200.5"
MCAST_GRP = "239.0.0.12"
PORT = 45013

def initSocket(group, port, host):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    
    sock.bind(("0.0.0.0", port))
    # struct.pack converts non-bytes values to bytes. To be used as the last parameter of 
    # setsockopt which needs to be a bytes-like object representing a buffer.
    # "4sl" is a format specifier for a 4-bytes string and a long int.
    mreq = struct.pack("4sl", socket.inet_aton(group), socket.INADDR_ANY)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
    return sock

sock = initSocket(MCAST_GRP, PORT, HOST)
while True:
    ready = select.select([sock], [], [], 1)
    if ready[0]:
        data = sock.recv(4096)
        print(f"received message:  {data}")   
    else:
        print("No data received")

But no message seems to be received! I only get No data received.

I am running this script as admin, and no other process seems to be using port 45013 (according to netstat -ano -p udp). I also allowed the "python" app through the firewall.

The device itself is connected to an ethernet adaptor (with IP address 192.168.200.5) to the computer via USB. And the computer DOES receive the UDP messages (see wireshark).

netsh int ip show join shows

Interface 25: Ethernet

Scope       References  Last  Address
----------  ----------  ----  ---------------------------------
0                    0  Yes   224.0.0.1
0                    4  Yes   224.0.0.251
0                    1  Yes   224.0.0.252
0                    3  Yes   239.255.255.250

So what else can I try? How to make it work? What is the problem?

Maybe there is a workaround to use wireshark inside python to receive the UDP data?

I have been using a C code to receive the UDP data on a Linux VM running on windows, and after some configuring and re-configuring settings and IP addresses the C code is able to receive the data. But not the python code. The python code now chooses to create a new error (on Linux VM only):

Traceback (most recent call last):
  File "udp_receiver.py", line 54, in <module>
    sock = initSocket(MCAST_GRP, PORT, HOST)
  File "udp_receiver.py", line 32, in initSocket
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
OSError: [Errno 19] No such device
Alex
  • 41,580
  • 88
  • 260
  • 469
  • Does `netsh int ip show join` confirm that the system has joined the groups? – user1686 Apr 06 '23 at 05:38
  • Not sure, output here: `Scope References Last Address ---------- ---------- ---- --------------------------------- 0 0 Yes 224.0.0.1 0 4 Yes 224.0.0.251 0 1 Yes 224.0.0.252 0 3 Yes 239.255.255.250` – Alex Apr 06 '23 at 05:41
  • I think you need `sock.bind((MCAST_GRP,port))` rather than binding to all interfaces. – Mark Setchell Apr 06 '23 at 06:56
  • When I try that I get an error `OSError: [WinError 10049] The requested address is not valid in its context` – Alex Apr 06 '23 at 06:58
  • On snuffling around, it appears this might be a minefield. One suggestion here https://stackoverflow.com/a/36599504/2836621 that you can use `''` as the address. No other ideas, sorry. – Mark Setchell Apr 06 '23 at 07:24
  • Already tried that, no success either – Alex Apr 06 '23 at 07:27

0 Answers0