Results 1 to 6 of 6

Thread: [SOLVED] Unable to read data by QDataStream

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Thumbs up [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
    Qt Code:
    1. typedef struct hShakeAckPkt
    2. {
    3. unsigned short Id; // 0XFEFE
    4. unsigned short BlockSize; // 45
    5. long Reserved1[2]; // fill with 0
    6. unsigned short FeedType; // fill with 0
    7. unsigned short Reserved2[3]; // fill with 0
    8. unsigned short Major; // 1
    9. unsigned short Minor; // 4
    10. unsigned char ProgramId[21]; // "CTCL" rest with '\0'
    11.  
    12. }hShakeAckPkt;
    13.  
    14. QDataStream & operator << (QDataStream &out, const hShakeAckPkt &hand_shake_pkt);
    15. 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 :
    Qt Code:
    1. QDataStream &operator <<(QDataStream &out, const hShakeAckPkt &hand_shake_pkt)
    2. {
    3. out << hand_shake_pkt.Id;
    4. out << hand_shake_pkt.BlockSize;
    5. out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.Reserved1), 2);
    6. out << hand_shake_pkt.FeedType;
    7. out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.Reserved2), 3);
    8. out << hand_shake_pkt.Major;
    9. out << hand_shake_pkt.Minor;
    10. out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.ProgramId), 21);
    11.  
    12. return out;
    13. }
    14.  
    15. QDataStream &operator >>(QDataStream &in, hShakeAckPkt &hand_shake_pkt)
    16. {
    17. in >> hand_shake_pkt.Id;
    18. in >> hand_shake_pkt.BlockSize;
    19. in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.Reserved1), 2);
    20. in >> hand_shake_pkt.FeedType;
    21. in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.Reserved2), 3);
    22. in >> hand_shake_pkt.Major;
    23. in >> hand_shake_pkt.Minor;
    24. in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.ProgramId), 21);
    25.  
    26. return in;
    27. }
    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?
    Last edited by karankumar1609; 15th January 2014 at 13:55.
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

Similar Threads

  1. Replies: 11
    Last Post: 21st June 2011, 01:05
  2. why "Unable to read image data"
    By zarelaky in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 21st December 2010, 00:47
  3. Replies: 1
    Last Post: 18th October 2010, 16:07
  4. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 19:03
  5. Replies: 1
    Last Post: 16th October 2006, 07:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.