Results 1 to 11 of 11

Thread: QTcpSocket reading stucks

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default QTcpSocket reading stucks

    Hi there! I am coding NNTP client application. It is convenient to read data line-by-line in blocking mode, so I do it in the next way:

    Qt Code:
    1. while (s != ".")
    2. {
    3. socket.waitForReadyRead();
    4. QByteArray b = socket.readLine();
    5.  
    6. s = QString(b);
    7. }
    To copy to clipboard, switch view to plain text mode 

    The problem is the app at some point begins reading lines slow and after some time stops reading at all. However, the following code works fine:

    Qt Code:
    1. while (!b.endsWith("\r\n.\r\n"))
    2. {
    3. QByteArray portion;
    4. portion = socket.readAll();
    5. b.append(portion);
    6. }
    To copy to clipboard, switch view to plain text mode 

    What can be the issue and is there any solution for it?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket reading stucks

    You are reading one line yet more than one line could be already waiting in the socket. Eventually you're lagging so much that the receive buffer fills completely and the socket doesn't receive any more data waiting for you to read what's already there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket reading stucks

    thank you!

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Question Re: QTcpSocket reading stucks

    Just noticed that reading large amounts of data via QTextStream gets stuck while reading via QTcpSocket works fine.

    The sample code works without any problems:
    Qt Code:
    1. while (s != ".")
    2. {
    3. if (!socket.canReadLine())
    4. socket.waitForReadyRead();
    5.  
    6. // using socket readLine method
    7. s = QString(socket.readLine()).simplified();
    8. }
    To copy to clipboard, switch view to plain text mode 

    The following sample works slow and eventually hangs up:
    Qt Code:
    1. QTextStream ts(&socket);
    2. while (s != ".")
    3. {
    4. if (!socket.canReadLine())
    5. socket.waitForReadyRead();
    6.  
    7. // using QTextStream readLine method
    8. s = ts.readLine();
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket reading stucks

    I suggest you change your code to non-blocking approach, then it's much easier:
    Qt Code:
    1. QTextStream ts(&socket); // declared elsewhere
    2. void Sth::onReadyRead(){
    3. while(socket.canReadLine()){
    4. s = ts.readLine();
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    If you insist on having it blocking:
    Qt Code:
    1. bool stop = false;
    2. while(!stop) {
    3. while(!stop && socket.canReadLine()) {
    4. s = ts.readLine();
    5. if(s.trimmed()==".") stop = true;
    6. }
    7. if(!stop) socket.waitForReadyRead(5000);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Your code is faulty bvecause you assume that after waitForReadyRead() there will be a complete line in the socket.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket reading stucks

    Yes, I understand, but due to short lines practically it always receives the whole line after that signal (my first sample never gets stuck although I wait for readyRead signal only once).

    Your code doesn't work either, but if I simply change
    Qt Code:
    1. s = ts.readLine()
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. s = QString(socket.readLine())
    To copy to clipboard, switch view to plain text mode 
    everything works like a charm.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket reading stucks

    The two lines of code are not equivalent!

    due to short lines practically it always receives the whole line after that signal
    So your program will "practically work" instead of just working. If the line is at least two bytes long (and if it not empty then it always is) then due to network congestion (which is out of your control) it can be fragmented into more than one tcp segment and your code will start returning empty lines or worse.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket reading stucks

    The two lines of code are not equivalent!
    so what is wrong with the first line?
    So your program will "practically work" instead of just working....
    yes, yes... i already changed that, just wanted to say there is another issue

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket reading stucks

    Quote Originally Posted by mentalmushroom View Post
    so what is wrong with the first line?
    It may be using a different encoding than QTextStream. There is no point in using QTextStream to read line-by-line if you want the same behaviour as when reading directly from the socket.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket reading stucks

    Thanks again. I thought QTextStream is right for that purpose - reading text from different devices and it automatically removes \r\n from the line end

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket reading stucks

    QTextStream is for reading text token by token (word by word). If you use readLine() it behaves in the same way as QIODevice::readLine() only that it uses a given text codec on the data being read.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. delay while reading data from QTcpSocket every 20ms
    By laedri in forum Qt Programming
    Replies: 3
    Last Post: 19th October 2010, 12:54
  2. Problem with reading in data from a QTCPSocket
    By Denarius in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2009, 08:54
  3. QTcpSocket writing reading problem
    By Misenko in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 07:27
  4. Reading non-ASCII lines from QTcpSocket via readLine()
    By joshtn in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2008, 23:52
  5. Replies: 6
    Last Post: 8th January 2007, 10:24

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.