Sending QByteArray over a socket



  • 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);
    

  • Qt Champions 2016

    @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



  • 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?



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


  • Moderators

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



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


  • Qt Champions 2016

    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.



  • @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)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.