Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved QUdpSocket working on Ubuntu but not WIndows10

    General and Desktop
    2
    3
    104
    Loading More Posts
    • 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.
    • K
      kioij last edited by kioij

      Hello everyone,

      After looking through the forum, I did not see a solution to this particular problem.
      I am having trouble with sending UDP packets through to Qt on Windows10.

      For sending a UDP packet, I'm using PacketSender from https://packetsender.com/.
      Using this program, TCP packets are picked up by wireshark but UDP packets are not, even with the same IP address and local port.

      For the code, QUdpSocket is running on a separate thread.
      It works on Ubuntu but the same code does not work in Windows 10.
      I disabled Windows Firewall and also added a new inbound rule to unblock the local port for UDP but this doesn't seem to solve the problem.

      My Qt code for receiving the UDP packets is:

      MySocket.h :

      class MyUDPSocket: public QThread
      {
          Q_OBJECT
      
      public:
          MyUDPSocket(moodycamel::BlockingReaderWriterQueue<QByteArray>& buffer);
      
          void run();
      
      public slots:
          void readPackets();
      
      private:
          QUdpSocket *udpSkt; 
          moodycamel::BlockingReaderWriterQueue<QByteArray>& queue; 
      };
      

      MySocket.cpp:

      MyUDPSocket::MyUDPSocket(moodycamel::BlockingReaderWriterQueue<QByteArray>& buffer)
          : queue(buffer)
      {
          moveToThread(this);
      }
      
      void MyUDPSocket::run() {
          udpSkt = new QUdpSocket(this);
          udpSkt->bind(QHostAddress::Any, 56666);
          connect(udpSkt, SIGNAL(readyRead()), this, SLOT(readPackets()));
          exec();
      }
      
      void MyUDPSocket::readPackets() {
          while (udpSkt->hasPendingDatagrams())  {
              QByteArray datagram;
              datagram.resize(udpSkt->pendingDatagramSize());
              QHostAddress sender;
              quint16 senderPort;
      
              udpSkt->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
      
              if(datagram.size() > 0) {
                  queue.enqueue(datagram);
              }
          }
      }
      

      Thank you for your help.

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

        QThread is not the thread, it's a wrapper around the secondary thread living in the main thread. Slots of a QThread subclass are executed in the main thread.
        See https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply Reply Quote 2
        • K
          kioij last edited by kioij

          Thank you for the information @VRonin
          I applied it to my implementation.


          Did anybody have a problem with porting QUdpSockets to Windows10 from Linux?

          1 Reply Last reply Reply Quote 0
          • First post
            Last post