Results 1 to 6 of 6

Thread: QtcpSocket read behavior

  1. #1
    Join Date
    Oct 2009
    Location
    Mantova, Italy
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default QtcpSocket read behavior

    Hi.
    I'm trying to understand the read behavior of QTcpSocket::read(maxSize) function.
    i have the following code (on the server):

    Qt Code:
    1. #include "server.h"
    2. #include <QTcpSocket>
    3. #include <iostream>
    4. server::server()
    5. {
    6. s=new QTcpServer;
    7.  
    8. }
    9. void server::active(){
    10. s->listen(QHostAddress::Any,5500);
    11. connect(s,SIGNAL(newConnection()),this,SLOT(nc()));
    12. }
    13. void server::nc(){
    14.  
    15. QTcpSocket *so=s->nextPendingConnection();
    16.  
    17. while(true){
    18. std::cout <<"wait read..."<<std::endl;
    19. so->waitForReadyRead(5000000);
    20. QByteArray a=so->read(1000);
    21. if (a.endsWith("\r\n"))
    22. a.resize(a.length()-2);
    23. if (a.startsWith("quit"))
    24. break;
    25.  
    26. //just to see readed text, this line will be deleted
    27. std::cout <<a.length()<<"--"<<QString(a).toStdString()<<std::endl;
    28. }
    29. so->disconnectFromHost();
    30. }
    To copy to clipboard, switch view to plain text mode 

    and for testing, i use putty to connect in raw mode to the server.
    So, if i send with putty these lines:
    Qt Code:
    1. testline1
    To copy to clipboard, switch view to plain text mode 

    the server will output this
    Qt Code:
    1. wait read...
    2. 9--testline1
    3. wait read...
    4. 0--
    5. wait read...
    To copy to clipboard, switch view to plain text mode 

    analizing the behavior, line 1,2 and 3 are ok, but line 4 is unexpected(or not?)... if i input other lines like:
    Qt Code:
    1. testline1
    2. testline222222
    3.  
    4. lastwasempty
    To copy to clipboard, switch view to plain text mode 
    note that line 3 is empty

    the server output is:
    Qt Code:
    1. wait read...
    2. 9--testline1
    3. wait read...
    4. 0--
    5. wait read...
    6. 14--testline222222
    7. wait read...
    8. 0--
    9. wait read...
    10. 0--
    11. wait read...
    12. 12--lastwasempty
    13. wait read...
    14. 0--
    15. wait read...
    To copy to clipboard, switch view to plain text mode 

    like previois example example, i can't understand why lines 4,8 and 14 are present...(obviously same thing for the "wait read" related lines, but this is not important)
    some explanations?
    Last edited by grisson; 9th August 2010 at 14:16.

  2. #2
    Join Date
    Oct 2009
    Location
    Mantova, Italy
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QtcpSocket read behavior

    up, other tests, same problem

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QtcpSocket read behavior

    Your code works fine on my Debian box. I use netcat to connect (nc localhost 5500) and using your input get the following on the console:
    Qt Code:
    1. wait read...
    2. 10--testline1
    3. wait read...
    4. 15--testline222222
    5. wait read...
    6. 1--
    7. wait read...
    8. 13--lastwasempty
    9. wait read...
    To copy to clipboard, switch view to plain text mode 
    Try putting in a qDebug() statement to see what you're actually receiving:
    Qt Code:
    1. QByteArray a=so->read(1000);
    2. qDebug() << a.toHex();
    3. if (a.endsWith("\r\n"))
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtcpSocket read behavior

    Note that you may not receive data in the same size as what was sent. So if you sent "hello\r\n" for example, you might receive "hello" in one read and "\r\n" in another.

    Therefore put your debugging before you strip such a sequence and you may find this is happening.

  5. #5
    Join Date
    Oct 2009
    Location
    Mantova, Italy
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QtcpSocket read behavior

    Try putting in a qDebug() statement to see what you're actually receiving:
    line added.

    Maybe is exactly this the problem (or maybe it's a putty problem) because i've done 2 tests:
    TEST 1:
    1) server on a ubuntu based vps piloted via ssh(putty)
    2) raw connection with putty to server on port 5500
    the lines sended are:

    Qt Code:
    1. line
    To copy to clipboard, switch view to plain text mode 
    and the server print(che std::cout line was deleted):
    Qt Code:
    1. wait read...
    2. "6c696e65"
    3. wait read...
    4. "0d0a"
    To copy to clipboard, switch view to plain text mode 

    TEST 2:
    with this test:
    1) same as last test
    2) another ssh session to vps, and now the connection is done simply by "telnet localhost 5500"

    the line sent was the same as last test ("line")

    but the result was:
    Qt Code:
    1. wait read...
    2. "6c696e650d0a"
    To copy to clipboard, switch view to plain text mode 

    maybe can be putty that do something strange before send the data?

    Note that you may not receive data in the same size as what was sent. So if you sent "hello\r\n" for example, you might receive "hello" in one read and "\r\n" in another.

    Therefore put your debugging before you strip such a sequence and you may find this is happening.
    ok..but if it's a normal behavior, how cqan i recognize the differences between a blank line sended (which receive only "\r\n") and a non-blank line which receive the data in one read and the \r\n in another?
    Last edited by grisson; 14th August 2010 at 15:24.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QtcpSocket read behavior

    You could change your logic a bit:
    Qt Code:
    1. while(true){
    2. so->waitForReadyRead(5000000);
    3. a.append(so->read(1000));
    4. if(a.endsWith("\n")) {
    5. if(a.endsWith("\r\n")) a.chop(2); else a.chop(1);
    6. qDebug() << a ;
    7. a.clear();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. ambiguous documentation and behavior of QTcpSocket
    By moviemax in forum General Programming
    Replies: 1
    Last Post: 4th June 2010, 23:11
  2. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:42
  3. Unable to read from QTcpSocket
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 21:09
  4. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 20:23
  5. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44

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.