没有看到来自其他设备的UDP多播消息 [英] not seeing UDP multicast messages from another device

查看:185
本文介绍了没有看到来自其他设备的UDP多播消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台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:

sender.py

sender.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('\nwaiting 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机。这台机器是连接的设备,运行一些我不知道的操作系统。我只能发送和接收来自它的消息。我可以通过指定我用来使用eth0的软件和我在网络适配器设置中分配给该端口的已定义IP(v4)地址,通过Windows 10计算机上的物理以太网端口发送以太网,TCP和IPv4数据包(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))



receiver.py

receiver.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多播消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆