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. How to send non-trivial datatypes over UDP?
Forum Updated to NodeBB v4.3 + New Features

How to send non-trivial datatypes over UDP?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.2k Views 1 Watching
  • 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 Offline
    M Offline
    Magnus2552
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      3
      • M Offline
        M Offline
        Magnus2552
        wrote on last edited by
        #3

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

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          1

          • Login

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