Results 1 to 6 of 6

Thread: [SOLVED] Unable to read data by QDataStream

  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 14: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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  4. #4
    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

    Default Re: Unable to read data by QDataStream

    This means if i write
    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), 16);
    6. out << hand_shake_pkt.FeedType;
    7. out.writeRawData(reinterpret_cast<const char*>(hand_shake_pkt.Reserved2), 6);
    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), 16);
    20. in >> hand_shake_pkt.FeedType;
    21. in.readRawData(reinterpret_cast<char*>(hand_shake_pkt.Reserved2), 6);
    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 

    This would be correct????
    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.

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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).

  6. The following user says thank you to stampede for this useful post:

    karankumar1609 (15th January 2014)

  7. #6
    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

    Default Re: Unable to read data by QDataStream

    Thanks for the effective and quick response.
    It works,,,,,
    Now i use
    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.  
    6. for(int i = 0; i < 2; ++i)
    7. out << hand_shake_pkt.Reserved1[i];
    8.  
    9. out << hand_shake_pkt.FeedType;
    10.  
    11. for(int i = 0; i < 3; ++i)
    12. out << hand_shake_pkt.Reserved2[i];
    13.  
    14. out << hand_shake_pkt.Major;
    15. out << hand_shake_pkt.Minor;
    16.  
    17. for(int i = 0; i < 21; ++i)
    18. out << hand_shake_pkt.ProgramId[i];
    19.  
    20. return out;
    21. }
    22.  
    23. QDataStream &operator >>(QDataStream &in, hShakeAckPkt &hand_shake_pkt)
    24. {
    25. in >> hand_shake_pkt.Id;
    26. in >> hand_shake_pkt.BlockSize;
    27.  
    28. for(int i = 0; i < 2; ++i)
    29. in >> hand_shake_pkt.Reserved1[i];
    30.  
    31. in >> hand_shake_pkt.FeedType;
    32.  
    33. for(int i = 0; i < 3; ++i)
    34. in >> hand_shake_pkt.Reserved2[i];
    35.  
    36. in >> hand_shake_pkt.Major;
    37. in >> hand_shake_pkt.Minor;
    38.  
    39. for(int i = 0; i < 21; ++i)
    40. in >> hand_shake_pkt.ProgramId[i];
    41.  
    42. return in;
    43. }
    To copy to clipboard, switch view to plain text mode 
    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, 02:05
  2. why "Unable to read image data"
    By zarelaky in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 21st December 2010, 01:47
  3. Replies: 1
    Last Post: 18th October 2010, 17:07
  4. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 20:03
  5. Replies: 1
    Last Post: 16th October 2006, 08: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.