Create a tcp client/server without using QDataStream



    • 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)?

  • Moderators

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



  • @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}
    

  • Moderators

    @tham Yes, ASCII is one byte encoding.



  • 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
    


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



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



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



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



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

    QTextCodec (inside QTextStream) takes care of everything for you

    Thanks, this is awesome


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.