Results 1 to 5 of 5

Thread: (SOLVED) Data loss over tcp connection?

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

    Default (SOLVED) Data loss over tcp connection?

    Hi,

    Sometimes the packets i send over the network seems to get lost O.o
    On the local machine all works fine... But not over the internet.

    Client:
    Qt Code:
    1. QByteArray database = inFile.readAll();
    2.  
    3. QByteArray outBlock;
    4. QDataStream out(&outBlock, QIODevice::WriteOnly);
    5. out.setVersion(QDataStream::Qt_4_5);
    6. out << qint64(0) << serverPass << quint16(GET_DB) << database;
    7. out.device()->seek(0);
    8. out << qint64(outBlock.size() - sizeof(qint64));
    9.  
    10. tcpSocket.write(outBlock);
    To copy to clipboard, switch view to plain text mode 

    Now before i send that packet i check the size od database(QByteArray)and it is correct!

    Server code:

    Qt Code:
    1. QDataStream inStream(tcpSocket);
    2. inStream.setVersion(QDataStream::Qt_4_5);
    3.  
    4. /* packet format */
    5. qint64 size2;
    6. QString serverPass2;
    7. quint16 reqType2;
    8. QByteArray data2;
    9.  
    10. inStream >> size2 >> serverPass2 >> reqType2 >> data2;
    To copy to clipboard, switch view to plain text mode 

    now i recive all data correctly except data2 (database) which is a QByteArray with size == 0! But that's not possible, so where is the data? The outgoing array(database) whas not empty...

    Is it possible to loose data over tcp??
    Last edited by giowck; 24th September 2009 at 20:05.

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

    Default Re: Data loss over tcp connection?

    data is transmitted in packages over the network. when you get notified (by a Qt signal) that data is available, this means SOME data, not necesarrily ALL data you expect.

    You have to wait til the complete package has arrived (which is why you send the package size first, so you know when you have all the data). Only then may you use >> to read the data into your QByteArray.

    See the various demos (fortune-client, I think is one) shipped with Qt that illustrate this.
    There should be several threads in this forum, too. Search it.

    HTH

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

    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

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

    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

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

    giowck (24th September 2009)

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.