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 from another thread

QSocketNotifier: socket notifiers cannot be enabled from another thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 297 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.
  • B Offline
    B Offline
    bxrs
    wrote on 25 Feb 2025, 08:46 last edited by
    #1

    main.cpp
    ds = new QThread();
    mythread = new readthread(tcpsocket);
    mythread->moveToThread(ds);
    connect(this,&readData::readstart,mythread,&readthread::readWork);
    ds->start();
    emit readstart(stopped,readCapacity);

    readthread.cpp
    readthread::readthread(QTcpSocket *s, QObject *parent)
    : QObject{parent}
    {
    tcpsocket = s;
    }
    void readthread::readWork(bool stopped, int readCapacity)
    {
    while(stoopped)
    {
    tcpsocket->write(commandData);
    if(tcpsocket->bytesAvailable() >= 1024)
    {
    time_wait = 0;
    Buf += tcpsocket->read(1024);
    }
    }
    }
    Error message when sending command:QSocketNotifier: socket notifiers cannot be enabled from another thread.The program runs normally, but after looping for a period of time, there may be unread data in the buffer, but read() cannot be reached, causing the program to get stuck in the read() function

    So, pass the socket into a sub thread through movtothread:
    main.cpp
    ds = new QThread();
    mythread = new readthread(tcpsocket);
    tcpsocket->moveToThread(ds);
    mythread->moveToThread(ds);
    connect(this,&readData::readstart,mythread,&readthread::readWork);
    ds->start();
    emit readstart(stopped,readCapacity);

    readthread.cpp
    readthread::readthread(QTcpSocket *s, QObject *parent)
    : QObject{parent}
    {
    tcpsocket = s;
    }
    void readthread::readWork(bool stopped, int readCapacity)
    {
    while(stoopped)
    {
    tcpsocket->write(commandData);
    if(tcpsocket->bytesAvailable() >= 1024)
    {
    time_wait = 0;
    Buf += tcpsocket->read(1024);
    }
    }
    }
    no error message when sending instructions,However, when reading data, it was found that the buffer could not receive the data, and the readable data was 0 (confirming that there was data sent back)May I ask how to solve this

    J 1 Reply Last reply 25 Feb 2025, 08:55
    0
    • B bxrs
      25 Feb 2025, 08:46

      main.cpp
      ds = new QThread();
      mythread = new readthread(tcpsocket);
      mythread->moveToThread(ds);
      connect(this,&readData::readstart,mythread,&readthread::readWork);
      ds->start();
      emit readstart(stopped,readCapacity);

      readthread.cpp
      readthread::readthread(QTcpSocket *s, QObject *parent)
      : QObject{parent}
      {
      tcpsocket = s;
      }
      void readthread::readWork(bool stopped, int readCapacity)
      {
      while(stoopped)
      {
      tcpsocket->write(commandData);
      if(tcpsocket->bytesAvailable() >= 1024)
      {
      time_wait = 0;
      Buf += tcpsocket->read(1024);
      }
      }
      }
      Error message when sending command:QSocketNotifier: socket notifiers cannot be enabled from another thread.The program runs normally, but after looping for a period of time, there may be unread data in the buffer, but read() cannot be reached, causing the program to get stuck in the read() function

      So, pass the socket into a sub thread through movtothread:
      main.cpp
      ds = new QThread();
      mythread = new readthread(tcpsocket);
      tcpsocket->moveToThread(ds);
      mythread->moveToThread(ds);
      connect(this,&readData::readstart,mythread,&readthread::readWork);
      ds->start();
      emit readstart(stopped,readCapacity);

      readthread.cpp
      readthread::readthread(QTcpSocket *s, QObject *parent)
      : QObject{parent}
      {
      tcpsocket = s;
      }
      void readthread::readWork(bool stopped, int readCapacity)
      {
      while(stoopped)
      {
      tcpsocket->write(commandData);
      if(tcpsocket->bytesAvailable() >= 1024)
      {
      time_wait = 0;
      Buf += tcpsocket->read(1024);
      }
      }
      }
      no error message when sending instructions,However, when reading data, it was found that the buffer could not receive the data, and the readable data was 0 (confirming that there was data sent back)May I ask how to solve this

      J Offline
      J Offline
      JonB
      wrote on 25 Feb 2025, 08:55 last edited by
      #2

      @bxrs
      Please use the forum's Code tags (</> toolbar button) when posting blocks of code.

      While you await a better answer. I can see this is not your actual code, which does not help. You write data to the socket and then immediately test for a response with >= 1024 bytes, without any delay. And if that is not there you keep resending the data, which does not sound helpful.

      B 1 Reply Last reply 25 Feb 2025, 09:04
      1
      • J JonB
        25 Feb 2025, 08:55

        @bxrs
        Please use the forum's Code tags (</> toolbar button) when posting blocks of code.

        While you await a better answer. I can see this is not your actual code, which does not help. You write data to the socket and then immediately test for a response with >= 1024 bytes, without any delay. And if that is not there you keep resending the data, which does not sound helpful.

        B Offline
        B Offline
        bxrs
        wrote on 25 Feb 2025, 09:04 last edited by
        #3

        @JonB while(0 < thr_readCapacity)
        {
        QApplication::processEvents();
        if(!thr_stopped)
        {
        tcpsocket->write(commandData);

                if(send_command(commandData))
                {
                    qDebug() << "EthernetRequest";
        
                    while(!thr_stopped)
                    {
                        while(thr_tcpsocket->bytesAvailable() < 1024)
                        {
                            QThread::msleep(10);
                            time_wait++;
                            if(time_wait >= 500)
                            {
                                qDebug() << "time_over";
                                thr_stopped = true;
                                break;
                            }
                        }
        
                        readlen = thr_tcpsocket->read(receivebytes.data(), 1024);
                        time_wait = 0;
        
                        if(readlen == 1024)//data.size() - count >= readlen
                        {
                            data.replace(count,readlen,receivebytes);
                            count += readlen;
                            readlen = 0;
                            qDebug() << "readcount" << count;
                        }
                        else
                        {
                            qDebug() << "read_less";
                            thr_stopped = true;
                            break;
                        }
        
                        if(count >= 65536)//count >= data.size()
                        {
                            if(thr_file->isOpen())
                            {
                                thr_file->write(data);
                            }
                            thr_readCapacity -= 64;
                            js++;
                            qDebug() << "bc" << js;
        //                    readvalue += 64;
        //                    progressDialog.setValue(readvalue);
                            count = 0;
                            break;
                        }
                    }
                }
                else
                {
                    thr_stopped = true;
                    break;
                }
            }
            else
            {
                break;
            }
        }
        
        qDebug() << "while_stop";
        thr_tcpsocket->moveToThread(QApplication::instance()->thread());
        emit compelet();
        

        }
        In fact, each time 64k data is received in a loop, when TCP leaves the main thread message QSocket Notify: socket notifiers cannot be enabled from another thread, There may be a situation where instructions can be sent, but data cannot be read from the buffer when receiving data. When TCP enters a sub thread, there are no errors when issuing instructions, but the read buffer cannot receive data

        jsulmJ 1 Reply Last reply 25 Feb 2025, 09:20
        0
        • B bxrs
          25 Feb 2025, 09:04

          @JonB while(0 < thr_readCapacity)
          {
          QApplication::processEvents();
          if(!thr_stopped)
          {
          tcpsocket->write(commandData);

                  if(send_command(commandData))
                  {
                      qDebug() << "EthernetRequest";
          
                      while(!thr_stopped)
                      {
                          while(thr_tcpsocket->bytesAvailable() < 1024)
                          {
                              QThread::msleep(10);
                              time_wait++;
                              if(time_wait >= 500)
                              {
                                  qDebug() << "time_over";
                                  thr_stopped = true;
                                  break;
                              }
                          }
          
                          readlen = thr_tcpsocket->read(receivebytes.data(), 1024);
                          time_wait = 0;
          
                          if(readlen == 1024)//data.size() - count >= readlen
                          {
                              data.replace(count,readlen,receivebytes);
                              count += readlen;
                              readlen = 0;
                              qDebug() << "readcount" << count;
                          }
                          else
                          {
                              qDebug() << "read_less";
                              thr_stopped = true;
                              break;
                          }
          
                          if(count >= 65536)//count >= data.size()
                          {
                              if(thr_file->isOpen())
                              {
                                  thr_file->write(data);
                              }
                              thr_readCapacity -= 64;
                              js++;
                              qDebug() << "bc" << js;
          //                    readvalue += 64;
          //                    progressDialog.setValue(readvalue);
                              count = 0;
                              break;
                          }
                      }
                  }
                  else
                  {
                      thr_stopped = true;
                      break;
                  }
              }
              else
              {
                  break;
              }
          }
          
          qDebug() << "while_stop";
          thr_tcpsocket->moveToThread(QApplication::instance()->thread());
          emit compelet();
          

          }
          In fact, each time 64k data is received in a loop, when TCP leaves the main thread message QSocket Notify: socket notifiers cannot be enabled from another thread, There may be a situation where instructions can be sent, but data cannot be read from the buffer when receiving data. When TCP enters a sub thread, there are no errors when issuing instructions, but the read buffer cannot receive data

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 25 Feb 2025, 09:20 last edited by jsulm
          #4

          @bxrs Why this dirty hacks with processEvents and sleep and what not if you could simply rely on assynchronous nature of Qt?! You also would not have to mess around with threads.

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

          B 1 Reply Last reply 25 Feb 2025, 09:29
          2
          • jsulmJ jsulm
            25 Feb 2025, 09:20

            @bxrs Why this dirty hacks with processEvents and sleep and what not if you could simply rely on assynchronous nature of Qt?! You also would not have to mess around with threads.

            B Offline
            B Offline
            bxrs
            wrote on 25 Feb 2025, 09:29 last edited by
            #5

            @jsulm QApplication::processEvents(); Will it cause the program to crash? If you want to modify the flag thr_stopped in another slot function while reading while while, but while still occupying the thread, how can you exit the loop? Thank you for your answer

            J 1 Reply Last reply 25 Feb 2025, 09:56
            0
            • B bxrs
              25 Feb 2025, 09:29

              @jsulm QApplication::processEvents(); Will it cause the program to crash? If you want to modify the flag thr_stopped in another slot function while reading while while, but while still occupying the thread, how can you exit the loop? Thank you for your answer

              J Offline
              J Offline
              JonB
              wrote on 25 Feb 2025, 09:56 last edited by
              #6

              @bxrs
              @jsulm is saying that there is not threading at all needed for this. Qt's QTcpSocket is asynchronous to start with. So why not get rid of all your threading stuff and use that as it is intended, it is neater and you won't have thread issues, whatever they are.

              B 1 Reply Last reply 25 Feb 2025, 10:06
              0
              • J JonB
                25 Feb 2025, 09:56

                @bxrs
                @jsulm is saying that there is not threading at all needed for this. Qt's QTcpSocket is asynchronous to start with. So why not get rid of all your threading stuff and use that as it is intended, it is neater and you won't have thread issues, whatever they are.

                B Offline
                B Offline
                bxrs
                wrote on 25 Feb 2025, 10:06 last edited by
                #7

                @JonB Thank you for your response.
                But without using threads, won't the while loop cause the program to block?

                J 1 Reply Last reply 25 Feb 2025, 10:17
                0
                • B bxrs
                  25 Feb 2025, 10:06

                  @JonB Thank you for your response.
                  But without using threads, won't the while loop cause the program to block?

                  J Offline
                  J Offline
                  JonB
                  wrote on 25 Feb 2025, 10:17 last edited by JonB
                  #8

                  @bxrs
                  What "while" loop? You won't have one. No blocking, no waiting! The asynchronous way of doing things is: send your data to device, put a slot on readyRead(), accumulate whatever is received into a (member variable, so it persists) buffer (you must not assume one call will receive 1024 bytes or whatever, Qt only guarantees you will get 1 or more bytes when readyRead() is called), check to see if 1024 bytes or whatever for a "complete message". If so, remove the 1024/message bytes from the "pending" buffer, emit your own signal (like messageReceived(bytes)) with that data, attach a slot to do whatever on completed signal. That is how we do asynchronous stuff without blocking or using a thread.

                  1 Reply Last reply
                  4

                  1/8

                  25 Feb 2025, 08:46

                  • Login

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