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 Updated to NodeBB v4.3 + New Features

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.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by tham
    #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)?
    jsulmJ 1 Reply Last reply
    0
    • thamT tham
      • 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)?
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      thamT 1 Reply Last reply
      4
      • jsulmJ jsulm

        @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.

        thamT Offline
        thamT Offline
        tham
        wrote on 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}
        
        jsulmJ VRoninV 2 Replies Last reply
        0
        • thamT tham

          @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}
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          • thamT tham

            @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}
            
            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #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

            thamT 1 Reply Last reply
            2
            • VRoninV VRonin

              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
              
              thamT Offline
              thamT Offline
              tham
              wrote on last edited by tham
              #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
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on 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

                thamT 1 Reply Last reply
                1
                • VRoninV VRonin

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

                  thamT Offline
                  thamT Offline
                  tham
                  wrote on 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
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 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

                    thamT 1 Reply Last reply
                    2
                    • VRoninV VRonin

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

                      thamT Offline
                      thamT Offline
                      tham
                      wrote on 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

                      • Login

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