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. Create a tcp client/server without using QDataStream
Forum Update on Monday, May 27th 2025

Create a tcp client/server without using QDataStream

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.0k 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.
  • T Offline
    T Offline
    tham
    wrote on 12 Dec 2017, 02:54 last edited by tham 12 Dec 2017, 04:51
    #1
    • Question :

    Is is possible to create a TCP client/server without using QDataStream?

    • Why I cannot use QDataStream :

    Because clients prefer the clients be written by node.js

    • Without QDataStream, how could I handle different endianness, string sizes under different platforms between clients(node js) and servers(Qt)?
    J 1 Reply Last reply 12 Dec 2017, 05:22
    0
    • T tham
      12 Dec 2017, 02:54
      • Question :

      Is is possible to create a TCP client/server without using QDataStream?

      • Why I cannot use QDataStream :

      Because clients prefer the clients be written by node.js

      • Without QDataStream, how could I handle different endianness, string sizes under different platforms between clients(node js) and servers(Qt)?
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 12 Dec 2017, 05:22 last edited by
      #2

      @tham You can still write the server using QDataStream.
      "how could I handle different endianness, string sizes under different platforms between clients(node js) and servers(Qt)?" - you can define all this as a part of your protocol.

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

      T 1 Reply Last reply 12 Dec 2017, 07:13
      4
      • J jsulm
        12 Dec 2017, 05:22

        @tham You can still write the server using QDataStream.
        "how could I handle different endianness, string sizes under different platforms between clients(node js) and servers(Qt)?" - you can define all this as a part of your protocol.

        T Offline
        T Offline
        tham
        wrote on 12 Dec 2017, 07:13 last edited by
        #3

        @jsulm Please correct me if I am wrong

        If all of the messages are composed by ASCII characters, all of the characters will always occupy 1 byte only and do not need to deal with endianness problem. All I need to do is make sure I have read the complete messages. The client can send their messages as following

        //8 bytes of characters to indicate how many bytes the messages is, after that come with json contents
        12345678{json content}
        
        J V 2 Replies Last reply 12 Dec 2017, 07:18
        0
        • T tham
          12 Dec 2017, 07:13

          @jsulm Please correct me if I am wrong

          If all of the messages are composed by ASCII characters, all of the characters will always occupy 1 byte only and do not need to deal with endianness problem. All I need to do is make sure I have read the complete messages. The client can send their messages as following

          //8 bytes of characters to indicate how many bytes the messages is, after that come with json contents
          12345678{json content}
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 12 Dec 2017, 07:18 last edited by
          #4

          @tham Yes, ASCII is one byte encoding.

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

          1 Reply Last reply
          3
          • T tham
            12 Dec 2017, 07:13

            @jsulm Please correct me if I am wrong

            If all of the messages are composed by ASCII characters, all of the characters will always occupy 1 byte only and do not need to deal with endianness problem. All I need to do is make sure I have read the complete messages. The client can send their messages as following

            //8 bytes of characters to indicate how many bytes the messages is, after that come with json contents
            12345678{json content}
            
            V Offline
            V Offline
            VRonin
            wrote on 12 Dec 2017, 08:16 last edited by VRonin 12 Dec 2017, 08:23
            #5

            use QTextStream...

            read the first 8 bytes either raw (reinterpret_cast but you lose control over endinaness) or with QDataStream. Then attach a QTextStream and read the json string

            to detect endianness you could send a fixed 32bit integer 16777216 read it

            char endianBuffer[4]; 
            socket.read(endianBuffer,4); 
            if(endianBuffer[0]) 
            // bigendian
            else
            // littleendian
            

            "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

            T 1 Reply Last reply 12 Dec 2017, 11:47
            2
            • V VRonin
              12 Dec 2017, 08:16

              use QTextStream...

              read the first 8 bytes either raw (reinterpret_cast but you lose control over endinaness) or with QDataStream. Then attach a QTextStream and read the json string

              to detect endianness you could send a fixed 32bit integer 16777216 read it

              char endianBuffer[4]; 
              socket.read(endianBuffer,4); 
              if(endianBuffer[0]) 
              // bigendian
              else
              // littleendian
              
              T Offline
              T Offline
              tham
              wrote on 12 Dec 2017, 11:47 last edited by tham 12 Dec 2017, 11:48
              #6

              @VRonin said in Create a tcp client/server without using QDataStream:

              to detect endianness you could send a fixed 32bit integer 16777216 read it

              This is a great idea, thanks. However, in this project I tend to make the all of the data as ASCII characters, no need to deal with endianness or string per byte, right now every info we need should be able to cover by json.

              Please point out the main drawback of this solution if any, thanks

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 12 Dec 2017, 11:50 last edited by
                #7

                json is used so widely because it's effective. I see no problem in sending, even utf-8 json via socket

                "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

                T 1 Reply Last reply 12 Dec 2017, 12:39
                1
                • V VRonin
                  12 Dec 2017, 11:50

                  json is used so widely because it's effective. I see no problem in sending, even utf-8 json via socket

                  T Offline
                  T Offline
                  tham
                  wrote on 12 Dec 2017, 12:39 last edited by
                  #8

                  @VRonin If it is utf-8, do I need to deal with endianness and bytes per character issues?

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 12 Dec 2017, 13:36 last edited by
                    #9

                    No, QTextCodec (inside QTextStream) takes care of everything for you

                    "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

                    T 1 Reply Last reply 12 Dec 2017, 16:17
                    2
                    • V VRonin
                      12 Dec 2017, 13:36

                      No, QTextCodec (inside QTextStream) takes care of everything for you

                      T Offline
                      T Offline
                      tham
                      wrote on 12 Dec 2017, 16:17 last edited by
                      #10

                      @VRonin said in Create a tcp client/server without using QDataStream:

                      QTextCodec (inside QTextStream) takes care of everything for you

                      Thanks, this is awesome

                      1 Reply Last reply
                      0

                      1/10

                      12 Dec 2017, 02:54

                      • Login

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