Create a tcp client/server without using QDataStream
-
wrote on 12 Dec 2017, 02:54 last edited by tham 12 Dec 2017, 04:51
- 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)?
-
- 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. -
@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.wrote on 12 Dec 2017, 07:13 last edited by@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}
-
@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}
@tham Yes, ASCII is one byte encoding.
-
@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}
wrote on 12 Dec 2017, 08:16 last edited by VRonin 12 Dec 2017, 08:23use 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
-
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
wrote on 12 Dec 2017, 11:47 last edited by tham 12 Dec 2017, 11:48@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
-
wrote on 12 Dec 2017, 11:50 last edited by
json is used so widely because it's effective. I see no problem in sending, even utf-8 json via socket
-
json is used so widely because it's effective. I see no problem in sending, even utf-8 json via socket
-
wrote on 12 Dec 2017, 13:36 last edited by
No, QTextCodec (inside QTextStream) takes care of everything for you
-
wrote on 12 Dec 2017, 16:17 last edited by
@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/10