我正在嘗試讓 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.*.*.* 多播群組工作?