Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Sending QByteArray over a socket

    General and Desktop
    4
    8
    3460
    Loading More Posts
    • 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.
    • D
      DRoscoe last edited by

      I have a QByteArray I want to serialize over a socket. Here is my implementation:

      void TcpCommunicationHandler::sendMessage(const QByteArray& msg_out)
      {
        if (socket)
        {
          QDataStream output_stream(socket);
          output_stream.setVersion(QDataStream::Qt_5_4);
          output_stream << msg_out;
        }
      }
      

      Is this the correct way to do this? If I wanted to know how many bytes were written, is there a way to access that information? Typically, I would call:

      qint64 bytes_sent = socket->write(msg_out);
      
      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @DRoscoe last edited by kshegunov

        @DRoscoe said in Sending QByteArray over a socket:

        Is this the correct way to do this?

        Yes, it is a correct way. :)

        If I wanted to know how many bytes were written, is there a way to access that information?

        Yes: http://doc.qt.io/qt-5/qiodevice.html#bytesWritten

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply Reply Quote 3
        • VRonin
          VRonin last edited by

          while your code is 99% correct (you should check that socket is writeable), out of curiosity, why don't you just call write on th QIODevice?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply Reply Quote 2
          • D
            DRoscoe last edited by

            @VRonin I wanted to leverage the QDataStream serialization. It might be unnecessary for QByteArray, but since we often look at existing code when writing new code, I wanted to write in a way that would work for any object, and not just QByteArray

            raven-worx 1 Reply Last reply Reply Quote 2
            • raven-worx
              raven-worx Moderators @DRoscoe last edited by

              @DRoscoe said in Sending QByteArray over a socket:

              I wanted to write in a way that would work for any object, and not just QByteArray

              It's all in the docs. Simply implement the stream operators for your custom class.
              For a list of Qt classes already supported by QDataStream see this.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              D 1 Reply Last reply Reply Quote 2
              • D
                DRoscoe @raven-worx last edited by

                @raven-worx Yes, I did see that in the documentation. My answer was in context of @VRonin 's question as to why I departed from simply using

                QByteArray some_msg("hello world");
                socket->write(some_msg);
                

                We are working on a very big project with short timelines, so whenever we have to write new code, the first thing we usually do is look at our existing code for an example. Whenever possible, I try to use good code, knowing this, so that bad practices don't get propagated. In my example a QByteArray is not a good example of using QDataSteam's serialization format, but if I can show how its done, then I've saved someone else some online research and/or prevented propagation of a pattern that is less desirable.

                The help and advice I've received from this community has been invaluable. Its the biggest reason we are migrating away from ACE in favor of Qt

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @DRoscoe last edited by kshegunov

                  Well, I'd suggest something else here too, talking about serialization that is. You could attach the data stream to a byte array and serialize on that, then you can still use QIODevice::write for the socket, e.g:

                  QByteArray buffer;
                  QDataStream out(&buffer, QIODevice::WriteOnly);
                  
                  out << data1 << data2 << data3;
                  
                  int bytes = socket->write(buffer);
                  // ... etc.
                  

                  Sometimes this approach can be beneficial (especially in debugging) as the serialized data is immediately accessible from the byte array.

                  Read and abide by the Qt Code of Conduct

                  VRonin 1 Reply Last reply Reply Quote 2
                  • VRonin
                    VRonin @kshegunov last edited by

                    @kshegunov said in Sending QByteArray over a socket:

                    You could attach the data stream to a byte array and serialize on that, then you can still use QIODevice::write for the socket

                    Probably stating the obvious but this method has the drawback of making you run out of memory pretty fast if you serialise a lot of data (e.g. audio/video streams)

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post