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. can't sending data outside qtcpsocket thread
QtWS25 Last Chance

can't sending data outside qtcpsocket thread

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 859 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.
  • R Offline
    R Offline
    rifky
    wrote on last edited by rifky
    #1

    Hi,
    anyone can help me about qtcpsocket with qthread after this .

    i want to send message over qtcpsocket, but i don't know why i got error in Application logs

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

    here is the readyRead() function :

    void UpdataServerThread::readyRead()
    {
        qDebug()<<updata_socket;
        qDebug()<<"connected_client_socket => "<<connected_client_socket;
        qDebug()<<"connected_client_ip => "<<connected_client_ip;
    
        QByteArray Data = updata_socket->readLine();
        QJsonDocument document = QJsonDocument::fromJson(Data.data());
        QJsonObject object = document.object();
    
        //qDebug()<<"current thread :"<<QThread::currentThread()<<" DEVICE::"<<updata_socket->peerAddress()<<"in : "<<Data.data();
    
        //authorize device
        if(Data.contains("devNumber")){
            connected_client_ip.insert(updata_socket->peerAddress(),object.value("devNumber").toString());
            QString str = "{\"msg\":\"Device registered successfully.\",\"timestamp\":"+QString::number(QDateTime::currentSecsSinceEpoch())+"}";
            QByteArray registered_device = str.toUtf8();
    
            ((QTcpSocket*)connected_client_socket.value(updata_socket->peerAddress()))->write(registered_device);
    
        }
        qDebug()<<"connected_client_ip::"<<connected_client_ip;
    }
    

    I've read topic similar topic like this, I haven't got solution.

    in this example of threadedfortuneserver , i saw write() in run() function is work as well as my code in run() function to ,

    how do I use write() outside run() function ?

    here is my code

    R 1 Reply Last reply
    0
    • R rifky

      Hi,
      anyone can help me about qtcpsocket with qthread after this .

      i want to send message over qtcpsocket, but i don't know why i got error in Application logs

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

      here is the readyRead() function :

      void UpdataServerThread::readyRead()
      {
          qDebug()<<updata_socket;
          qDebug()<<"connected_client_socket => "<<connected_client_socket;
          qDebug()<<"connected_client_ip => "<<connected_client_ip;
      
          QByteArray Data = updata_socket->readLine();
          QJsonDocument document = QJsonDocument::fromJson(Data.data());
          QJsonObject object = document.object();
      
          //qDebug()<<"current thread :"<<QThread::currentThread()<<" DEVICE::"<<updata_socket->peerAddress()<<"in : "<<Data.data();
      
          //authorize device
          if(Data.contains("devNumber")){
              connected_client_ip.insert(updata_socket->peerAddress(),object.value("devNumber").toString());
              QString str = "{\"msg\":\"Device registered successfully.\",\"timestamp\":"+QString::number(QDateTime::currentSecsSinceEpoch())+"}";
              QByteArray registered_device = str.toUtf8();
      
              ((QTcpSocket*)connected_client_socket.value(updata_socket->peerAddress()))->write(registered_device);
      
          }
          qDebug()<<"connected_client_ip::"<<connected_client_ip;
      }
      

      I've read topic similar topic like this, I haven't got solution.

      in this example of threadedfortuneserver , i saw write() in run() function is work as well as my code in run() function to ,

      how do I use write() outside run() function ?

      here is my code

      R Offline
      R Offline
      rifky
      wrote on last edited by
      #2

      @rifky said in QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread:

      solve with

      connect(updata_socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
      
      

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

      here is the readyRead() function :

      void UpdataServerThread::readyRead()
      {
          qDebug()<<updata_socket;
          qDebug()<<"connected_client_socket => "<<connected_client_socket;
          qDebug()<<"connected_client_ip => "<<connected_client_ip;
      
          QByteArray Data = updata_socket->readLine();
          QJsonDocument document = QJsonDocument::fromJson(Data.data());
          QJsonObject object = document.object();
      
          //qDebug()<<"current thread :"<<QThread::currentThread()<<" DEVICE::"<<updata_socket->peerAddress()<<"in : "<<Data.data();
      
          //authorize device
          if(Data.contains("devNumber")){
              connected_client_ip.insert(updata_socket->peerAddress(),object.value("devNumber").toString());
              QString str = "{\"msg\":\"Device registered successfully.\",\"timestamp\":"+QString::number(QDateTime::currentSecsSinceEpoch())+"}";
              QByteArray registered_device = str.toUtf8();
      
              ((QTcpSocket*)connected_client_socket.value(updata_socket->peerAddress()))->write(registered_device);
      
          }
          qDebug()<<"connected_client_ip::"<<connected_client_ip;
      }
      

      but i want to write outside ,

      void UpdataServerThread::sendCommand(QByteArray cmd)
      {
          qDebug()<<"current thread :@sendCommand"<<QThread::currentThread();
      
          //    qDebug()<<"connected_client_ip::"<<connected_client_ip;
          qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
          updata_socket->write("hi22");
      }
      

      how can i fix this code?

      KroMignonK 1 Reply Last reply
      0
      • R rifky

        @rifky said in QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread:

        solve with

        connect(updata_socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
        
        

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

        here is the readyRead() function :

        void UpdataServerThread::readyRead()
        {
            qDebug()<<updata_socket;
            qDebug()<<"connected_client_socket => "<<connected_client_socket;
            qDebug()<<"connected_client_ip => "<<connected_client_ip;
        
            QByteArray Data = updata_socket->readLine();
            QJsonDocument document = QJsonDocument::fromJson(Data.data());
            QJsonObject object = document.object();
        
            //qDebug()<<"current thread :"<<QThread::currentThread()<<" DEVICE::"<<updata_socket->peerAddress()<<"in : "<<Data.data();
        
            //authorize device
            if(Data.contains("devNumber")){
                connected_client_ip.insert(updata_socket->peerAddress(),object.value("devNumber").toString());
                QString str = "{\"msg\":\"Device registered successfully.\",\"timestamp\":"+QString::number(QDateTime::currentSecsSinceEpoch())+"}";
                QByteArray registered_device = str.toUtf8();
        
                ((QTcpSocket*)connected_client_socket.value(updata_socket->peerAddress()))->write(registered_device);
        
            }
            qDebug()<<"connected_client_ip::"<<connected_client_ip;
        }
        

        but i want to write outside ,

        void UpdataServerThread::sendCommand(QByteArray cmd)
        {
            qDebug()<<"current thread :@sendCommand"<<QThread::currentThread();
        
            //    qDebug()<<"connected_client_ip::"<<connected_client_ip;
            qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
            updata_socket->write("hi22");
        }
        

        how can i fix this code?

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

        @rifky said in can't sending data outside qtcpsocket thread:

        how can i fix this code?

        Perhaps you could use QTimer::singleShot():

        void UpdataServerThread::sendCommand(QByteArray cmd)
        {
            qDebug()<<"current thread :@sendCommand"<<QThread::currentThread();
        
            //    qDebug()<<"connected_client_ip::"<<connected_client_ip;
            qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
            if(QThread::currentThread() != updata_socket->thread())
               QTimer::singleShot(0, updata_socket, [updata_socket](){
                       updata_socket->write("hi22");
                   });
            else
                updata_socket->write("hi22");
        }
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        R 1 Reply Last reply
        1
        • KroMignonK KroMignon

          @rifky said in can't sending data outside qtcpsocket thread:

          how can i fix this code?

          Perhaps you could use QTimer::singleShot():

          void UpdataServerThread::sendCommand(QByteArray cmd)
          {
              qDebug()<<"current thread :@sendCommand"<<QThread::currentThread();
          
              //    qDebug()<<"connected_client_ip::"<<connected_client_ip;
              qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
              if(QThread::currentThread() != updata_socket->thread())
                 QTimer::singleShot(0, updata_socket, [updata_socket](){
                         updata_socket->write("hi22");
                     });
              else
                  updata_socket->write("hi22");
          }
          
          R Offline
          R Offline
          rifky
          wrote on last edited by rifky
          #4

          @KroMignon said in can't sending data outside qtcpsocket thread:

          Perhaps you could use QTimer::singleShot():

          void UpdataServerThread::sendCommand(QByteArray cmd)
          {
              qDebug()<<"current thread :@sendCommand"<<QThread::currentThread();
          
              //    qDebug()<<"connected_client_ip::"<<connected_client_ip;
              qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
              if(QThread::currentThread() != updata_socket->thread())
                 QTimer::singleShot(0, updata_socket, [updata_socket](){
                         updata_socket->write("hi22");
                     });
              else
                  updata_socket->write("hi22");
          }
          

          thx for response but i got error :

          updataserverthread.cpp:79:50: error: 'updata_socket' in capture list does not name a variable
          updataserverthread.cpp:79:66: error: expected body of lambda expression

          ../qtcpsocket_w_qthread/updataserverthread.cpp:79:50: error: 'updata_socket' in capture list does not name a variable
                      QTimer::singleShot(0, updata_socket,[updata_socket]()){
                                                           ^
          ../qtcpsocket_w_qthread/updataserverthread.cpp:79:66: error: expected body of lambda expression
                      QTimer::singleShot(0, updata_socket,[updata_socket]()){
                                                                           ^
          2 errors generated.
          
          KroMignonK 1 Reply Last reply
          0
          • R rifky

            @KroMignon said in can't sending data outside qtcpsocket thread:

            Perhaps you could use QTimer::singleShot():

            void UpdataServerThread::sendCommand(QByteArray cmd)
            {
                qDebug()<<"current thread :@sendCommand"<<QThread::currentThread();
            
                //    qDebug()<<"connected_client_ip::"<<connected_client_ip;
                qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
                if(QThread::currentThread() != updata_socket->thread())
                   QTimer::singleShot(0, updata_socket, [updata_socket](){
                           updata_socket->write("hi22");
                       });
                else
                    updata_socket->write("hi22");
            }
            

            thx for response but i got error :

            updataserverthread.cpp:79:50: error: 'updata_socket' in capture list does not name a variable
            updataserverthread.cpp:79:66: error: expected body of lambda expression

            ../qtcpsocket_w_qthread/updataserverthread.cpp:79:50: error: 'updata_socket' in capture list does not name a variable
                        QTimer::singleShot(0, updata_socket,[updata_socket]()){
                                                             ^
            ../qtcpsocket_w_qthread/updataserverthread.cpp:79:66: error: expected body of lambda expression
                        QTimer::singleShot(0, updata_socket,[updata_socket]()){
                                                                             ^
            2 errors generated.
            
            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @rifky said in can't sending data outside qtcpsocket thread:

            QTimer::singleShot(0, updata_socket,[updata_socket]()){

            You have one extra closing parenthesis!
            ==> should be QTimer::singleShot(0, updata_socket,[updata_socket]() {

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            R 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @rifky said in can't sending data outside qtcpsocket thread:

              QTimer::singleShot(0, updata_socket,[updata_socket]()){

              You have one extra closing parenthesis!
              ==> should be QTimer::singleShot(0, updata_socket,[updata_socket]() {

              R Offline
              R Offline
              rifky
              wrote on last edited by rifky
              #6

              @KroMignon said in can't sending data outside qtcpsocket thread:

              QTimer::singleShot(0, updata_socket,updata_socket {

                  qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
                  if(QThread::currentThread() != updata_socket->thread()){
                      QTimer::singleShot(0, updata_socket,[updata_socket]() {
                          updata_socket->write("hi22");
                      });
                  }else{
                      updata_socket->write("hi22");
                  }
              

              is it correct ?

              i still got error
              updataserverthread.cpp:79:46: error: 'updata_socket' in capture list does not name a variable
              Screen Shot 2020-08-14 at 18.38.08.png

              KroMignonK 1 Reply Last reply
              0
              • R rifky

                @KroMignon said in can't sending data outside qtcpsocket thread:

                QTimer::singleShot(0, updata_socket,updata_socket {

                    qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
                    if(QThread::currentThread() != updata_socket->thread()){
                        QTimer::singleShot(0, updata_socket,[updata_socket]() {
                            updata_socket->write("hi22");
                        });
                    }else{
                        updata_socket->write("hi22");
                    }
                

                is it correct ?

                i still got error
                updataserverthread.cpp:79:46: error: 'updata_socket' in capture list does not name a variable
                Screen Shot 2020-08-14 at 18.38.08.png

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #7

                @rifky said in can't sending data outside qtcpsocket thread:

                is it correct ?

                Why this question? Does it work or not?
                If you are not confident with lambda functions take a look at this ==> https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rifky
                  wrote on last edited by
                  #8

                  thx for help sir @KroMignon
                  i change to

                   QTimer::singleShot(0, updata_socket,[this]() {
                  

                  it's working

                  KroMignonK 1 Reply Last reply
                  0
                  • R rifky

                    thx for help sir @KroMignon
                    i change to

                     QTimer::singleShot(0, updata_socket,[this]() {
                    

                    it's working

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by KroMignon
                    #9

                    @rifky said in can't sending data outside qtcpsocket thread:

                    i change to
                    QTimer::singleShot(0, updata_socket,this {

                    it's working

                    Sorry my fault, I did not notice that updata_socket is not a local variable but a member of your class.
                    But this will not resolve your threading issue, I think you should change this to:

                    void UpdataServerThread::sendCommand(QByteArray cmd)
                    {
                        qDebug()<<"current thread :@sendCommand"<<QThread::currentThread();
                    
                        //    qDebug()<<"connected_client_ip::"<<connected_client_ip;
                        qDebug()<<"UpdataServerThread::sendCommand::"<<cmd;
                        if(QThread::currentThread() != updata_socket->thread())
                        {
                           auto localVariable = updata_socket
                           QTimer::singleShot(0, localVariable, [localVariable](){
                                   localVariable->write("hi22");
                               });
                        }
                        else
                            updata_socket->write("hi22");
                    }
                    

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rifky
                      wrote on last edited by
                      #10

                      @KroMignon no problem, thx for helping me, I really appreciate it

                      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