PDA

View Full Version : [SOLVED] Unable to read data by QDataStream



karankumar1609
15th January 2014, 09:03
Hello All,

I am currently trying to read data from socket, but it is something new to me that i want to read a structure from socket.
I have the following structure with implemented overloaded <<, and >> operator


typedef struct hShakeAckPkt
{
unsigned short Id; // 0XFEFE
unsigned short BlockSize; // 45
long 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);


for reading from datastream i have implemented the above operator as :


QDataStream &operator <<(QDataStream &out, const hShakeAckPkt &hand_shake_pkt)
{
out << hand_shake_pkt.Id;
out << hand_shake_pkt.BlockSize;
out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.Reserved1), 2);
out << hand_shake_pkt.FeedType;
out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.Reserved2), 3);
out << hand_shake_pkt.Major;
out << hand_shake_pkt.Minor;
out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.ProgramId), 21);

return out;
}

QDataStream &operator >>(QDataStream &in, hShakeAckPkt &hand_shake_pkt)
{
in >> hand_shake_pkt.Id;
in >> hand_shake_pkt.BlockSize;
in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.Reserved1), 2);
in >> hand_shake_pkt.FeedType;
in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.Reserved2), 3);
in >> hand_shake_pkt.Major;
in >> hand_shake_pkt.Minor;
in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.ProgramId), 21);

return in;
}


The problem is when i read the data it is not the original one.
And what is the procedure to read array??

Anyone have idea about that?

ChrisW67
15th January 2014, 10:02
Reserved1: How do you propose to recover 2 longs from the file if you only write 2 bytes to the file in the first place?
Reserved2: How do you propose to recover 3 unsigned shorts from the file if you only write 3 bytes to the file in the first place?

Write and read an array one element at a time for as many elements as the array contains.

anda_skoa
15th January 2014, 10:15
Aside from your code obviously being wrong (a single long has more than 2 bytes, two longs have way more than that), I would say the way to serialize an array is to loop over its entries and serialize them individually.

Cheers,
_

karankumar1609
15th January 2014, 10:41
This means if i write


QDataStream &operator <<(QDataStream &out, const hShakeAckPkt &hand_shake_pkt)
{
out << hand_shake_pkt.Id;
out << hand_shake_pkt.BlockSize;
out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.Reserved1), 16);
out << hand_shake_pkt.FeedType;
out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.Reserved2), 6);
out << hand_shake_pkt.Major;
out << hand_shake_pkt.Minor;
out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.ProgramId), 21);

return out;
}

QDataStream &operator >>(QDataStream &in, hShakeAckPkt &hand_shake_pkt)
{
in >> hand_shake_pkt.Id;
in >> hand_shake_pkt.BlockSize;
in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.Reserved1), 16);
in >> hand_shake_pkt.FeedType;
in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.Reserved2), 6);
in >> hand_shake_pkt.Major;
in >> hand_shake_pkt.Minor;
in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.ProgramId), 21);

return in;
}


This would be correct????

stampede
15th January 2014, 11:04
Serializing a long without taking endianness into account will result in bugs on machines with different byte ordering.
You can let Qt handle this by using QDataStream operators for built-in types (Qt defaults to big endian).

karankumar1609
15th January 2014, 13:54
Thanks for the effective and quick response.
It works,,,,,
Now i use


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;
}