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. Receiving data from QTcpSocket
Forum Updated to NodeBB v4.3 + New Features

Receiving data from QTcpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
30 Posts 6 Posters 4.9k Views 1 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.
  • B Bonnie

    @SPlatten
    Wait, you are saying bytesWritten is never emitted in the sending side (the client)?
    Then we should start from the sending part...

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #16

    @Bonnie , I'm happy to try anything, apart from the initial connection, no data seems to be getting sent.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SPlattenS SPlatten

      @KroMignon , I'm not calling socket->write(), I'm using QDataStream where I have an instance call mdsOut and sending data to that:

      mdsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact);
      

      I'm not seeing anything written from the onBytesWritten slot. It does appear that the waitForConnection is blocking and never exits.

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

      @SPlatten said in Receiving data from QTcpSocket:

      I'm not calling socket->write(), I'm using QDataStream where I have an instance call mdsOut and sending data to that:

      I would suggest you to change this to:

      QByteArray block;
      QDataStream  mdsOut(&block, QIODevice::WriteOnly);
      mdsOut.setVersion(QDataStream::Qt_5_14);
      //Build the module start-up message
      QJsonObject objMsg;
      objMsg.insert(clsJSON::mscszModule, clsModHelper::msszTitle);
      objMsg.insert(clsJSON::mscszMsgType, clsJSON::mscszCmdInitial);
      objMsg.insert(clsJSON::mscszPort, clsModHelper::muint16ModulePort);
      mdsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact);
      
      write(block);
      
      

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

      SPlattenS 1 Reply Last reply
      0
      • KroMignonK KroMignon

        @SPlatten said in Receiving data from QTcpSocket:

        I'm not calling socket->write(), I'm using QDataStream where I have an instance call mdsOut and sending data to that:

        I would suggest you to change this to:

        QByteArray block;
        QDataStream  mdsOut(&block, QIODevice::WriteOnly);
        mdsOut.setVersion(QDataStream::Qt_5_14);
        //Build the module start-up message
        QJsonObject objMsg;
        objMsg.insert(clsJSON::mscszModule, clsModHelper::msszTitle);
        objMsg.insert(clsJSON::mscszMsgType, clsJSON::mscszCmdInitial);
        objMsg.insert(clsJSON::mscszPort, clsModHelper::muint16ModulePort);
        mdsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact);
        
        write(block);
        
        
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #18

        @KroMignon said in Receiving data from QTcpSocket:

        QByteArray block;

        Whats wrong with the way I did it? Thats what all the examples I've seen are doing?

        Kind Regards,
        Sy

        KroMignonK 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @KroMignon said in Receiving data from QTcpSocket:

          QByteArray block;

          Whats wrong with the way I did it? Thats what all the examples I've seen are doing?

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

          @SPlatten said in Receiving data from QTcpSocket:

          Whats wrong with the way I did it? Thats what all the examples I've seen are doing?

          I don't know if there is something wrong, but this way you can check what and when something is written on TCP socket.
          Which is what we want to achieve ==> be sure TCP client is sending something to server.

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

          SPlattenS 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @SPlatten said in Receiving data from QTcpSocket:

            Whats wrong with the way I did it? Thats what all the examples I've seen are doing?

            I don't know if there is something wrong, but this way you can check what and when something is written on TCP socket.
            Which is what we want to achieve ==> be sure TCP client is sending something to server.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #20

            @KroMignon , right now it appears that the application is never getting past waitForConnected, even with a 20 second timeout.

            Kind Regards,
            Sy

            KroMignonK 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @KroMignon , right now it appears that the application is never getting past waitForConnected, even with a 20 second timeout.

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

              @SPlatten said in Receiving data from QTcpSocket:

              right now it appears that the application is never getting past waitForConnected, even with a 20 second timeout.

              Please don't be hurt, but I am not a friend of doing synchronous/blocking calls with Qt. Qt base design is asynchronous.
              I prefer using signals/slots mechanism and I am using connected(), disconnected() , bytesWritten(qint64) and readyRead() signals.

              My application needs to run on Android, Linux and Windows and waitForConnected has issues with Windows, from documentation:

              Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows.

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

              SPlattenS 1 Reply Last reply
              1
              • KroMignonK KroMignon

                @SPlatten said in Receiving data from QTcpSocket:

                right now it appears that the application is never getting past waitForConnected, even with a 20 second timeout.

                Please don't be hurt, but I am not a friend of doing synchronous/blocking calls with Qt. Qt base design is asynchronous.
                I prefer using signals/slots mechanism and I am using connected(), disconnected() , bytesWritten(qint64) and readyRead() signals.

                My application needs to run on Android, Linux and Windows and waitForConnected has issues with Windows, from documentation:

                Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by SPlatten
                #22

                @KroMignon , I'm happy to change, the problem is there are so many incomplete and what seem poor examples online.

                Could the problem be because I am using both synchronous and asynchronous methods in my application?

                In the class constructor:

                    connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
                    connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
                    connect(this, SIGNAL(readyRead()), this, SLOT(onDataIn()));
                    connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
                

                And then in the onConnect slot:

                    qdbg() << clsModHelper::mscpszHost << muint16XMLMPAMport;
                    connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
                        
                    if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) {
                        emit terminateModule();
                    }
                

                Kind Regards,
                Sy

                KroMignonK JonBJ 2 Replies Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #23

                  @SPlatten said in Receiving data from QTcpSocket:

                  the problem is there are so many incomplete and what seem poor examples online.

                  https://doc.qt.io/qt-5/qtcpsocket.html#details - not enough examples there? And if not - write a good example, provide a patch for Qt and we will happily review it...

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

                  SPlattenS 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @SPlatten said in Receiving data from QTcpSocket:

                    the problem is there are so many incomplete and what seem poor examples online.

                    https://doc.qt.io/qt-5/qtcpsocket.html#details - not enough examples there? And if not - write a good example, provide a patch for Qt and we will happily review it...

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #24

                    @Christian-Ehrlicher , when I have a working example I might just do that.

                    Kind Regards,
                    Sy

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

                      The Fortune Server/Client example is doing exactly what you try.

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

                      SPlattenS 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        The Fortune Server/Client example is doing exactly what you try.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #26

                        @Christian-Ehrlicher How do I download the Fortune Server and Client examples?

                        Kind Regards,
                        Sy

                        jsulmJ JonBJ 2 Replies Last reply
                        0
                        • SPlattenS SPlatten

                          @Christian-Ehrlicher How do I download the Fortune Server and Client examples?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #27

                          @SPlatten said in Receiving data from QTcpSocket:

                          How do I download the Fortune Server and Client examples?

                          Examples are part of a Qt installation (if using Qt installer).

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

                          1 Reply Last reply
                          3
                          • SPlattenS SPlatten

                            @Christian-Ehrlicher How do I download the Fortune Server and Client examples?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #28

                            @SPlatten said in Receiving data from QTcpSocket:

                            Fortune Server and Client examples

                            You can also just visit the doc pages, the links are at the end of the pages: https://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html & https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html

                            1 Reply Last reply
                            3
                            • SPlattenS SPlatten

                              @KroMignon , I'm happy to change, the problem is there are so many incomplete and what seem poor examples online.

                              Could the problem be because I am using both synchronous and asynchronous methods in my application?

                              In the class constructor:

                                  connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
                                  connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
                                  connect(this, SIGNAL(readyRead()), this, SLOT(onDataIn()));
                                  connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
                              

                              And then in the onConnect slot:

                                  qdbg() << clsModHelper::mscpszHost << muint16XMLMPAMport;
                                  connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
                                      
                                  if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) {
                                      emit terminateModule();
                                  }
                              
                              KroMignonK Offline
                              KroMignonK Offline
                              KroMignon
                              wrote on last edited by KroMignon
                              #29

                              @SPlatten said in Receiving data from QTcpSocket:

                              And then in the onConnect slot:

                              qdbg() << clsModHelper::mscpszHost << muint16XMLMPAMport;
                              connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
                                  
                              if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) {
                                  emit terminateModule();
                              }
                              

                              That is your problem!
                              You are "bad mixing" synchronous and asynchronous!

                              OnConnected() will be called when TCP server has accepted to the connection.
                              So you can start writing to server in this slot.
                              No need to use waitForConnected()

                              ==> you only have to use connectToHost() to start connection request to TCP server.

                              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
                              4
                              • SPlattenS SPlatten

                                @KroMignon , I'm happy to change, the problem is there are so many incomplete and what seem poor examples online.

                                Could the problem be because I am using both synchronous and asynchronous methods in my application?

                                In the class constructor:

                                    connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
                                    connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
                                    connect(this, SIGNAL(readyRead()), this, SLOT(onDataIn()));
                                    connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
                                

                                And then in the onConnect slot:

                                    qdbg() << clsModHelper::mscpszHost << muint16XMLMPAMport;
                                    connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
                                        
                                    if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) {
                                        emit terminateModule();
                                    }
                                
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #30

                                @SPlatten said in Receiving data from QTcpSocket:

                                Could the problem be because I am using both synchronous and asynchronous methods in my application?

                                As @KroMignon says. Also I believe we have seen programs going wrong if they try to use the asynchronous signals at the same time as the synchronous waitFor..s, they get in each other's way IIRC.

                                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