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 10.8k 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.
  • R Offline
    R Offline
    rktech
    wrote on last edited by
    #1

    I have this code:
    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(QTcpSocket *s, 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) {
                y.s->write("test");
                y.s->flush();
            }
            qet.restart();
        }
    }
    }
    void Pool::add(QTcpSocket *s, QTime qdt){
    conn c;
    c.s=s;
    c.qdt=qdt;
    x.append(c);
    }
    

    part of myserver.h:

    #include "myserver.h"
    Pool p;
    MyServer::MyServer(QObject *parent) :
        QObject(parent)
    {
        server = new QTcpServer(this);
        p.start();
        connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
    
        if(!server->listen(QHostAddress::Any, 1234))
        {
            qDebug() << "Server could not start!";
        }
        else
        {
            qDebug() << "Server started!";
        }
    }
    
    void MyServer::newConnection()
    {
        QTcpSocket *socket = server->nextPendingConnection();
        connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
        socket->write("hello client\r\n");
        socket->flush();
        socket->waitForBytesWritten(3000);
        QDateTime qdt;
        p.add(socket, qdt.time());
    }
    

    And I get this error:
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
    The code itself works, but I don't know, why I am getting this error/warning.

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

      Hi,

      Based on your code, you are moving socket between threads which you should not.

      See the threaded fortune server example.

      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
      2
      • R Offline
        R Offline
        rktech
        wrote on last edited by rktech
        #3

        I have read that, but I don't know how to fix it. Is it only possible to remove this warning?

        jsulmJ 1 Reply Last reply
        0
        • R rktech

          I have read that, but I don't know how to fix it. Is it only possible to remove this warning?

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Is it only possible to remove this warning?

          You should fix the issue instead of removing the warning!
          Did you check the example @SGaist gave you?

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

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

            @jsulm As I have written, I have read it, but I don't know, how to fix my code.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              but I don't know, how to fix my code.

              As shown in the example pass the socket fd instead the object to your thread. You have to override incomingConnection() as shown there.

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

              1 Reply Last reply
              2
              • R Offline
                R Offline
                rktech
                wrote on last edited by
                #7

                @Christian-Ehrlicher I have read the example like three-times, but I still don't know how to fix it. Can you help me?

                Christian EhrlicherC 1 Reply Last reply
                0
                • R rktech

                  @Christian-Ehrlicher I have read the example like three-times, but I still don't know how to fix it. Can you help me?

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  but I still don't know how to fix it.

                  You have to override incomingConnection() as shown there.

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

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

                    @Christian-Ehrlicher said in QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread:

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

                    but I still don't know how to fix it.

                    You have to override incomingConnection() as shown there.

                    I have tried, but I don't know how to implement that into my code.

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

                      What exactly have you tried ?

                      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
                      0
                      • SGaistS SGaist

                        What exactly have you tried ?

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

                        @SGaist To add override to my code, but I don't know where should I place it...

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

                          @SGaist How can I send the connection from incomingConnection() to Pool?

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

                            @SGaist I have successfully overrided the incomingconnection(), but I don't know, how to pass it to the Pool.

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

                              You'll have to redesign your pool a bit to ensure that your sockets are created in the context of the run method of your QThread subclass.

                              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
                              0
                              • SGaistS SGaist

                                You'll have to redesign your pool a bit to ensure that your sockets are created in the context of the run method of your QThread subclass.

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

                                @SGaist I know, but how?

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

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

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

                                  R 2 Replies 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
                                    #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

                                          • Login

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