我有以下代码片段并在同一个VirtualBox VM中运行。我没有得到任何错误,但我什么都没得到。在wireshark中,我看到来自本地Adress的数据包到多播地址,但没有返回。
receiver.py
import PyQt5.QtNetwork as QTN
groupaddr = QTN.QHostAddress("239.255.43.22")
udp = QTN.QUdpSocket()
udp.setSocketOption(QTN.QAbstractSocket.MulticastLoopbackOption,1)
udp.setSocketOption(QTN.QAbstractSocket.MulticastTtlOption,32)
udp.bind(QTN.QHostAddress.AnyIPv4,54555,QTN.QUdpSocket.ShareAddress)
ni = QTN.QNetworkInterface()
udp.joinMulticastGroup(groupaddr,ni)
while True:
if udp.isReadable():
data,host,port = udp.readDatagram(udp.pendingDatagramSize())
print(data)
print(host)
udp.leaveMulticastGroup(groupaddr)
udp.close()
break
sender.py
import PyQt5.QtNetwork as QTN
groupaddr = QTN.QHostAddress("239.255.43.22")
udp = QTN.QUdpSocket()
udp.setSocketOption(QTN.QAbstractSocket.MulticastLoopbackOption,1)
udp.setSocketOption(QTN.QAbstractSocket.MulticastTtlOption,32)
ni = QTN.QNetworkInterface()
udp.bind(QTN.QHostAddress.AnyIPv4,54555,QTN.QUdpSocket.ShareAddress)
udp.setMulticastInterface(ni)
udp.joinMulticastGroup(groupaddr,ni)
udp.writeDatagram("test",groupaddr,54555)
答案 0 :(得分:1)
我是个白痴,应该让RTFM更加小心,使用hasPendingDatagrams()代替isReadable()解决它