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

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 4 Posters 9.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.
  • J.HilkJ J.Hilk

    Hier a little helper, code is untested, so you'll may have to modify /bugfix it:

    asynchronus and threaded.

    //main.cpp
    
    #include <QApplication>
    #include <QThread>
    #include "client.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QThread *thread = new QThread ();
        client *m_client = new client();
        m_client->moveToThread(thread);
        QObject::connect(thread, &QThread::started, m_client, &client::startconnection);
        QObject::connect(thread, &QThread::finished, m_client, &client::deleteLater);
        QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
        thread->start();
    
        return a.exec();
    }
    
    
    #include "client.h"
    
    client::client(QObject *parent):QObject(parent)
    {
    
    }
    void client::startconnection()
    {
        QTcpSocket client_socket = new QTcpSocket(this);
        
        connect(client_socket, &QTcpSocket::bytesWritten, this, &client::BytesWritten);
        connect(client_socket, &QTcpSocket::readyRead, this, &client::ReadyRead);
        
        connect(client_socket,SIGNAL(connected()),this,SLOT(connection()));
        connect(client_socket,SIGNAL(disconnected()),this,SLOT(disconnection()));
        
        client_socket->connectToHost("127.0.0.1",100);
    }
    
    void client::BytesWritten(const quint64 &bytes){
        qDebug() << bytes << "Bytes written";
    }
    
    void client::ReadyRead(){
        qDebug() << Q_FUNC_INFO << client_socket->readAll();
    }
    
    void client::connection(){
        qDebug()<<"connection is successfull, now write test data";
        writedata("TestString");
    }
    
    void client::disconnection(){
        qDebug()<<"disconnected..";
    }
    void client::writedata(const QString &str){
        qDebug()<<"to write somthing";
        client_socket->write(str.c_str());
    }
    
    J Offline
    J Offline
    JadeN001
    wrote on last edited by
    #16

    @J.Hilk thnaks , after doing some changes to this code , I no longer see those warnings in client code...but client doesn't read untill I write something(using cin.readline() function). Is there a way through which client doesn't wait to write something and reads the message from other client, in short it doesn't get blocked in writing.

    J.HilkJ 1 Reply Last reply
    0
    • J JadeN001

      @J.Hilk thnaks , after doing some changes to this code , I no longer see those warnings in client code...but client doesn't read untill I write something(using cin.readline() function). Is there a way through which client doesn't wait to write something and reads the message from other client, in short it doesn't get blocked in writing.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #17

      @JadeN001 ready read is emitted as soon as data is available at the socket.

      The fact that you only get data when you write something has nothing to do with qt, but with the server you connect to.

      Change the server you have, so that he sends without beeing triggered first. A Timer for example.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

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

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

        text=cin.readLine();

        Isn't that a blocking operation ? If so, the event loop is blocked until you get something from stdin.

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

        J 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @JadeN001 ready read is emitted as soon as data is available at the socket.

          The fact that you only get data when you write something has nothing to do with qt, but with the server you connect to.

          Change the server you have, so that he sends without beeing triggered first. A Timer for example.

          J Offline
          J Offline
          JadeN001
          wrote on last edited by
          #19

          @J.Hilk My server is just sending message when it arrives from client.i think at client side, i have to change the logic of calling write() function only when if there is no data in buffer for reading..I have tried to use bytesavailable().but it is not working.Do you have any idea about it?

          1 Reply Last reply
          0
          • SGaistS SGaist

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

            text=cin.readLine();

            Isn't that a blocking operation ? If so, the event loop is blocked until you get something from stdin.

            J Offline
            J Offline
            JadeN001
            wrote on last edited by
            #20

            @SGaist yes , that too is causing trouble. can you suggest any nonblocking userinput method? I'm really stuck at this point , SocketNotifier warning and this cin.readLine().

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

              It would rather be: do your blocking input in one thread and manage your network in another one.

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

              J 1 Reply Last reply
              0
              • SGaistS SGaist

                It would rather be: do your blocking input in one thread and manage your network in another one.

                J Offline
                J Offline
                JadeN001
                wrote on last edited by JadeN001
                #22

                @SGaist thanks very much
                I followed your suggestion and now it seems like , I'm getting this notification only when I write() something. @SGaist Is there a way to get the current SocketNotifier and handle it ?

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

                  You might be interested by this small example for reading std using Qt.

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

                  J 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    You might be interested by this small example for reading std using Qt.

                    J Offline
                    J Offline
                    JadeN001
                    wrote on last edited by
                    #24

                    @SGaist thanks very much
                    I tried this way too, but here I must pass the socketDescriptor as a parameter in QSocketNotifier. Its a bit confusing on client side how we can get socketDescriptor as in my knowledge we can only get socketdescriptor at serverside because of its Incommingconnection(). now, One more thing....Is it possible to get a warning of socket Notifier because reading and writing functions are being called at the same time ??
                    0_1524828143661_op.png

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

                      The socket notifier was only for stdin. As for your client side network connection, do everything in your dedicated worker object.

                      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