Results 1 to 5 of 5

Thread: (SOLVED) Data loss over tcp connection?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2009
    Posts
    44
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    29
    Thanked 5 Times in 5 Posts

    Default Re: Data loss over tcp connection?

    thanks for your answer. I looked at the source code from various examples... The problem is that i need a blocking function (no signals like readyRead() ...)

    i only use socket.waitForReadyRead()... So how can i continue the function block only if i have all data?

    Something like:

    Qt Code:
    1. ...
    2. socket.waitForReadyRead();
    3.  
    4. inStream >> size;
    5.  
    6. if (size > socket.bytesAvailable())
    7. socket.waitForReadyRead();
    8.  
    9. instream >> database;
    10. ...
    To copy to clipboard, switch view to plain text mode 
    ?

    Sorry im new at network programming

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Data loss over tcp connection?

    well, while-loops are not network-specific, are they?

    code taken from fortune client example

    Qt Code:
    1. const int Timeout = 5 * 1000;
    2.  
    3. // wait for blocksize to arrive
    4. while (socket.bytesAvailable() < (int)sizeof(quint16)) {
    5. if (!socket.waitForReadyRead(Timeout)) {
    6. emit error(socket.error(), socket.errorString());
    7. return;
    8. }
    9. }
    10.  
    11. // read blockSize
    12. quint16 blockSize;
    13. QDataStream in(&socket);
    14. in.setVersion(QDataStream::Qt_4_0);
    15. in >> blockSize;
    16. // blocksize does not include "itself"
    17.  
    18. // now we know how much data has to arrive in total
    19. // wait for rest of the block...
    20. while (socket.bytesAvailable() < blockSize) {
    21. if (!socket.waitForReadyRead(Timeout)) {
    22. emit error(socket.error(), socket.errorString());
    23. return;
    24. }
    25.  
    26. // now we are sure that no 'short reads' can happen
    27. instream >> database;
    To copy to clipboard, switch view to plain text mode 

    you have to take care if blockSize is the size of the complete package (including the 2 bytes of the quint16 of blocksize itself) or just the data part's size. in the fortune server example blocksize is only the data part

    HTH

  3. The following user says thank you to caduel for this useful post:

    giowck (24th September 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    44
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    29
    Thanked 5 Times in 5 Posts

    Smile Re: Data loss over tcp connection?

    Hey thanks... BTW i solved the problem with this loop:

    Qt Code:
    1. qint64 size;
    2. QString serverPass;
    3. quint16 reqType;
    4. QByteArray data;
    5.  
    6. in >> size;
    7.  
    8. while (tcpSocket->bytesAvailable() < size) {
    9. if (!tcpSocket->waitForReadyRead()) {
    10. qDebug() << "Not enough bytes!";
    11. disconnectFromServer();
    12. return false;
    13. }
    14. }
    15.  
    16. in >> serverPass >> reqType >> data;
    To copy to clipboard, switch view to plain text mode 

    THANKS!!!

Similar Threads

  1. LocalSocket and Data Loss
    By ManuMies in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2009, 15:32
  2. data rate transfer is decreasing in TCP connection
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 17:15
  3. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 23:46
  4. loss of data recieved using http->readAll()
    By arunredi in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2008, 18:12
  5. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 11:22

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
  •  
Qt is a trademark of The Qt Company.