我正在尝试让 Windows 上的 Python3 加入 IPv4 链接本地多播组 239.255.0.3。当我将脚本配置为加入组 224.3.29.71 时,我收到 Windows 防火墙提示,我成功加入了该组并收到了消息。但是当我将组地址更改为 239.255.0.3(在发送器和接收器上)时,消息不再通过。在这两种配置中,我都可以看到(通过 Wireshark)到达 Windows 的数据包。但它们没有到达我的应用程序。我有一条以太网电缆直接从我的 Windows PC(静态 IP 配置)连接到正在传输多播数据包(也是静态 IP 配置)的嵌入式设备。
细节:
- IPv4 子网:192.168.150.0/24
- 发射机IP:192.168.150.2
- 接收方IP:192.168.150.5
- 工作多播地址:224.3.29.71
- 无效的多播地址:239.255.0.3
接收方代码:
import socket
import struct
import sys
multicast_group = '224.3.29.71' # WORKS
#multicast_group = '239.255.0.3' # DOES NOT WORK. OF COURSE I AM CHANGING THE TX SIDE TO MATCH.
address = ('', 32768)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#allow other sockets to bind this port too
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# Bind to the server address
sock.bind(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 loop
while True:
print('\nwaiting to receive message', address[1], file=sys.stderr)
data, address = sock.recvfrom(1024)
print('received %s bytes from %s' % (len(data), address), file=sys.stderr)
print(data.decode(), file=sys.stderr)
我是否需要配置一些 Windows 设置以允许 239.*.*.* 多播组工作?