Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Learning
  3. Qt in Education
  4. How to receive properly using QDataStream
QtWS25 Last Chance

How to receive properly using QDataStream

Scheduled Pinned Locked Moved Solved Qt in Education
9 Posts 5 Posters 1.7k Views
  • 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.
  • A Offline
    A Offline
    azhagan2
    wrote on 7 Mar 2023, 04:27 last edited by
    #1

    Hi , I tried to receive some structure variables from server side through socket by write function, and i tried to receive those in Qt QDatastream , but cant able to receive properly , only first int variable is receiving properly , remaining are not.
    this is my struct and code

    struct my_struct
    {
    int a;
    char b[100];
    char c[100];
    int d;
    char e[100];
    };

    QDataStream in(mysocket);
    in.setByteOrder(QDataStream::LittleEndian);
    my_struct s;
    in>>s.a;
    const int b_size = sizeof(s.b);
    in.readRawData(s.b, b_size);
    //s.b[b_size+1] = '\0';
    const int c_size = sizeof(s.c);
    in.readRawData(s.c, c_size);
    s.c[c_size+1] = '\0';

    in>>s.d;

    const int e_size = sizeof(s.e);
    in.readRawData(s.e,e_size);
    s.e[e_size+1] = '\0';

    QString b(s.b);
    QString c(s.c);
    QString e(s.e);
    ui->plainTextEdit_2->clear();
    result = QString("Received Data : \n Msg No. = %1, \n Message = %2, \n Msg dir. = %3, \n Msg Id = %4 \n Msg_end = %5 \n").arg(s.a).arg(b).arg(c).arg(s.d).arg(e);

    J C 2 Replies Last reply 7 Mar 2023, 06:06
    0
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 7 Mar 2023, 05:53 last edited by
      #2

      Using QDataStream for this purpose is very hairy and not recommended.
      It works only if the serialization is exactly the same on both sides. It’s not a type safe operation.
      Better to use JSON for transmission of structured data.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      2
      • A azhagan2
        7 Mar 2023, 04:27

        Hi , I tried to receive some structure variables from server side through socket by write function, and i tried to receive those in Qt QDatastream , but cant able to receive properly , only first int variable is receiving properly , remaining are not.
        this is my struct and code

        struct my_struct
        {
        int a;
        char b[100];
        char c[100];
        int d;
        char e[100];
        };

        QDataStream in(mysocket);
        in.setByteOrder(QDataStream::LittleEndian);
        my_struct s;
        in>>s.a;
        const int b_size = sizeof(s.b);
        in.readRawData(s.b, b_size);
        //s.b[b_size+1] = '\0';
        const int c_size = sizeof(s.c);
        in.readRawData(s.c, c_size);
        s.c[c_size+1] = '\0';

        in>>s.d;

        const int e_size = sizeof(s.e);
        in.readRawData(s.e,e_size);
        s.e[e_size+1] = '\0';

        QString b(s.b);
        QString c(s.c);
        QString e(s.e);
        ui->plainTextEdit_2->clear();
        result = QString("Received Data : \n Msg No. = %1, \n Message = %2, \n Msg dir. = %3, \n Msg Id = %4 \n Msg_end = %5 \n").arg(s.a).arg(b).arg(c).arg(s.d).arg(e);

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 7 Mar 2023, 06:06 last edited by
        #3

        @azhagan2 Does the server also use QDataStream? If now you can't use QDataStream on receiver side.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply 7 Mar 2023, 12:24
        1
        • A azhagan2
          7 Mar 2023, 04:27

          Hi , I tried to receive some structure variables from server side through socket by write function, and i tried to receive those in Qt QDatastream , but cant able to receive properly , only first int variable is receiving properly , remaining are not.
          this is my struct and code

          struct my_struct
          {
          int a;
          char b[100];
          char c[100];
          int d;
          char e[100];
          };

          QDataStream in(mysocket);
          in.setByteOrder(QDataStream::LittleEndian);
          my_struct s;
          in>>s.a;
          const int b_size = sizeof(s.b);
          in.readRawData(s.b, b_size);
          //s.b[b_size+1] = '\0';
          const int c_size = sizeof(s.c);
          in.readRawData(s.c, c_size);
          s.c[c_size+1] = '\0';

          in>>s.d;

          const int e_size = sizeof(s.e);
          in.readRawData(s.e,e_size);
          s.e[e_size+1] = '\0';

          QString b(s.b);
          QString c(s.c);
          QString e(s.e);
          ui->plainTextEdit_2->clear();
          result = QString("Received Data : \n Msg No. = %1, \n Message = %2, \n Msg dir. = %3, \n Msg Id = %4 \n Msg_end = %5 \n").arg(s.a).arg(b).arg(c).arg(s.d).arg(e);

          C Offline
          C Offline
          ChrisW67
          wrote on 7 Mar 2023, 06:49 last edited by
          #4

          @azhagan2 said in How to receive properly using QDataStream. See comments in the code

          QDataStream in(mysocket);
          in.setByteOrder(QDataStream::LittleEndian);
          my_struct s;
          in>>s.a;  
          // ^^  OK if the writer used the same int size and byte order.
          const int b_size = sizeof(s.b);
          // ^^ b_size = 100
          in.readRawData(s.b, b_size);
          // ^^ How many bytes were read?  100, or something else.  Were bytes there to be read?
          //s.b[b_size+1] = '\0';
          // ^^ set the 101st byte to zero!  Buffer overflow
          const int c_size = sizeof(s.c);
          // ^^ c_size = 100
          in.readRawData(s.c, c_size);
          // ^^ How many bytes were read?  
          s.c[c_size+1] = '\0';
          // ^^ set the 101st byte to zero!  Buffer overflow
          in>>s.d;
          // ^^ Maybe if you are still in sync with the sent packet.
          const int e_size = sizeof(s.e);
          in.readRawData(s.e,e_size);
          s.e[e_size+1] = '\0';
          
          1 Reply Last reply
          0
          • J jsulm
            7 Mar 2023, 06:06

            @azhagan2 Does the server also use QDataStream? If now you can't use QDataStream on receiver side.

            A Offline
            A Offline
            azhagan2
            wrote on 7 Mar 2023, 12:24 last edited by
            #5

            @jsulm No , server not using QDataStream

            A 1 Reply Last reply 7 Mar 2023, 13:01
            0
            • A azhagan2
              7 Mar 2023, 12:24

              @jsulm No , server not using QDataStream

              A Offline
              A Offline
              Axel Spoerl
              Moderators
              wrote on 7 Mar 2023, 13:01 last edited by
              #6

              @jsulm No , server not using QDataStream

              Then QDataStream is definitively not what should be used. It's not type safe as said before.
              Which format is the server sending? What's the nature of the connection?

              Software Engineer
              The Qt Company, Oslo

              A 1 Reply Last reply 8 Mar 2023, 05:14
              1
              • A Axel Spoerl
                7 Mar 2023, 13:01

                @jsulm No , server not using QDataStream

                Then QDataStream is definitively not what should be used. It's not type safe as said before.
                Which format is the server sending? What's the nature of the connection?

                A Offline
                A Offline
                azhagan2
                wrote on 8 Mar 2023, 05:14 last edited by
                #7

                @Axel-Spoer l Iam sending those structures through TCP sockets using send/write function.

                J 1 Reply Last reply 8 Mar 2023, 09:04
                0
                • A azhagan2
                  8 Mar 2023, 05:14

                  @Axel-Spoer l Iam sending those structures through TCP sockets using send/write function.

                  J Offline
                  J Offline
                  JonB
                  wrote on 8 Mar 2023, 09:04 last edited by
                  #8

                  @azhagan2
                  That in itself doesn't say much. It's more about in what "format" or what "protocol" you are sending data.

                  In any case QDataStream is not for use here, it's only for if both sides are written in Qt and using it. You should either use "raw" reads/writes, and make sure about things like what the endian ordering is both sides and how you might send, say, a string, or as @Axel-Spoerl says maybe use JSON for more complex but still structured data.

                  1 Reply Last reply
                  2
                  • A Offline
                    A Offline
                    azhagan2
                    wrote on 8 Mar 2023, 11:06 last edited by
                    #9

                    Thanks Everybody , all the replies helped me out :)

                    1 Reply Last reply
                    0
                    • A azhagan2 has marked this topic as solved on 8 Mar 2023, 11:06

                    5/9

                    7 Mar 2023, 12:24

                    • Login

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