1

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?

asked Mar 14, 2025 at 3:23
3
  • 1
    You need to bind to the interface on that machine, not to the multicast IP. So, for example, bind to "0.0.0.0". You then use setsockopt to join the multicast group. It's not really necessary to use multicast with UDP. Why are you doing that? Commented Mar 14, 2025 at 5:40
  • 1
    You need to use joinMulticastGroup(). Commented Mar 14, 2025 at 6:29
  • 1
    This is a basic misunderstanding of multicast: the switch closest to you will only forward multicast packets that you explicitly subscribed to. See @kiner_shah 's comment above. Commented Mar 14, 2025 at 7:43

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.