Results 1 to 6 of 6

Thread: QtcpSocket read behavior

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.