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. QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
Forum Updated to NodeBB v4.3 + New Features

QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 13.5k Views 2 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.
  • A Offline
    A Offline
    aminM
    wrote on last edited by aminM
    #1

    hi.
    excuse me. I am weak in English.
    I'm trying to write a program that reads UDP datagrams with separate thread.

    class NetRecieverThread : public QThread
    {
        Q_OBJECT
    public:
        QUdpSocket *m_Socket;
        int m_iPortNum;
        QString m_stIpAddress;
    
        NetRecieverThread(QObject *parent ,QString  sAddress ,int PortNum )
        {
            m_iPortNum = PortNum;
            m_stIpAddress = sAddress;
            m_Socket = new QUdpSocket (this);
            bool bl = m_Socket ->bind(QHostAddress(sAddress),PortNum);
            bl = 0;
    
            //connect(m_Socket ,SIGNAL(readyRead()),this,SLOT(readData()));
    
        }
    
        void run() Q_DECL_OVERRIDE
        {
            while(true)
            {
    
                QByteArray datagram;
                bool ReadyRead = false;
                while(!ReadyRead)
                    ReadyRead = m_Socket->waitForReadyRead(30000);
    
                datagram.resize(m_Socket->pendingDatagramSize());
    
                QHostAddress sender;
                quint16 senderPort;
    
    
               int iSize =  m_Socket->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort);
               if(iSize <=0)
                   return;
    
    
                char *data = datagram.data();
                 quint8 RecData = data[0];
                 qDebug() <<"\n \n\ NetRecieverThread : Rec Data "<<RecData<<"Thread Information : "<< QThread::currentThread()<<QThread::currentThreadId();
                emit resultReady(datagram);
    
            }
        }
    signals:
        void resultReady(QByteArray datagram);
    
    };
    
    //--------------------------------------------------------------------
    //--------------------------------------------------------------------
    
    class NetRecieveManager : public QObject
    {
        Q_OBJECT
         NetRecieverThread *tRec;
    public:
        NetRecieveManager()
        {
        }
    
    
    
        void startWorkInAThread()
        {
    
           tRec = new NetRecieverThread(0,"127.0.0.1",56071);
           connect(tRec, &NetRecieverThread::resultReady, this, &NetRecieveManager::handleResults);
    
          tRec->start();
    
        }
    private slots:
        void handleResults(QByteArray datagram)
        {
           char *ch  = datagram.data();
           quint8 RecData = ch[0];
           qDebug() <<"NetRecieveManager: Rec Data "<<RecData<<"Thread Information : "<< QThread::currentThread()<<QThread::currentThreadId()<<"\n \n ";
    
        }
    
    };
    
    //--------------------------------------------------------------------
    //--------------------------------------------------------------------
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        //------------------------------------------------------
        NetRecieveManager myob;
        QThread  NetMangerThread;
        NetMangerThread.setObjectName("NetMangerThread");
    
        myob.startWorkInAThread();
        myob.moveToThread(&NetMangerThread);
        NetMangerThread.start();
        //------------------------------------------------------
    
        return a.exec();
    }
    

    then output program is :

     QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    
     
     NetRecieverThread : Rec Data  208 Thread Information :  NetRecieverThread(0xb625c8) 0xe10
    NetRecieveManager: Rec Data  208 Thread Information :  QThread(0x12fedc, name = "NetMangerThread") 0xb38 
     
     
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    
     
     NetRecieverThread : Rec Data  209 Thread Information :  NetRecieverThread(0xb625c8) 0xe10
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    NetRecieveManager: Rec Data  209 Thread Information :  QThread(0x12fedc, name = "NetMangerThread") 0xb38 
     
     
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    
     
     NetRecieverThread : Rec Data  210 Thread Information :  NetRecieverThread(0xb625c8) 0xe10
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    NetRecieveManager: Rec Data  210 Thread Information :  QThread(0x12fedc, name = "NetMangerThread") 0xb38 
     
     
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    
     
     NetRecieverThread : Rec Data  211 Thread Information :  NetRecieverThread(0xb625c8) 0xe10
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    NetRecieveManager: Rec Data  211 Thread Information :  QThread(0x12fedc, name = "NetMangerThread") 0xb38 
     
     
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    
     
     NetRecieverThread : Rec Data  212 Thread Information :  NetRecieverThread(0xb625c8) 0xe10
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    NetRecieveManager: Rec Data  212 Thread Information :  QThread(0x12fedc, name = "NetMangerThread") 0xb38 
    
    

    everything is ok. both 2 threads work good. i dont know why get this message
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread

    Is the problem will be.
    thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The constructor of NetRecieverThread is run in the main thread so m_Socket is created in the main thread. On the other hand the run method is called in the thread managed by QThread that's why you get the message.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        The constructor of NetRecieverThread is run in the main thread so m_Socket is created in the main thread. On the other hand the run method is called in the thread managed by QThread that's why you get the message.

        A Offline
        A Offline
        aminM
        wrote on last edited by aminM
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          The constructor of NetRecieverThread is run in the main thread so m_Socket is created in the main thread. On the other hand the run method is called in the thread managed by QThread that's why you get the message.

          A Offline
          A Offline
          aminM
          wrote on last edited by
          #4

          @SGaist
          thanks SGait
          ok.
          i change code and move create m_socket to run function and solved problem.

              void run() Q_DECL_OVERRIDE
              {
                  m_Socket = new QUdpSocket (0);
                  bool bl = m_Socket ->bind(QHostAddress(m_stIpAddress),m_iPortNum);
               while(true)
               {
                 .
                 .
                 .
          
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Don't forget to delete the socket before the end of run otherwise you'll have a memory leak

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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