没有使用PYQT5 Multicast UDP接收数据报

时间:2014-05-12 09:48:41

标签: python python-3.x udp multicast pyqt5

我有以下代码片段并在同一个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)

1 个答案:

答案 0 :(得分:1)

我是个白痴,应该让RTFM更加小心,使用hasPendingDatagrams()代替isReadable()解决它