在指定的UDP端口上接收组播数据包的小程序?

问题描述 投票:4回答:5

我想调试一些多播问题,我希望有一些小程序/实用程序来显示传入的多播数据包。

从发送机器(A),我使用Richard Stevens的sock程序(随TCP / IP插图书Vol1提供)发送多播数据包(源端口= dest端口= 7000),如下所示:

sock -u -b 7000 224.0.0.7 7000

在接收机器(B)上,我可以使用Wireshark捕获已发送的数据包,但是,在B上运行的相同sock命令不报告接收任何内容。

然后,我应该在B上使用什么程序来查看传入的多播数据包,除了Wireshark是否过度。

Linux和Windows程序都是受欢迎的。

multicast
5个回答
10
投票

这是一个打印传入数据的python脚本;

# 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(0) 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(0)

while 1:
    try:
        data, addr = sock.recvfrom(1024)
    except socket.error as e:
        pass
    else:
        print "From: ", addr
        print "Data: ", data

2
投票

您可以使用netcat(nc)来执行此操作:

netcat -vv -l -p 1234 -u

这意味着netcat在UDP模式下在本地主机的端口1234上详细侦听。


0
投票

我在当天写了一个多播测试应用程序。

你可以在这里查看:https://github.com/eranbetzalel/SimpleMulticastAnalyzer


0
投票

在Windows上,我发现这些实用程序非常方便调试udp(两端)

http://www-personal.umich.edu/~bdr/et/mcast-windows.html#download


0
投票

这是我使用netcat搜索捕获多播数据包的第一个命中,我发现tcpdump可以更好地完成工作。只是为任何其他人点击这篇文章做笔记。

安装:

sudo apt install tcpdump

跑步:

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

看起来可能还有一个Windows端口,但我没有尝试过:https://www.winpcap.org/windump/

© www.soinside.com 2019 - 2024. All rights reserved.