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 Unsolved General and Desktop
33 Posts 5 Posters 11.0k 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.
  • SGaistS SGaist

    You have to transfer the descriptor to your pool and there in your run method create new sockets when descriptors are added.

    R Offline
    R Offline
    rktech
    wrote on last edited by
    #17

    @SGaist Should I add this part of code to my Pool::add() method? (Of course with editing it)

    QTcpSocket tcpSocket;
    
        if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
            emit error(tcpSocket.error());
            return;
        }
    
    1 Reply Last reply
    0
    • SGaistS SGaist

      You have to transfer the descriptor to your pool and there in your run method create new sockets when descriptors are added.

      R Offline
      R Offline
      rktech
      wrote on last edited by
      #18

      @SGaist I have (probably) successfully rewritten the Pool add method, but I have another problem. When I add this:

      protected:
          void incomingConnection(qintptr socketDescriptor) override;
      

      to myserver.h I get this error:

      ...\myserver.h:35: Chyba: only virtual member functions can be marked 'override'
      

      (Chyba is Error in my local)
      What I am doing wrong?

      mrjjM 1 Reply Last reply
      0
      • R rktech

        @SGaist I have (probably) successfully rewritten the Pool add method, but I have another problem. When I add this:

        protected:
            void incomingConnection(qintptr socketDescriptor) override;
        

        to myserver.h I get this error:

        ...\myserver.h:35: Chyba: only virtual member functions can be marked 'override'
        

        (Chyba is Error in my local)
        What I am doing wrong?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #19

        Hi
        well it is virtual https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
        so does Pool inherit QTcpServer ?
        The error you get says "Hey, i dont see that funtion in base class, so you cant use override"

        R 2 Replies Last reply
        0
        • mrjjM mrjj

          Hi
          well it is virtual https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
          so does Pool inherit QTcpServer ?
          The error you get says "Hey, i dont see that funtion in base class, so you cant use override"

          R Offline
          R Offline
          rktech
          wrote on last edited by
          #20

          @mrjj No, the server is build under the myserver class

          R 1 Reply Last reply
          0
          • R rktech

            @mrjj No, the server is build under the myserver class

            R Offline
            R Offline
            rktech
            wrote on last edited by
            #21
            This post is deleted!
            1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              well it is virtual https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
              so does Pool inherit QTcpServer ?
              The error you get says "Hey, i dont see that funtion in base class, so you cant use override"

              R Offline
              R Offline
              rktech
              wrote on last edited by
              #22

              @mrjj the header of server look like:

              #ifndef MYSERVER_H
              #define MYSERVER_H
              
              #include <QObject>
              #include <QDebug>
              #include <QTcpServer>
              #include <QTcpSocket>
              #include <QString>
              #include <QFile>
              #include <QStandardPaths>
              #include <QThread>
              #include <QElapsedTimer>
              #include <pool.h>
              #include <QDateTime>
              class MyServer : public QObject
              {
                  Q_OBJECT
              public:
                  explicit MyServer(QObject *parent = nullptr);
              
              signals:
              
              public slots:
                  void newConnection();
                  void onReadyRead();
              
              private:
                  QTcpServer *server;
                  QList<QTcpSocket*>  _sockets;
                  QString a;
                  QString com;
                  QTcpSocket *socket;
                  QList<QTcpSocket*> sl;
              /*protected:
                  void incomingConnection(qintptr socketDescriptor) override;*/
              };
              
              #endif // MYSERVER_H
              
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #23

                Sorry but please learn C++ first. If you want to override a function from a class you should inherit from this class. So inherit from QTcpServer, override the function and do whatever you want to. This is basic c++ stuff...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                R 2 Replies Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  Sorry but please learn C++ first. If you want to override a function from a class you should inherit from this class. So inherit from QTcpServer, override the function and do whatever you want to. This is basic c++ stuff...

                  R Offline
                  R Offline
                  rktech
                  wrote on last edited by
                  #24

                  @Christian-Ehrlicher That's the thing, that I didn't notice. Thanks

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    Sorry but please learn C++ first. If you want to override a function from a class you should inherit from this class. So inherit from QTcpServer, override the function and do whatever you want to. This is basic c++ stuff...

                    R Offline
                    R Offline
                    rktech
                    wrote on last edited by
                    #25

                    @Christian-Ehrlicher But I don't know, how to edit it.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rktech
                      wrote on last edited by
                      #26

                      I have rewritten half of my project to solve the problem:
                      myserver.h

                      #ifndef MYSERVER_H
                      #define MYSERVER_H
                      #include <QStringList>
                      #include <QTcpServer>
                      #include <QDateTime>
                      class MyServer : public QTcpServer
                      {
                          Q_OBJECT
                      
                      public:
                          MyServer(QObject *parent = nullptr);
                      
                      protected:
                          void incomingConnection(qintptr socketDescriptor) override;
                      
                      private:
                          QStringList fortunes;
                      };
                      
                      #endif // MYSERVER_H
                      

                      myserver.cpp

                      #include "myserver.h"
                      #include "pool.h"
                      Pool p;
                      
                      MyServer::MyServer(QObject *parent)
                          : QTcpServer(parent)
                      {
                          p.start();
                      }
                      
                      void MyServer::incomingConnection(qintptr socketDescriptor)
                      {
                      QDateTime qdt;
                      p.add(socketDescriptor,qdt.time());
                      }
                      

                      pool.h

                      #ifndef POOL_H
                      #define POOL_H
                      #include<QObject>
                      #include<QThread>
                      #include<QTcpSocket>
                      #include<QDateTime>
                      #include<QElapsedTimer>
                      class Pool : public QThread
                      {
                          Q_OBJECT
                      
                      protected:
                          void run();
                      private:
                          struct conn{
                              QTcpSocket *s;
                              QTime qdt;
                          };
                          QList<conn> x;
                      public:
                          void add(int socketDescriptor, QTime qdt);
                      };
                      #endif // POOL_H
                      

                      pool.cpp

                      #include "pool.h"
                      #include <QElapsedTimer>
                      void Pool::run()
                      {
                      QElapsedTimer qet;
                      qet.start();
                      while (true) {
                          if(qet.elapsed()>=5000){
                              foreach (conn y, x) {
                                  if(y.s==nullptr){
                                      qDebug()<<"empty";
                                      continue;
                                  }
                                  y.s->write("test");
                                  y.s->flush();
                              }
                              qet.restart();
                          }
                      }
                      }
                      void Pool::add(int socketDescriptor, QTime qdt){
                      QTcpSocket* s = new QTcpSocket;
                      s->setSocketDescriptor(socketDescriptor);
                      s->write("1\n");
                      conn c;
                      c.s=s;
                      c.qdt=qdt;
                      x.append(c);
                      }
                      

                      But I have still the same error:

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

                      What's wrong?

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

                        Because your add method is called in the thread of the tcp server.

                        As I already told you, the socket creation must happen in the run function.

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

                        R 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          Because your add method is called in the thread of the tcp server.

                          As I already told you, the socket creation must happen in the run function.

                          R Offline
                          R Offline
                          rktech
                          wrote on last edited by
                          #28

                          @SGaist I have solved it. I am creating a list of descriptors in add() method and then rewriting it like this:
                          pool.cpp

                          void Pool::run()
                          {
                          QElapsedTimer qet;
                          qet.start();
                          while (true) {
                              if(!d.empty()){
                                  qDebug()<<"rewriting";
                                  for (int i = 0; i<d.size();i++) {
                                      desc des = d[i];
                                      conn con;
                                      QTcpSocket* soc = new QTcpSocket;
                                      soc->setSocketDescriptor(des.descriptor);
                                      con.s=soc;
                                      con.qdt=des.qt;
                                      d.removeAt(i);
                                      x.append(con);
                                  }
                                  qDebug()<<"done";
                              }
                              if(qet.elapsed()>=5000){
                                  foreach (conn y, x) {
                                      if(!y.s->isOpen()){
                                          qDebug()<<"empty";
                                          continue;
                                      }
                                      y.s->write("test");
                                      qDebug()<<"l";
                                      y.s->flush();
                                  }
                                  qet.restart();
                              }
                          }
                          }
                          void Pool::add(int socketDescriptor, QTime qdt){
                          desc de;
                          de.descriptor=socketDescriptor;
                          de.qt=qdt;
                          d.append(de);
                          }
                          

                          definitions of structs and lists in pool.h:

                            struct conn{
                                  QTcpSocket *s;
                                  QTime qdt;
                              };
                              struct desc{
                                  qintptr descriptor;
                                  QTime qt;
                              };
                              QList<conn> x;
                              QList<desc> d;
                          
                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #29

                            You are missing one important thing: you are accessing a resource from two different threads. You should protect its access accordingly.

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

                            R 1 Reply Last reply
                            1
                            • SGaistS SGaist

                              You are missing one important thing: you are accessing a resource from two different threads. You should protect its access accordingly.

                              R Offline
                              R Offline
                              rktech
                              wrote on last edited by
                              #30

                              @SGaist In my last code it is fixed, right? If no, how to protect it correctly?

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

                                When using a new technology it's always a good idea to start by reading the documentation.

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

                                R 1 Reply Last reply
                                1
                                • SGaistS SGaist

                                  When using a new technology it's always a good idea to start by reading the documentation.

                                  R Offline
                                  R Offline
                                  rktech
                                  wrote on last edited by
                                  #32

                                  @SGaist I have read it, but I didn't catch what should I edit in my code.

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

                                    Did you read the Synchronizing Threads chapter linked in it ?

                                    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
                                    1

                                    • Login

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