我在连接 2 台计算机时遇到问题。第一次(发射机)在 Windows 上运行,第二次在 Linux(ubuntu 16.04)上运行。我通过本地的这2台机器之间的QUdpSocket传输数据,但是当传输通过时,接收器什么也没有(我的意思是QT什么都没有显示)。为了检查,它是否工作正常,我使用了wireshark,它表明它是机器之间的连接,所有数据包都通过。但QT什么也没显示!我使用了关于接收多播数据包的简单 qt 示例。我做错了什么?
在 2 台 Windows 机器上一切正常。
我的 ip 是 192.168.1.1 和网络掩码 255.255.255.0
我已添加此代码,请参见下文。我还添加了wireshark printscreen。
#include <QtWidgets>
#include <QtNetwork>
#include "receiver.h"
Receiver::Receiver(QWidget *parent)
: QDialog(parent)
{
groupAddress = QHostAddress("226.1.1.1");//QHostAddress("239.255.43.21");
statusLabel = new QLabel(tr("Listening for multicasted messages"));
quitButton = new QPushButton(tr("&Quit"));
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::AnyIPv4, 50100,QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint );//udpSocket->bind(QHostAddress::AnyIPv4, 45454, );
//udpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption,QVariant(1));
//udpSocket->setMulticastInterface(QNetworkInterface::interfaceFromName(groupAddress.toString())); //getInterfaceByAddress()
udpSocket->joinMulticastGroup(groupAddress);
//bool flag = groupAddress.isMulticast();
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("Multicast Receiver"));
}
void Receiver::processPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
statusLabel->setText(tr("Received datagram: \"%1\"").arg(datagram.data()));
}
}