我遇到连接2台计算机的问题。第一个(发送器)运行在Windows上,第二个运行在linux上(ubuntu 16.04)。我通过本地在这两台机器之间通过QUdpSocket传输数据,但是当传输通过时,接收器什么都没捕获(我的意思是QT什么也没有显示)。为了检查,它是否正常工作,我使用了wireshark,它表明它是机器之间的连接,所有数据包都通过。但QT什么都没有!我使用了关于接收多播数据包的简单qt示例。我做错了什么?
在2台Windows机器上,所有工作都正常。
我的IP是192.168.1.1 和网络掩码255.255.255.0
我已添加此代码,请参见下文。我还添加了wireshark版画屏幕。
#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()));
}
}