Results 1 to 6 of 6

Thread: Problem with TCP server/client (corrupted data)

  1. #1

    Default Problem with TCP server/client (corrupted data)

    Hi,
    I'm developing a tcp-based server/client. I found a code to post a struct through tcp, but the data I get is looking weird. Here is what I have right now:

    Qt Code:
    1. struct abc {
    2. char a;
    3. char b;
    4. };
    5.  
    6. abc *a = (abc*)malloc(sizeof(struct abc));
    7. a->a='1';
    8. a->b = '2';
    9.  
    10. QByteArray block;
    11. QDataStream out(&block, QIODevice::WriteOnly);
    12. out.setVersion(QDataStream::Qt_4_0);
    13.  
    14. out << (void*)a;
    15.  
    16. socket->write(block.data());
    To copy to clipboard, switch view to plain text mode 

    And here is the code to receive it:

    Qt Code:
    1. char *data = (char*)this->socket->readAll().data();
    2.  
    3. QDataStream in(data);
    4. in.setVersion(QDataStream::Qt_4_0);
    5.  
    6. QByteArray block;
    7. in >> block;
    8.  
    9. abc *received;
    10. received = (abc*)data;
    To copy to clipboard, switch view to plain text mode 

    And when I debug this data, there are many strange characters instead of chars... What can be wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with TCP server/client (corrupted data)

    You are sending a pointer, that's probably not what you want since the pointer will point to a random location after it is received.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: Problem with TCP server/client (corrupted data)

    Thanks for the reply,
    if I don't want a pointer, I can only send it through a reference, and the problem is the same (garbage data):

    Qt Code:
    1. abc a;
    2. a.a = '1';
    3. a.b = '2';
    4.  
    5. out << &a;
    6.  
    7. socket->write(block.data());
    To copy to clipboard, switch view to plain text mode 

    Is there a way not to use a reference?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with TCP server/client (corrupted data)

    That's not the point. Using "&a" you are still sending a pointer (and not a reference). You need to send the data, for instance:

    Qt Code:
    1. struct abc {
    2. char a;
    3. char b;
    4. };
    5.  
    6. QDataStream& operator<<(QDataStream& stream, const abc &object) {
    7. stream << object.a << object.b;
    8. return stream;
    9. }
    10.  
    11.  
    12. QDataStream& operator>>(QDataStream& stream, abc &object) {
    13. stream >> object.a >> object.b;
    14. return stream;
    15. }
    To copy to clipboard, switch view to plain text mode 
    and then you can do:
    Qt Code:
    1. abc a;
    2. a.a = '1';
    3. a.b = '2';
    4. QDataStream str(&data);
    5. str << a;
    6. socket->write(data); // btw. such code is practically useless,
    7. // the receiver doesn't know how much data to expect
    To copy to clipboard, switch view to plain text mode 

    Being lazy and trying to dump a memory block directly into the data stream is usually a bad idea.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: Problem with TCP server/client (corrupted data)

    Finally, I got the answer:

    Qt Code:
    1. struct abc {
    2. char a;
    3. char b;
    4. };
    5.  
    6. abc *ab;
    7. ab = new abc;
    8.  
    9. socket->write((char*)ab, sizeof(struct abc));
    To copy to clipboard, switch view to plain text mode 

    Works perfect!

    Oh, and I receive data like this:

    Qt Code:
    1. abc *received;
    2.  
    3. QByteArray data = socket->readAll();
    4.  
    5. received = (abc*)data.data();
    To copy to clipboard, switch view to plain text mode 
    Last edited by chris15001900; 16th October 2011 at 09:55.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with TCP server/client (corrupted data)

    You are aware that your solution is not portable, right? Probably even between 32 and 64b platforms.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. server not getting client data
    By raj_iv in forum Qt Programming
    Replies: 0
    Last Post: 31st May 2011, 08:30
  2. server-client problem
    By sksingh73 in forum Newbie
    Replies: 2
    Last Post: 3rd July 2010, 07:18
  3. TCP server-client app problem
    By pogostick in forum Newbie
    Replies: 6
    Last Post: 25th January 2010, 08:13
  4. Qt-- Client Server communication problem
    By thisismyuname in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 01:04
  5. password problem with client and server
    By mate in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2008, 18:20

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.