Results 1 to 6 of 6

Thread: QTextStream setup for networking?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    61
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: QTextStream setup for networking?

    Quote Originally Posted by wysota View Post
    Whenever you see fit.It's best to create it when the device it operates on is created and keep the stream until the device is destroyed.
    Ok. Will the stream object become useless when the socket is disconnected (after a connection) and thus needs to be re-initialized if I want to reconnect?

    If you want to read lines, there is no sense to use the stream. It such case use QIODevice::readLine() instead.
    What about if I dont want to read a line, but rather use XML either through SAX or an XML stream? I guess the same problem is valid for this case. The problem is of a general nature: What if I want to read something from a _stream_ connected to a socket. Will it block the thread?
    Last edited by invictus; 23rd February 2008 at 14:45.

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

    Default Re: QTextStream setup for networking?

    Quote Originally Posted by invictus View Post
    Ok. Will the stream object become useless when the socket is disconnected (after a connection) and thus needs to be re-initialized if I want to reconnect?
    The stream operates on the underlying device. If the device has data to be read, the stream should work fine. Thus when you reconnect (which means reopening the device) the stream will be able to read/write data.

    What about if I dont want to read a line, but rather use XML either through SAX or an XML stream?
    Then you don't need a text stream. It's useful only if you want to read separate words. If you want to use Qt classes, all of them will take a QIODevice as its parameter. See QDomDocument::setContent for an example.
    What if I want to read something from a _stream_ connected to a socket. Will it block the thread?
    No, it will tell you there is nothing to read.

  3. #3
    Join Date
    Feb 2007
    Posts
    61
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: QTextStream setup for networking?

    So if I connect a QXmlStreamReader to the socket and in the readyRead() slot I use the reader.readNext() it won't block even though there are no "complete" token to read next in the buffer? I hope it won't say that it's the end of the stream

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

    Default Re: QTextStream setup for networking?

    Quote Originally Posted by invictus View Post
    So if I connect a QXmlStreamReader to the socket and in the readyRead() slot I use the reader.readNext() it won't block even though there are no "complete" token to read next in the buffer? I hope it won't say that it's the end of the stream
    It will probably return an invalid or no token.

    I suggest you read all data and concatenate it to a string and then pass the whole string to the xml reader. Of course unless you implement a complex and complete handler for the xml. In such situation you should handle cases where the xml you read is invalid or incomplete so you can't really perform any actions until you read until the end of the xml stream. You can build a syntax tree or something like that and "execute" it when you read the whole xml document though.

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.