Results 1 to 5 of 5

Thread: QTCPSocket not getting all data

  1. #1
    Join Date
    Sep 2008
    Location
    Falmouth, MA, USA
    Posts
    34
    Thanks
    4
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTCPSocket not getting all data

    I have a somewhat strange problem, which may not really be a qt problem, but might have more to do with my my understanding of tcp

    However, I have not seen it when programming sockets using the standard socket API under Linux

    I have a linux application that launches a TCP server. It waits for a data request message, and when it gets it, sends

    1) a data start message, containing the number of bytes to be transferred
    2) a series of lines of ascii text, totalling the number of bytes described in the initial message.

    my QT application (running on windows XP, QT version 4.6) always receives the data request message--but does not get all of the line by line data. here's my receiving code:
    Qt Code:
    1. int totalBytesRead = 0;
    2. int byteCount;
    3. bytesToRead = totalBytesToRead;
    4. massiveArray = (char *)malloc(bytesToRead * sizeof(char));
    5.  
    6.  
    7. // here is where I neeed to open a temporary file and write this data out
    8. tempIniFile = new QTemporaryFile;
    9. if(tempIniFile->open()){
    10. char *theTempFile = strdup((char *)tempIniFile->fileName().toAscii().data());
    11.  
    12.  
    13. while(totalBytesRead < totalBytesToRead){
    14. byteCount = iniClient->readLine(massiveArray,bytesToRead);
    15.  
    16. tempIniFile->write(massiveArray,byteCount);
    17. totalBytesRead += byteCount;
    18. bytesToRead -= byteCount;
    19. }
    To copy to clipboard, switch view to plain text mode 
    byteCount will eventually stop being anything but 0--sometimes after 10 lines of data, sometimes after 200, but once it returns zero, I never get anything else from the socket.

    My understanding is that if the reading TCP buffers are full, the sender will block unitl it can write again--I get no errors there, it always shows a successful number of bytes written. Unless I misunderstand, this means they are successfully transferred to the client--but my software isn't getting the data.

    Do I need to implement some handshaking after each line?

    Any other ideas?

    Thanks in advance

    Jonathan Howland

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTCPSocket not getting all data

    Why don't you connect to the signal readyRead() that is sent by the QTcpSocket?

    Btw... have you noticed there exists a QByteArray...?

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

    jhowland (29th January 2010)

  4. #3
    Join Date
    Sep 2008
    Location
    Falmouth, MA, USA
    Posts
    34
    Thanks
    4
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTCPSocket not getting all data

    Quote Originally Posted by boudie View Post
    Why don't you connect to the signal readyRead() that is sent by the QTcpSocket?

    Btw... have you noticed there exists a QByteArray...?
    Thanks! The snippet I sent was actually within a readyRead(), and that was the problem---I was reading all the available bytes from the readyread()--I had to get more readyRead() signals to read more bytes, but I was stuck in my slot function, so never got them.

    I got rid of all the byte counting, and now just signal the end of the transmission. Its much cleaner code, and it actually seems to work.

    And as far as QByteArrays...in reality, my roots are in C programming--well, to be honest, they're in Fortran--, and when I start flailing, I revert to C habits. My new code uses a QByteArray to read the new data--no mallocs, honest!

  5. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTCPSocket not getting all data

    no mallocs, honest!
    OK, we're friends again!

  6. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTCPSocket not getting all data

    Just to add:
    Large data packages don't has to be sent at once through TCP, so you can receive your data in many packages (every time readyRead() will be emitted) so just save expected data size (from that first 'special' package) and then in in slot on readyRead check with bytesAvailable() how many bytes received. If you got less then you are waiting for, then save all the data in some buffer (QByteArray or write directly to file but you can withour readLine() just write what readAll() returns), substract bytesAvailable from all required bytes count and keep waiting for another readyRead() and so on until you get all required data. Then you have complete package in your buffer.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. QTcpSocket + receiving data
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:10
  2. Problem with reading in data from a QTCPSocket
    By Denarius in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2009, 09:54
  3. Problem in printing the data return /read from QTcpsocket
    By skumar434 in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 20:36
  4. Corrupt JPEG data: premature end of data segment
    By node_ex in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2008, 09:57
  5. QTcpSocket and data size - how do I know the size?
    By themolecule in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2007, 02:19

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.