Problem reading payload with QUdpSocket
-
I need to read the payload of a UDP message transmitted by a server with IP 172.19.66.100 from port 50493, to another server with IP 239.9.66.100 to port 1000. With Wireshark I can read it, but using the QUdpSocket I am not able.
This is my code, that is 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"), 10000); //! [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"), 10000);
but I tried different solutions and none is really working. The only way I managed to read the message, is transmitting the message directly to my local IP, instead of the server IP, but it is not what I need...
How should I modify the code? -
You are listening on port 10000 for datagrams you claim to be sending to port 1000. Could be a typo in your post. BTW, binding to ports less than 1024 can require elevated privileges. You should check the return value from your bind() call.
The target address is in the multicast range. Are you trying receive multicast data? If so, you need to join the multicast group and any intervening routing needs to be in place.
-
Sorry, 10000 is just a typo mistake.
Anyway it is not a problem of authorizations, because as I wrote if I stream to my IP with the same port, it works fine.I am not expert of netweorks, so I have no idea if I am receiving in multicast, could you explain me, please?
-
Sorry, 10000 is just a typo mistake.
Anyway it is not a problem of authorizations, because as I wrote if I stream to my IP with the same port, it works fine.I am not expert of netweorks, so I have no idea if I am receiving in multicast, could you explain me, please?
@Mattia Why don't you add error handling like @ChrisW67 suggested?
Check bind() return value and if it returns false print https://doc.qt.io/qt-6/qabstractsocket.html#error or https://doc.qt.io/qt-6/qiodevice.html#errorString -
Hi, ff you want to observe the traffic between 2 other computers from a 3rd PC (this is sometimes called packet sniffing) you have to enable promiscuous mode
(this is whatWireshark does) -
@hskoglund , probably you are right, because if I use the pcap library and I enable the promiscuous mode, I am able to read the messages. But how should I enable it in QUdpSocket?
-
- try bind to 0.0.0.0
- disable any firewall, in Windows, turnoff firewall , linux, disable ufw.
- Check the ether pack settings, sometimes different ethernet MTU can cause strange problem, eg, a client of 1500 and Server is 1472
QUDPSocket in windows will miss some packs if default OS UDP buffersize is used. Add these reg paraments can solve pack-loss problem (need a reboot):
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\AFD\Parameters] "DefaultReceiveWindow"=dword:00100000 "FastSendDatagramThreshold"=dword:00002800 "DefaultSendWindow"=dword:00100000
Using paras above, a cheap CPU can also cache all packs from client even if in Signal-Slot mod (in respond to readyRead()).
-
Sorry, 10000 is just a typo mistake.
Anyway it is not a problem of authorizations, because as I wrote if I stream to my IP with the same port, it works fine.I am not expert of netweorks, so I have no idea if I am receiving in multicast, could you explain me, please?
@Mattia said in Problem reading payload with QUdpSocket:
I am not expert of netweorks, so I have no idea if I am receiving in multicast, could you explain me, please?
Any IPv4 address 224.0.0.0 to 239.255.255.255 is a multicast address (aka multicast group). Senders are intending that there will be many receivers; any program that joins the multicast group will receive the packets.
The address 239.9.66.100 is the address of a multicast group. A host should not have a multicast address assigned to any of it interfaces; it should have a non-multicast address that it uses for traffic. When a host joins the multicast group the upstream switches/routers will conspire to deliver the selected group traffic to the host.