How to send non-trivial datatypes over UDP?

  • Sending a string over an UDP socket is trivial, once you have correctly initialized the socket.

    QByteArray datagram;
    udpSocket->writeDatagram(datagram, hostaddress, port);

    But what about other datatypes? What I am searching for is some way to send any data.
    For example I have a QPointF pos, which I have to send over UDP. How could I do that, I cannot just call datagram.append on pos.
    Is there any clean built-in way to do that?
    Thanks for any help :)

  • Lifetime Qt Champion


    One way you could do it is with QDataStream or QTextStream to serialize your data into your QByteArray.

    Note that you should still structure your data properly. You can't just go and send anything and everything and expect the other side to automagically decode it.

  • Thank you a lot! I got it working with QDataStream :)

  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

Log in to reply

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