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 5.0k 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.
  • SPlattenS SPlatten

    I have written a class derived from QTcpSocket, I connect to a server:

        connectToHost("localhost", 8123);
    
        if ( waitForConnected(20000) != true ) {
            emit terminateModule();
        }
        //Associate this TCP socket with the output data stream
        mdsOut.setDevice(this);
        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);
    

    Using the debugger I can see the data is sent and on the application that will receive the data, I have a class derived from QTcpServer in its constructor:

    listen(QHostAddress::Any, 8123);
    

    I implement the incomingConnection slot:

    void clsSocketServer::incomingConnection(qintptr sfd) {
        qDebug() << "incomingConnection";
        clsSocketClient* pSckClient = new clsSocketClient(this);
        pSckClient->setSocketDescriptor(sfd);
        connect(pSckClient, &QTcpSocket::disconnected, pSckClient, &clsSocketClient::onDisconnected);
        connect(pSckClient, &QTcpSocket::readyRead, pSckClient, &clsSocketClient::onReadyClient);
    }
    

    And I can see this is getting called, but there is no activity in the onReadyClient slot:

    void clsSocketClient::onReadyClient() {
        qDebug() << "onReadyClient";
    }
    

    I must be missing something very fundamental ?

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

    @SPlatten "Note: If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection()."
    https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection

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

    SPlattenS 1 Reply Last reply
    2
    • jsulmJ jsulm

      @SPlatten "Note: If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection()."
      https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection

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

      @jsulm , I've added this and rebuilt, no change, I think I'm missing the incoming data stream which I'm looking at now.

      Kind Regards,
      Sy

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

        What is mdsOut and are you sure you're sending the data out (e.g. by calling flush() - and check it's return value)

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

        SPlattenS 2 Replies Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          What is mdsOut and are you sure you're sending the data out (e.g. by calling flush() - and check it's return value)

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

          @Christian-Ehrlicher , I haven't called flush...I haven't seen that in any samples I've seen either. I'll give it a try.

          mdsOut is an instance of QDataStream.

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            What is mdsOut and are you sure you're sending the data out (e.g. by calling flush() - and check it's return value)

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

            @Christian-Ehrlicher , I added called to flush after sending, no difference.

            Kind Regards,
            Sy

            KroMignonK 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @Christian-Ehrlicher , I added called to flush after sending, no difference.

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

              @SPlatten said in Receiving data from QTcpSocket:

              I added called to flush after sending, no difference.

              And is the signal bytesWritten(qint64) emitted by the socket?
              You can also try to remove TCP delay on client tcp socket with socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);

              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 added called to flush after sending, no difference.

                And is the signal bytesWritten(qint64) emitted by the socket?
                You can also try to remove TCP delay on client tcp socket with socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);

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

                @KroMignon , thank you, I've added:

                connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
                

                To the constructor of my class which is derived from QTcpSocket. The slot:

                void clsModHelper::onBytesWritten(qint64 int64Written) {
                    qDebug() << "clsModHelper::onBytesWritten:" << int64Written;
                }
                

                I'm not seeing anything when the data is written. The slot isn't getting called at all.

                Kind Regards,
                Sy

                KroMignonK B 2 Replies Last reply
                0
                • B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by
                  #9

                  Maybe the data is already received before you connect the signal.
                  How about print pSckClient->bytesAvailable() in incomingConnection?

                  SPlattenS 1 Reply Last reply
                  0
                  • B Bonnie

                    Maybe the data is already received before you connect the signal.
                    How about print pSckClient->bytesAvailable() in incomingConnection?

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

                    @Bonnie , added that:

                    void clsSocketServer::incomingConnection(qintptr sfd) {
                        qDebug() << "incomingConnection";
                        clsSocketClient* pSckClient = new clsSocketClient(sfd, this);
                        addPendingConnection(pSckClient);
                        qDebug() << pSckClient->bytesAvailable();
                    }
                    

                    I see:

                    D00000000000000000020:incomingConnection
                    D00000000000000000021:0
                    

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @KroMignon , thank you, I've added:

                      connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
                      

                      To the constructor of my class which is derived from QTcpSocket. The slot:

                      void clsModHelper::onBytesWritten(qint64 int64Written) {
                          qDebug() << "clsModHelper::onBytesWritten:" << int64Written;
                      }
                      

                      I'm not seeing anything when the data is written. The slot isn't getting called at all.

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

                      @SPlatten said in Receiving data from QTcpSocket:

                      I'm not seeing anything when the data is written. The slot isn't getting called at all.

                      I am a little lost.
                      Just to be sure a understand what you have done:

                      • the TCP accept did work
                      • you have create a new tcp socket to handle connection with client called pSckClient
                      • you have send data on pSckClient to the TCP client
                      • your TCP client does not receive anything.

                      Is this correct?

                      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 seeing anything when the data is written. The slot isn't getting called at all.

                        I am a little lost.
                        Just to be sure a understand what you have done:

                        • the TCP accept did work
                        • you have create a new tcp socket to handle connection with client called pSckClient
                        • you have send data on pSckClient to the TCP client
                        • your TCP client does not receive anything.

                        Is this correct?

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

                        @KroMignon , The incomingConnection is getting called, in this slot I create a new client object using the descriptor parameter passed to the incomingConnection slot.

                        Data is sent from by the client application but I'm not seeing anything on the receiving application which is listening to localhost and port 8123.

                        Kind Regards,
                        Sy

                        KroMignonK 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @KroMignon , thank you, I've added:

                          connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
                          

                          To the constructor of my class which is derived from QTcpSocket. The slot:

                          void clsModHelper::onBytesWritten(qint64 int64Written) {
                              qDebug() << "clsModHelper::onBytesWritten:" << int64Written;
                          }
                          

                          I'm not seeing anything when the data is written. The slot isn't getting called at all.

                          B Offline
                          B Offline
                          Bonnie
                          wrote on last edited by
                          #13

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

                          SPlattenS 1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            @KroMignon , The incomingConnection is getting called, in this slot I create a new client object using the descriptor parameter passed to the incomingConnection slot.

                            Data is sent from by the client application but I'm not seeing anything on the receiving application which is listening to localhost and port 8123.

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

                            @SPlatten said in Receiving data from QTcpSocket:

                            The incomingConnection is getting called, in this slot I create a new client object using the descriptor parameter passed to the incomingConnection slot.
                            Data is sent from by the client application but I'm not seeing anything on the receiving application which is listening to localhost and port 8123.

                            Okay, let's continue step by step:

                            • what is the return value of socket->write() from the client application? Do you really write something on TCP socket?
                            • is bytesWritten(qint64) emitted on client side?
                            • are you sure the event loop of the tread used by the socket is not locked? For example on server side waitForConnected() locks the event loop. The event loop have to be called to emit the bytesWritten(qint64) signal

                            Same on server side:

                            • are you sure the event loop used by the connection socket in not locked

                            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:

                              The incomingConnection is getting called, in this slot I create a new client object using the descriptor parameter passed to the incomingConnection slot.
                              Data is sent from by the client application but I'm not seeing anything on the receiving application which is listening to localhost and port 8123.

                              Okay, let's continue step by step:

                              • what is the return value of socket->write() from the client application? Do you really write something on TCP socket?
                              • is bytesWritten(qint64) emitted on client side?
                              • are you sure the event loop of the tread used by the socket is not locked? For example on server side waitForConnected() locks the event loop. The event loop have to be called to emit the bytesWritten(qint64) signal

                              Same on server side:

                              • are you sure the event loop used by the connection socket in not locked
                              SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by
                              #15

                              @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.

                              Kind Regards,
                              Sy

                              KroMignonK 1 Reply Last reply
                              0
                              • 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

                                          • Login

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