5

I'd like to debug some multicast issues, and I hope to have some small programs/utilities to display incoming multicast packets.

From the sending machine(A), I use Richard Stevens's sock program(provided with his TCP/IP Illustrated book Vol1) to send multicast packets(source port=dest port=7000), like this:

sock -u -b 7000 224.0.0.7 7000

On the receiving machine(B), I can capture the very sent packet with Wireshark, however, the same sock command running on B does not report receiving anything.

Then, what program should I use on B to see incoming multicast packets, aside from Wireshark which is overkill.

Linux and Windows programs are both welcome.

enter image description here

Jimm Chen
  • 3,411
  • 3
  • 35
  • 59

4 Answers4

15

Here's a Python script that will print the incoming data:

# Multicast client
# Adapted from: http://chaos.weblogs.us/archives/164

import socket

ANY = "0.0.0.0" 
MCAST_ADDR = "224.0.0.7"
MCAST_PORT = 7000

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

# Allow multiple sockets to use the same PORT number
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Bind to the port that we know will receive multicast data
sock.bind((ANY, MCAST_PORT))

# Tell the kernel that we want to add ourselves to a multicast group
# The address for the multicast group is the third param
status = sock.setsockopt(socket.IPPROTO_IP,
                         socket.IP_ADD_MEMBERSHIP,
                         socket.inet_aton(MCAST_ADDR) + socket.inet_aton(ANY))

# setblocking(False) is equiv to settimeout(0.0) which means we poll the socket.
# But this will raise an error if recv() or send() can't immediately find or send data. 
sock.setblocking(False)

while 1:
    try:
        data, addr = sock.recvfrom(1024)
    except socket.error as e:
        pass
    else:
        print("From: ", addr)
        print("Data: ", data)
Harsath
  • 133
  • 3
  • 6
robhem
  • 168
  • 2
  • 8
  • 2
    I happen to know Python. You snippet works great on Windows XP and Linux(openSUSE 11.3), exept that it consumes 100% CPU core. – Jimm Chen Mar 12 '13 at 13:54
  • The 100% CPU usage is due to sock.setblocking(0), you may wish to do a blocking read with a small timeout using sock.settimeout(0.1) or similar instead – Lummo Nov 24 '16 at 08:20
  • 1
    I realize this is a very old answer, but for what it is worth this does not work in Python 3. – William John Holden Dec 31 '20 at 17:12
1

You can use netcat (nc) to do that:

netcat -vv -l -p 1234 -u

This means netcat is verbosely listening on port 1234 of the localhost in UDP mode.

FuePi
  • 1,958
  • 22
  • 18
0

I've written a multicast testing application back in the day.

You can check it out here: https://github.com/eranbetzalel/SimpleMulticastAnalyzer

Eran Betzalel
  • 4,105
  • 3
  • 38
  • 66
0

This is the first hit when I searched for capturing multicast packets using netcat, and I found out that tcpdump does a job better. Just making a note for any one else hitting this post.

To install:

sudo apt install tcpdump

To run:

tcpdump -c 8 -n -i eth0 portrange 1234-1239

looks like there may be a Windows port too, but I didn't try it: https://www.winpcap.org/windump/

user1261470
  • 141
  • 3
  • 6
  • 2
    How can this work, as there is no way it can know what multicast group to sub\scribe to? – Chris Mar 30 '20 at 03:15
  • @Chris It is a network traffic dump. It works as long as the operating system kernel receives the multicast packet and it doesn't announce membership to the network by itself. – Pavel Šimerda Sep 08 '20 at 12:02
  • 3
    @Pavel: But without any announcement from the OS onto the network, the multicast packets will not be sent from switch to the NIC (surely). Thats the whole point of multicast - the packets onto go to subscribed "consumers". – Chris Sep 09 '20 at 03:35
  • @Chris First, not all multicast is subscription-based. Second, you can use `tcpdump` together with another tool, see: https://unix.stackexchange.com/a/467676/60296 – Pavel Šimerda Mar 02 '21 at 22:03