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)?
-
@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}
-
use QTextStream...
read the first 8 bytes either raw (
reinterpret_cast
but you lose control over endinaness) or with QDataStream. Then attach aQTextStream
and read the json stringto detect endianness you could send a fixed 32bit integer
16777216
read itchar 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