[SOLVED] Unable to read data by QDataStream
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
Code:
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;
for reading from datastream i have implemented the above operator as :
Code:
{
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;
}
{
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?
Re: Unable to read data by QDataStream
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.
Re: Unable to read data by QDataStream
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,
_
Re: Unable to read data by QDataStream
This means if i write
Code:
{
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;
}
{
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????
Re: Unable to read data by QDataStream
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).
Re: Unable to read data by QDataStream
Thanks for the effective and quick response.
It works,,,,,
Now i use
Code:
{
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;
}
{
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;
}