Qt Forum

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

    Solved How to send non-trivial datatypes over UDP?

    General and Desktop
    2
    4
    784
    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.
    • M
      Magnus2552 last edited by

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

      QByteArray datagram;
      datagram.append("STRINGTOSEND");
      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 :)

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 3
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 3
          • M
            Magnus2552 last edited by

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

            1 Reply Last reply Reply Quote 1
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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