Results 1 to 4 of 4

Thread: QFile::read failed after n bytes already read

  1. #1
    Join Date
    May 2013
    Location
    Quebec, Canada
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QFile::read failed after n bytes already read

    Hello all,

    I'm new to QT, new to this forum too, student programmer, etc. In brief, new.

    I tried the following example in order to learned how to do a tcp connection and how to send some bytes too.
    example : http://www.qtcentre.org/threads/4820...hrough-sockets , code in the bottom of the page.
    Everything work well except data reading from a file.

    I wrote (copy) that code in the server part :
    Qt Code:
    1. // Send File
    2. QFile inputFile(FILENAME);
    3. QByteArray read;
    4. inputFile.open(QIODevice::ReadOnly);
    5. while(1)
    6. {
    7. read.clear();
    8. read = inputFile.read(32768*8);
    9. qDebug() << "Read : " << read.size();
    10. qDebug() << "Pos : " << inputFile.pos();
    11. if(read.size() == 0)
    12. break;
    13.  
    14. qDebug() << "Written :" << client.write(read);
    15. client.waitForBytesWritten();
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    So, it works well, but it stop after 3 pass (while) and just sent a certain amount of bytes even if I use whatever file as input.
    It seems that is the read = inputFile.read(32768*8) that is wrong, but I'm not sure.

    Following is the server output. In the third pass it just read 182276 bytes and the QTCpSocket client.write(read) return -1 this time.
    The amount of data received by the client = the amount sent by the server = 706564.

    File transfer started
    Thread called
    Thread Descriptor : 21
    Thread : Connected
    Read : 262144
    Pos : 262144
    Written : 262144
    Read : 262144
    Pos : 524288
    Written : 262144
    Read : 182276
    Pos : 706564
    Written : -1
    QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState
    Read : 0
    Pos : 706564
    QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState
    Thread : File transfer completed
    /home/serge/Bureau/tp6-server/tp6-server s'est terminé avec le code 0

    Thank you very much for your time.

  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: QFile::read failed after n bytes already read

    QFile::read failed after n bytes already read
    There's nothing wrong with the read() in the code you show us. It reads two blocks of 256k and the remainder of the file is 182276 bytes assuming the file you have opened contains 706564 bytes.

    The "QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState" indicates that the TCP connection to the other end has been closed, possibly deliberately by the other end, possibly some other error condition. Nothing in the code we can see would help diagnose this. Try looking at QTcpSocket::error() or connecting to the error() signal.
    Last edited by ChrisW67; 3rd May 2013 at 03:49.

  3. #3
    Join Date
    May 2013
    Location
    Quebec, Canada
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile::read failed after n bytes already read

    Thank you for your quick reply ChrisW67

    Quote Originally Posted by ChrisW67 View Post
    There's nothing wrong with the read() in the code you show us. It reads two blocks of 256k and the remainder of the file is 182276 bytes assuming the file you have opened contains 706564 bytes.
    The file is bigger than 706564 bytes. I tried with another file with the same size and it read the same way.

    Quote Originally Posted by ChrisW67 View Post
    The "QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState" indicates that the TCP connection to the other end has been closed, possibly deliberately by the other end, possibly some other error condition. Nothing in the code we can see would help diagnose this. Try looking at QTcpSocket::error() or connecting to the error() signal.
    I just take a quick look this morning at that with wireshark and it seems that the client side is resetting (RST) for an unknown reason when the last QTpSocket::write happens in the server side. I will take the time to investigate this afternoon with QTcpSocket::error().

    Thank you for your help,

    Serge

  4. #4
    Join Date
    May 2013
    Location
    Quebec, Canada
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile::read failed after n bytes already read

    Just for the record :

    Finally, I managed my QAbstractSocket::waitForBytesWritten() with the following code from the QT documentation and I don't get the error message
    Qt Code:
    1. client.disconnectFromHost();
    2. if(client.state() == QAbstractSocket::UnconnectedState ||
    3. client.waitForDisconnected(1000))
    4. qDebug("Disconnected!");
    5. else
    6. qDebug() << "Error when DisconnectFromHost :" << client.error();
    To copy to clipboard, switch view to plain text mode 

    The was no "read" error at all. In fact, the problem was in the client side.
    I just forget to read in the QTcpSocket until there was not data. In the exemple in the link that I posted, the client read just one time.
    This way, the server read and write data and the client read it.
    The second time the server read data and write it in the TCPSocket, but it is not read or flushed because the client doesn't read it.
    And the third time, the server read data and it is not able to write it in the socket.

    This is the new client code. The previous code didn't have the while(client->waitForReadyRead()) but just a one time waitForReadyRead. That was my problem from the beginning.
    Qt Code:
    1. QFile file(filename);
    2. if(!(file.open(QIODevice::Append)))
    3. {
    4. qDebug("File connot be opened.");
    5. exit(0);
    6. }
    7. else
    8. {
    9. while(client->waitForReadyRead())
    10. {
    11. QByteArray read = client->read(client->bytesAvailable());
    12. qDebug() << "Read : " << read.size();
    13. qDebug() << "Written : " << file.write(read);
    14. }
    15. }
    16.  
    17.  
    18. file.close();
    To copy to clipboard, switch view to plain text mode 

    Thanks for all.

Similar Threads

  1. QTcpSocket can't read all bytes
    By Qiieha in forum Qt Programming
    Replies: 27
    Last Post: 23rd August 2011, 16:48
  2. Can somebody show how to read bytes?
    By "BumbleBee" in forum Newbie
    Replies: 36
    Last Post: 20th April 2011, 18:57
  3. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 08:38
  4. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 21:23
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15: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.