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 can you send complex objects with sockets?
Forum Updated to NodeBB v4.3 + New Features

How can you send complex objects with sockets?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 796 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.
  • D Offline
    D Offline
    danga96
    wrote on last edited by
    #1

    hello to the whole community Qt Forum,
    I have a question, I'm developing a client / server application and I would like to know if there are socket (qt) functions capable of sending complex objects of variable size, but note when sending.
    Obviously the receiver will also know the size;
    Thanks in advance

    aha_1980A 1 Reply Last reply
    0
    • D danga96

      hello to the whole community Qt Forum,
      I have a question, I'm developing a client / server application and I would like to know if there are socket (qt) functions capable of sending complex objects of variable size, but note when sending.
      Obviously the receiver will also know the size;
      Thanks in advance

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      Hi @danga96,

      you should look up QDataStream which allows exactly this.

      Regards

      Qt has to stay free or it will die.

      D 1 Reply Last reply
      5
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Another alternative is to use JSON or the more recently added CBOR directly on the device

        "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
        3
        • aha_1980A aha_1980

          Hi @danga96,

          you should look up QDataStream which allows exactly this.

          Regards

          D Offline
          D Offline
          danga96
          wrote on last edited by
          #4

          @aha_1980
          Thank you for your reply; only that I need to send an object through a socket, not through a file ... I know they are very similar, but I would like to find, a class that does this right :)

          aha_1980A 1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @danga96 said in How can you send complex objects with sockets?:

            I need to send an object through a socket, not through a file

            #include <QCoreApplication>
            #include <QDebug>
            #include <QDataStream>
            #include <QByteArray>
            
            struct complex_object{
                QString m_string;
                float m_float;
                int m_int;
            };
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                complex_object cp;
                cp.m_string = "Some 💩";
                cp.m_float = 0.666;
                cp.m_int = 42;
            
                qInfo() << "before send:" << cp.m_string << cp.m_float << cp.m_int;
            
                QByteArray data; // used by socket stuff to hold data
                QDataStream sdata(&data, QIODevice::WriteOnly);
                sdata << cp.m_string << cp.m_float << cp.m_int;
            
                qInfo() << "... magically floating through cyberspace via sockets";
            
                complex_object cp2;
                QDataStream rdata(&data, QIODevice::ReadOnly);
                rdata >> cp2.m_string >> cp2.m_float >> cp2.m_int;
            
                qInfo() << "after receive:" << cp2.m_string << cp2.m_float << cp2.m_int;
            
                return a.exec();
            }
            

            I like puppies and kittens... (reference to show that describes "knowing your place", and yes, totally random)

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            1
            • D danga96

              @aha_1980
              Thank you for your reply; only that I need to send an object through a socket, not through a file ... I know they are very similar, but I would like to find, a class that does this right :)

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @danga96

              Thank you for your reply; only that I need to send an object through a socket, not through a file

              Everything is a file in Unix, and everything a QIODevice in Qt.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              2
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #7

                @aha_1980 said in How can you send complex objects with sockets?:

                Everything is a file in Unix, and everything a QIODevice in Qt.

                So say we all...

                C++ is a perfectly valid school of magic.

                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