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;
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);
To copy to clipboard, switch view to plain text mode
for reading from datastream i have implemented the above operator as :
{
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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?
Bookmarks