I need to read the payload of a UDP message transmitted by a server with IP 172.19.66.100
with port 50493
, to another server with IP 239.9.66.100
with port 1000
.
With Wireshark I can read it, but I am not able to when using QUdpSocket.
This is my code, based on the standard Qt example:
#include <QLabel>
#include <QPushButton>
#include <QUdpSocket>
#include <QVBoxLayout>
#include <QNetworkDatagram>
#include "receiver.h"
Receiver::Receiver(QWidget *parent)
: QWidget(parent)
{
statusLabel = new QLabel(tr("Listening for broadcasted messages"));
statusLabel->setWordWrap(true);
auto quitButton = new QPushButton(tr("&Quit"));
//! [0]
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress("239.9.66.100"), 1000);
//! [0]
//! [1]
connect(udpSocket, &QUdpSocket::readyRead,
this, &Receiver::processPendingDatagrams);
//! [1]
connect(quitButton, &QPushButton::clicked,
this, &Receiver::close);
auto buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);
auto mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("Broadcast Receiver"));
}
void Receiver::processPendingDatagrams()
{
//! [2]
while (udpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = udpSocket->receiveDatagram();
statusLabel->setText(QString(datagram.data().toHex()));
//! [2]
}
}
I guess the problem is in this line:
udpSocket->bind(QHostAddress("239.9.66.100"), 1000);
I tried different solutions but none is really working. The only way I managed to read the message, is by transmitting the message directly to my local IP, instead of the server IP, but it is not what I need.
How should I modify my code?