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. QUdpSocket.readyread is not emitting when QCoreApplication not created from main thread.
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket.readyread is not emitting when QCoreApplication not created from main thread.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 491 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Diren
    wrote on last edited by VRonin
    #1

    Re: Why the main thread is needed for QCoreApplication?

    I wrote this code to create QCoreApplication from different thread. It is ok for many situations except udp send and QUdpSocket.readyread not working. Is it a bug or known issue?

    #include <QCoreApplication>
    #include <QNetworkDatagram>
    #include <QObject>
    #include <QUdpSocket>
    #include <thread>
    
    class UDPListener : public QObject
    {
    public:
        UDPListener(){}
        void startListening(quint16 port)
        {
            if (udpSocket.bind(port))
                connect(&udpSocket, &QUdpSocket::readyRead, this, &UDPListener::processPendingDatagrams);
        }    
        
    private:
        QUdpSocket udpSocket;
        
    public Q_SLOTS:
        void processPendingDatagrams()
        {
            while (udpSocket.state() == QUdpSocket::BoundState && udpSocket.hasPendingDatagrams())
            {
                QNetworkDatagram datagram = udpSocket.receiveDatagram();
                qDebug() << datagram.data();
            }
        }
    };
    
    void runsInOtherThread(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        a.exec();
    }
    
    void udpSend()
    {
        quint16 port = 5000;    
        QHostAddress pTargetAddress;
        pTargetAddress.setAddress("127.0.0.1");
        
        QUdpSocket test;
        test.writeDatagram("hello",5,pTargetAddress,port);
    }
    
    #define OTHERTHREAD
    
    int main(int argc, char *argv[])
    {
    #ifdef OTHERTHREAD
        std::thread t(runsInOtherThread, argc, argv);
    #else
        QCoreApplication a(argc, argv);
    #endif
        
        UDPListener u;
        u.startListening(5000);
        
        udpSend(); 
        std::thread t2(udpSend);
        
    #ifdef OTHERTHREAD
        t.join();
    #else
        a.exec();
    #endif
        return 0;
    }
    
    JonBJ 1 Reply Last reply
    0
    • D Diren

      Re: Why the main thread is needed for QCoreApplication?

      I wrote this code to create QCoreApplication from different thread. It is ok for many situations except udp send and QUdpSocket.readyread not working. Is it a bug or known issue?

      #include <QCoreApplication>
      #include <QNetworkDatagram>
      #include <QObject>
      #include <QUdpSocket>
      #include <thread>
      
      class UDPListener : public QObject
      {
      public:
          UDPListener(){}
          void startListening(quint16 port)
          {
              if (udpSocket.bind(port))
                  connect(&udpSocket, &QUdpSocket::readyRead, this, &UDPListener::processPendingDatagrams);
          }    
          
      private:
          QUdpSocket udpSocket;
          
      public Q_SLOTS:
          void processPendingDatagrams()
          {
              while (udpSocket.state() == QUdpSocket::BoundState && udpSocket.hasPendingDatagrams())
              {
                  QNetworkDatagram datagram = udpSocket.receiveDatagram();
                  qDebug() << datagram.data();
              }
          }
      };
      
      void runsInOtherThread(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          a.exec();
      }
      
      void udpSend()
      {
          quint16 port = 5000;    
          QHostAddress pTargetAddress;
          pTargetAddress.setAddress("127.0.0.1");
          
          QUdpSocket test;
          test.writeDatagram("hello",5,pTargetAddress,port);
      }
      
      #define OTHERTHREAD
      
      int main(int argc, char *argv[])
      {
      #ifdef OTHERTHREAD
          std::thread t(runsInOtherThread, argc, argv);
      #else
          QCoreApplication a(argc, argv);
      #endif
          
          UDPListener u;
          u.startListening(5000);
          
          udpSend(); 
          std::thread t2(udpSend);
          
      #ifdef OTHERTHREAD
          t.join();
      #else
          a.exec();
      #endif
          return 0;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Diren
      I don't know if this is relevant/the cause, but as we have said to another similar-ish post elsewhere: really think you should do thread work via QThread not std::thread if you want Qt things to work smoothly...?

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        It shouldn't matter what thread the QCoreApplication is but you still have to run an event loop in the thread that owns the UDPListener. The thread that should take care of executing the processPendingDatagrams is blocked by t.join(); in your example

        "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

        D 1 Reply Last reply
        3
        • VRoninV VRonin

          It shouldn't matter what thread the QCoreApplication is but you still have to run an event loop in the thread that owns the UDPListener. The thread that should take care of executing the processPendingDatagrams is blocked by t.join(); in your example

          D Offline
          D Offline
          Diren
          wrote on last edited by
          #4

          @VRonin
          When i move 2 lines to
          UDPListener u;
          u.startListening(5000);

          between
          QCoreApplication a(argc, argv);
          and
          a.exec();

          it works. I have to think about it. Thank you.

          Pablo J. RoginaP 1 Reply Last reply
          0
          • D Diren

            @VRonin
            When i move 2 lines to
            UDPListener u;
            u.startListening(5000);

            between
            QCoreApplication a(argc, argv);
            and
            a.exec();

            it works. I have to think about it. Thank you.

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @Diren said in QUdpSocket.readyread is not emitting when QCoreApplication not created from main thread.:

            it works.

            time to mark your post as solved then? thanks.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0

            • Login

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