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. Listening and receiving data from a socket.
Forum Updated to NodeBB v4.3 + New Features

Listening and receiving data from a socket.

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 1.1k 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.
  • JonBJ JonB

    @SPlatten
    While you're waiting for someone who understands better than I....

    You are reading data into a QByteArray in a transaction. But the sender has not sent a QByteArray (has it?). So what do you expect to happen?

    EDIT Mine crossed with @Christian-Ehrlicher's. Let me try to hint briefly. To use <<, and transactions, the client must be sending QDataStream objects for you to receive them. Your client is not, it's just sending a stream of bytes.

    That means, all you can do is read whatever bytes happen to be there when readyRead is raised. And that could be anything from the whole string down to just 1 byte. As it stands --- without inventing some protocol for properly sending messages --- all you can do is keep accumulating received bytes until the final } arrives. Push bytes received into a member buffer. Exit the readyRead handler and allow it to be called again for further bytes. Append future bytes to the pending buffer. When it contains a } you have received the complete client "message".

    If the client were a Qt program, and it was using QDataStream to send a QByteArray for the message, then you could use socket >> QByteArray and a transaction. When bytes arrived the transaction would look to see whether the whole QByteArray was there. If not all bytes arrived yet, it would buffer those arrived and commitTransaction() returns false. When more bytes arrive, it would try appending them. When the whole array received, commitTransaction() will return true.

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

    @JonB , how the data is received is nothing to do with how it is sent, I can receive the same data as either a QString or array, the content is the same. I've also tested the same code using a QString, the result is the same.

    What I mean by this is that receiving data in a QByteArray or QString makes no difference, the content would be the same, the only difference is the way 0 (nulls) are handled.

    Kind Regards,
    Sy

    JonBJ KroMignonK 2 Replies Last reply
    0
    • SPlattenS SPlatten

      @JonB , how the data is received is nothing to do with how it is sent, I can receive the same data as either a QString or array, the content is the same. I've also tested the same code using a QString, the result is the same.

      What I mean by this is that receiving data in a QByteArray or QString makes no difference, the content would be the same, the only difference is the way 0 (nulls) are handled.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #7

      @SPlatten
      You are failing to understand. Nothing to do with QByteArray vs QString.

      how the data is received is nothing to do with how it is sent

      Not at all true, given that you are using QByteArray, and transactions. How it is sent totally does matter. Nothing other than client sending a QByteArray via a QDataStream will satisfy your server receiver code. You are thinking, somehow, that bytes sent without QDataStream can be received as QDataStream, which they can't.

      I have typed in a detailed explanation in an EDIT to my previous to try to help you. Until you understand that you are going to struggle, I urge you to read it carefully.... :)

      SPlattenS 1 Reply Last reply
      3
      • JonBJ JonB

        @SPlatten
        You are failing to understand. Nothing to do with QByteArray vs QString.

        how the data is received is nothing to do with how it is sent

        Not at all true, given that you are using QByteArray, and transactions. How it is sent totally does matter. Nothing other than client sending a QByteArray via a QDataStream will satisfy your server receiver code. You are thinking, somehow, that bytes sent without QDataStream can be received as QDataStream, which they can't.

        I have typed in a detailed explanation in an EDIT to my previous to try to help you. Until you understand that you are going to struggle, I urge you to read it carefully.... :)

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

        @JonB , It was my understanding which is obviously wrong that I could receive any data on the socket using a QDataStream, in my testing I'm trying to receive data from a browser which is a standard HTTP request.

        I'm now replacing the QDataStream receive data with:

        void clsListener::onDataIn() {
            QByteArray arybytMsg;
            while( mpClient->bytesAvailable() ) {
                QByteArray arybytMsg = mpClient->readAll();
                qdbg() << "HERE";
            }
        }
        

        Just to see if that resolves the issue.

        [Edit] This now works and I am receiving the data.

        Kind Regards,
        Sy

        Christian EhrlicherC JonBJ 2 Replies Last reply
        0
        • SPlattenS SPlatten

          @JonB , It was my understanding which is obviously wrong that I could receive any data on the socket using a QDataStream, in my testing I'm trying to receive data from a browser which is a standard HTTP request.

          I'm now replacing the QDataStream receive data with:

          void clsListener::onDataIn() {
              QByteArray arybytMsg;
              while( mpClient->bytesAvailable() ) {
                  QByteArray arybytMsg = mpClient->readAll();
                  qdbg() << "HERE";
              }
          }
          

          Just to see if that resolves the issue.

          [Edit] This now works and I am receiving the data.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @SPlatten said in Listening and receiving data from a socket.:

          It was my understanding which is obviously wrong

          It's all in the docs: "A data stream is a binary stream of encoded information..."
          It really helps (as already told you many times) to read the documentation of a class before using 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
          4
          • Christian EhrlicherC Christian Ehrlicher

            @SPlatten said in Listening and receiving data from a socket.:

            It was my understanding which is obviously wrong

            It's all in the docs: "A data stream is a binary stream of encoded information..."
            It really helps (as already told you many times) to read the documentation of a class before using it.

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

            @Christian-Ehrlicher , thank you, that just takes time which I don't feel I have.

            Kind Regards,
            Sy

            Christian EhrlicherC jsulmJ 2 Replies Last reply
            -2
            • SPlattenS SPlatten

              @Christian-Ehrlicher , thank you, that just takes time which I don't feel I have.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #11

              @SPlatten said in Listening and receiving data from a socket.:

              that just takes time which I don't feel I have.

              You would have solved the issue by yourself 17hours ago when you would have read the documentation.

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

                @JonB , It was my understanding which is obviously wrong that I could receive any data on the socket using a QDataStream, in my testing I'm trying to receive data from a browser which is a standard HTTP request.

                I'm now replacing the QDataStream receive data with:

                void clsListener::onDataIn() {
                    QByteArray arybytMsg;
                    while( mpClient->bytesAvailable() ) {
                        QByteArray arybytMsg = mpClient->readAll();
                        qdbg() << "HERE";
                    }
                }
                

                Just to see if that resolves the issue.

                [Edit] This now works and I am receiving the data.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #12

                @SPlatten
                This is getting better, but you are still assuming that one readyRead call to onDataIn allows you to write a loop on bytesAvailable() and that will accumulate all bytes sent. It won't. It may do because your message data is so small, but not necessarily, and not if the data gets larger. (Try sending a very long [e.g. many K? megabytes?] string!)

                I showed you the only correct algorithm. Save the data received into a persistent (e.g. member variable) buffer. Append to that as more arrives. Check if you have now received the terminating } from your sending "protocol". When you do, you have the complete message. Period.

                1 Reply Last reply
                1
                • SPlattenS SPlatten

                  @JonB , how the data is received is nothing to do with how it is sent, I can receive the same data as either a QString or array, the content is the same. I've also tested the same code using a QString, the result is the same.

                  What I mean by this is that receiving data in a QByteArray or QString makes no difference, the content would be the same, the only difference is the way 0 (nulls) are handled.

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

                  @SPlatten said in Listening and receiving data from a socket.:

                  how the data is received is nothing to do with how it is sent, I can receive the same data as either a QString or array, the content is the same. I've also tested the same code using a QString, the result is the same.

                  That's not true, you have to read data in the same way you sent them when you are using QDataStream.
                  As TCP is a stream socket, you can not be sure all data have been received, so startTransaction() in combination with commitTransaction() will simply reception side. When not all data are received, QDataStream will rollback the readed data and so you can retry reading on next received data chunk.

                  But to ensure this works, you have to read what you have transfered, in the same order.

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

                    @Christian-Ehrlicher , thank you, that just takes time which I don't feel I have.

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

                    @SPlatten said in Listening and receiving data from a socket.:

                    that just takes time which I don't feel I have

                    But you have time to ask in a forum and wait for an answer?!

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

                    SPlattenS 1 Reply Last reply
                    4
                    • jsulmJ jsulm

                      @SPlatten said in Listening and receiving data from a socket.:

                      that just takes time which I don't feel I have

                      But you have time to ask in a forum and wait for an answer?!

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

                      @jsulm , whilst I try to resolve it myself, granted I could also be reading the documentation.

                      Kind Regards,
                      Sy

                      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