Qt Forum

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

    Solved How to receive properly using QDataStream

    Qt in Education
    5
    9
    116
    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.
    • A
      azhagan2 last edited by

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

      jsulm C 2 Replies Last reply Reply Quote 0
      • Axel Spoerl
        Axel Spoerl last edited by

        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 Reply Quote 2
        • jsulm
          jsulm Lifetime Qt Champion @azhagan2 last edited by

          @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 Reply Quote 1
          • Topic has been marked as solved  A azhagan2 
          • C
            ChrisW67 @azhagan2 last edited by

            @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 Reply Quote 0
            • A
              azhagan2 @jsulm last edited by

              @jsulm No , server not using QDataStream

              Axel Spoerl 1 Reply Last reply Reply Quote 0
              • Axel Spoerl
                Axel Spoerl @azhagan2 last edited by

                @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 Reply Quote 1
                • A
                  azhagan2 @Axel Spoerl last edited by

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

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @azhagan2 last edited by

                    @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 Reply Quote 2
                    • A
                      azhagan2 last edited by

                      Thanks Everybody , all the replies helped me out :)

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