Results 1 to 3 of 3

Thread: QTCPsocket data corrupt when receive

  1. #1
    Join Date
    Feb 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTCPsocket data corrupt when receive

    Sorry for my bad english, but iḿ trying to learn the concept of data transfer between qtserver qtcpsocket, but my files on to receive are corrupt. what is wrong? I Need your help please, thanks.
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTCPsocket data corrupt when receive

    This is the receiver
    Qt Code:
    1. void Dialog::tcpReady()
    2. {
    3. QFile file("linux.png");
    4. while(socket.bytesAvailable() > 0)
    5. {
    6. QByteArray ar=socket.readAll();
    7. file.open(QIODevice::WriteOnly);
    8. QDataStream ds(&file);
    9. ds.setVersion(QDataStream::Qt_4_6);
    10. ds << ar;
    11. }
    12. file.close();
    13. }
    To copy to clipboard, switch view to plain text mode 

    This is the Sender
    Qt Code:
    1. void ServerThread::run()
    2. {
    3. QTcpSocket socket;
    4. if( !socket.setSocketDescriptor( m_descriptor ) )
    5. {
    6. qDebug( "Socket error!" );
    7. return;
    8. }
    9.  
    10.  
    11. QFile file("linux.png");
    12. if (!file.open(QIODevice::ReadOnly))
    13. {
    14. qDebug("Cannot open file!");
    15. return;
    16. }
    17.  
    18. QByteArray data=file.readAll();
    19. int size=data.size();
    20.  
    21. QByteArray sizeBytes;
    22. quint32 b= quint32(data.size() - sizeof(quint32));
    23. sizeBytes.append(b);
    24. sizeBytes.append(data);
    25. socket.write(sizeBytes);
    26. socket.waitForDisconnected();
    27. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTCPsocket data corrupt when receive

    Hi, gabizzz. The first slot doesn't have to be a loop, as socket.readAll() always reads everything in the buffer.

    So instead try this, it should do the same:

    Qt Code:
    1. void Dialog::tcpReady()
    2. {
    3. QFile file("linux.png");
    4. file.open(QIODevice::WriteOnly + QIODevice::Append);
    5. QDataStream ds(&file);
    6. ds.setVersion(QDataStream::Qt_4_6);
    7. ds << socket.readAll();
    8. file.close();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Better check you error conditions though. If the file open fails, your program will misbehave. I slipped in QIODevice::Append, assuming you don't want to overwrite your file for every received packet. For improved performance it's best to leave the file open and not reopen it for each packet though.

    The rest of the code looks quite right (I haven't seen the whole source code).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

Similar Threads

  1. QTCPSocket not getting all data
    By jhowland in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2010, 15:20
  2. QTcpSocket + receiving data
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 08:10
  3. Replies: 6
    Last Post: 29th April 2009, 18:17
  4. Corrupt JPEG data: premature end of data segment
    By node_ex in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2008, 08:57
  5. Corrupt JPEG data
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2007, 16:12

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.