PDA

View Full Version : Fastest way to read structure from QTcpSocket



karankumar1609
15th January 2014, 14:28
Hello All,

I have got one problem which is resolved in http://www.qtcentre.org/threads/57634-SOLVED-Unable-to-read-data-by-QDataStream (http://www.qtcentre.org/threads/57634-SOLVED-Unable-to-read-data-by-QDataStream).
But i want to know that, is there any other fastest way to read all the data from socket in a structure.

My current structure is


typedef struct hShakeAckPkt
{
unsigned short Id; // 0XFEFE
unsigned short BlockSize; // 45
int Reserved1[2]; // fill with 0
unsigned short FeedType; // fill with 0
unsigned short Reserved2[3]; // fill with 0
unsigned short Major; // 1
unsigned short Minor; // 4
unsigned char ProgramId[21]; // "CTCL" rest with '\0'

}hShakeAckPkt;

QDataStream & operator << (QDataStream &out, const hShakeAckPkt &hand_shake_pkt);
QDataStream & operator >> (QDataStream &in, hShakeAckPkt &hand_shake_pkt);


And the code for reading the data from the socket is


QDataStream &operator <<(QDataStream &out, const hShakeAckPkt &hand_shake_pkt)
{
out << hand_shake_pkt.Id;
out << hand_shake_pkt.BlockSize;

for(int i = 0; i < 2; ++i)
out << hand_shake_pkt.Reserved1[i];

out << hand_shake_pkt.FeedType;

for(int i = 0; i < 3; ++i)
out << hand_shake_pkt.Reserved2[i];

out << hand_shake_pkt.Major;
out << hand_shake_pkt.Minor;

for(int i = 0; i < 21; ++i)
out << hand_shake_pkt.ProgramId[i];

return out;
}

QDataStream &operator >>(QDataStream &in, hShakeAckPkt &hand_shake_pkt)
{
in >> hand_shake_pkt.Id;
in >> hand_shake_pkt.BlockSize;

for(int i = 0; i < 2; ++i)
in >> hand_shake_pkt.Reserved1[i];

in >> hand_shake_pkt.FeedType;

for(int i = 0; i < 3; ++i)
in >> hand_shake_pkt.Reserved2[i];

in >> hand_shake_pkt.Major;
in >> hand_shake_pkt.Minor;

for(int i = 0; i < 21; ++i)
in >> hand_shake_pkt.ProgramId[i];

return in;
}


Now i want to know that, is there any other fastest way to read all the data in structure memory. (With or without QDataStream)

?????

Momergil
3rd July 2014, 13:58
I'll certainly happy to see that too :) But I'm worried that with QDataStream there is no other way to do serialization/deserialization without going after each item of the struct. Now about without the QDataStream, maybe this article will do the trick (depending in which direction you want to do it: http://smartqt.blogspot.com/2014/05/how-to-convert-struct-in-and-from.html).