问题描述
我有一台 Windows 机器,其中有两个脚本通过 UDP 多播(在同一台机器上)发送和接收消息.我有一个 C 和 Python3 实现.Python3 看起来像这样:
I have a Windows machine where I have two scripts that send and receive messages via UDP multicast (on the same machine). I have a C and Python3 implementation of this. The Python3 one looks like this:
发件人.py
import socket
MCAST_GRP = '239.1.1.1'
MCAST_PORT = 1234
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
print("Sending")
sock.sendto(bytearray("str()", "utf-8"), (MCAST_GRP, MCAST_PORT))
data, address = sock.recvfrom(1024)
print('received %s bytes from %s' % (len(data), address))
print(data)
receiver.py
receiver.py
import socket
import struct
import sys
multicast_group = '239.1.1.1'
server_address = ('', 1234)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
sock.bind(server_address)
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
# Receive/respond loop
while True:
print('
waiting to receive message')
data, address = sock.recvfrom(1024)
print('received %s bytes from %s' % (len(data), address))
print(data)
print('sending acknowledgement to', address)
sock.sendto(bytearray("ack", "utf-8"), address)
我有另一个物理设备连接到同一台机器,但我的程序无法从它接收任何消息.我可以看到 Wireshark 正在查看来自其他物理设备的消息,这些消息通过 eth0
接口到达相同的 IP 和端口.我知道我的脚本生成的流量在 VirtualBox Host-Only Network
上.我不确定这是否会导致我看不到来自外部设备的 UDP 多播消息.
I have another physical device hooked up to the same machine but I cannot receive any messages from it with my programs. I can see that Wireshark is seeing the messages from the other physical device, these messages are coming over the eth0
interface to the same IP and Port. I know that the traffic generated by my scripts is on the VirtualBox Host-Only Network
. I am not sure if that could cause the issue of me not seeing the UDP multicast messages from the external device.
我也在 Linux 机器上测试过(最新的 kali 版本),但也无法从外部设备接收到任何消息.
I have tested it on a Linux machine as well (latest kali version) but could not receive any messages from the external device as well.
如果我遗漏了一些信息,请告诉我.
If I am missing some information, please let me know.
编辑 1:
我的设置如下:我正在运行本机 Windows 10 机器.这台机器是一个连接的设备,它正在运行一些我不知道的操作系统.我只能从中发送和接收消息.我可以通过在我的 Windows 10 机器上的物理以太网端口发送以太网、TCP 和 IPv4 数据包,方法是指定我为此使用的软件来使用 eth0 和我在网络适配器设置中分配给该端口的已定义 IP(v4) 地址(192.168.1.100)
My setup is as follows: I am running a native Windows 10 machine. To this machine is a device connected that is running some OS I don't know. I am only able to send and receive messages from it. I can send Ethernet, TCP, and IPv4 packets over the physical ethernet port on my Windows 10 machine by specifying the software I am using for this to use eth0 and a defined IP(v4) address I assigned to that port in the network adapter settings (192.168.1.100)
脚本在也连接到设备的同一台 Windows 10 计算机上运行.他们在这个接口 VirtualBox Host-Only Network
上发送,但我不知道为什么.我没有配置这样的东西.我认为接口应该不是问题,因为这就是 UDP 多播的工作原理(我不确定,所以如果我弄错了,请告诉我!)
The scripts are running on the same Windows 10 machine that is also connected to the device. They are sending on this interface VirtualBox Host-Only Network
but I don't know why. I did not configure anything like this. I assume that the interface should not be a problem because that is how UDP Multicast works (I am not sure of that so if I am mistaken please let me know!)
发件人的示例输出如下所示:
A sample output of the sender looks like this:
Sending
received 3 bytes from ('192.168.56.1', 3000)
b'ack'
Process finished with exit code 0
和接收者:
waiting to receive message
received 5 bytes from ('192.168.56.1', 55132)
b'robot'
sending acknowledgement to ('192.168.56.1', 55132)
waiting to receive message
我希望澄清设置.如果仍有信息缺失,请告诉我!
I hope that clarifies the setup. If there is still information missing please let me know!
推荐答案
如 https://www.tldp.org/HOWTO/Multicast-HOWTO-6.html,套接字 API 要求您识别接口以及要使用的多播地址/端口.
As covered in https://www.tldp.org/HOWTO/Multicast-HOWTO-6.html, the sockets API requires that you identify the interface as well as the multicast address/port you want to use.
通过在示例代码中未指定此内容,您已将其留给操作系统来选择,并且它选择了 VirtualBox Host-Only 网络.不幸的是,这种类型的网络仅限于在 Windows 机器上运行的虚拟机.
By not specifying this in your sample code, you have left this down to the OS to pick and it has picked a VirtualBox Host-Only Network. Unfortunately this type of network is limited to VMs running on the Windows machines.
要修复它,您需要确定要用于多播的接口,并将其传递给您的发送和接收代码.例如:
To fix it, you need to identify the interface that you want to use for the multicast and pass that in to your sending and receving code. For example:
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(MCAST_IF_IP))
接收器.py
mreq = struct.pack('4s4s', group, socket.inet_aton(MCAST_IF_IP))
其中 MCAST_IF_IP
是您要使用的接口的 IP 地址.
where MCAST_IF_IP
is the IP address of the interface that you want to use.
这篇关于看不到来自另一台设备的 UDP 多播消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!