Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem reading payload with QUdpSocket
Forum Update on Monday, May 27th 2025

Problem reading payload with QUdpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 340 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mattia
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mattia
        wrote on last edited by
        #3

        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?

        jsulmJ C 2 Replies Last reply
        0
        • M Mattia

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            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)

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Mattia
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              0
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #7

                Promiscuous mode cannot be enabled using QUdpSocket as far as I know.
                Best bet is probably to link with the pcab library (which as you say, can do it) and call it from you Qt code.

                1 Reply Last reply
                1
                • goldenhawkingG Offline
                  goldenhawkingG Offline
                  goldenhawking
                  wrote on last edited by goldenhawking
                  #8
                  1. try bind to 0.0.0.0
                  2. disable any firewall, in Windows, turnoff firewall , linux, disable ufw.
                  3. 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()).

                  Qt is the best C++ framework I've ever met.

                  1 Reply Last reply
                  0
                  • M Mattia

                    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?

                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved