Results 1 to 5 of 5

Thread: Problem with QTcpSocket and QDataStream

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with QTcpSocket and QDataStream

    The problem is that my run function is returning only the first few characters (up until the first whitespace actually) of the data that has arrived on the socket. So it returns

    Qt Code:
    1. 200
    To copy to clipboard, switch view to plain text mode 

    instead of

    Qt Code:
    1. 200 newsreader2.eweka.nl NNRP Service Ready - news.eweka.nl (posting ok)
    To copy to clipboard, switch view to plain text mode 

    Anyone know why? It works fine if I uncomment the readLine() function, but I'm trying to get it to work using a datastream.

    Qt Code:
    1. void Connection::run()
    2. {
    3. QTcpSocket socket;
    4. const int Timeout = timeout * 1000;
    5. emit emitData( "run() commencing" );
    6. quit = false;
    7. socket.connectToHost( hostName, port );
    8. if( !socket.waitForConnected( Timeout ) ){
    9. emit emitData( "returning ( socket.waitForConnected )" );
    10. return;
    11. }
    12. while(!quit){
    13. emit emitData( "entering loop" );
    14. if( !socket.waitForReadyRead( Timeout ) ){
    15. emit emitData( "returning ( socket.waitForReadyRead )" );
    16. return;
    17. }
    18. QString data;
    19. QTextStream dataStream( &socket );
    20. // while( socket.canReadLine() ){
    21. // emit emitData( socket.readLine() );
    22. // emit emitData( QString::number( socket.bytesAvailable() ) );
    23. // if( socket.waitForReadyRead( Timeout ) ){
    24. // emit emitData( "returning ( socket.bytesAvailable )" );
    25. // return;
    26. // }
    27. // }
    28. dataStream >> data;
    29. emit emitData( data );
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

  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: Problem with QTcpSocket and QDataStream

    QDataStream is very specific, it requires data to be formatted the way it likes it. I suggest you use QTextStream (but it'll stop at every whitespace) or use readLine() directly. If you really need/want QDataStream, use QDataStream::readRawData().

  3. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QTcpSocket and QDataStream

    Thanks, just before you posted that I found out about QTextStream.readLine()

  4. #4
    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: Problem with QTcpSocket and QDataStream

    Quote Originally Posted by Valheru
    Thanks, just before you posted that I found out about QTextStream.readLine()
    Note that using QTextStream is a bit slower than operating on the device directly. So if QTextStream::readLine() is the only method of that class you use, you could consider calling readLine on the socket instead.

  5. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QTcpSocket and QDataStream

    Hmm, the reason why I am trying to stream the data is that because in the past with an earlier version of my program, I was getting strange errors when processing large amounts of data on the socket. I would list a newsgroup, and recieve the list of titles of the (in the form of the post number) from the newsserver. However, this only worked up until a certain number of posts. When listing large newsgroups, such as alt.binaries.boneless, after a while I would start getting an error. I can't remember what exactly, something along the lines of maxLen() > 2 ... I was processing the readLine() of the socket, adding the information directly into a QListWidget.

Similar Threads

  1. 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.