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. Sending QByteArray over a socket
QtWS25 Last Chance

Sending QByteArray over a socket

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 4.3k Views
  • 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 Offline
    D Offline
    DRoscoe
    wrote on last edited by
    #1

    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);
    
    kshegunovK 1 Reply Last reply
    0
    • D DRoscoe

      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);
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @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
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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
        2
        • D Offline
          D Offline
          DRoscoe
          wrote on last edited by
          #4

          @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-worxR 1 Reply Last reply
          2
          • D DRoscoe

            @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-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @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
            2
            • raven-worxR raven-worx

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

              D Offline
              D Offline
              DRoscoe
              wrote on last edited by
              #6

              @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

              kshegunovK 1 Reply Last reply
              0
              • D DRoscoe

                @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

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                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

                VRoninV 1 Reply Last reply
                2
                • kshegunovK 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.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

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

                  • Login

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