Results 1 to 2 of 2

Thread: Read text lines using QDataStream or QTextStream or neither from tcpsocket?

  1. #1
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Read text lines using QDataStream or QTextStream or neither from tcpsocket?

    I am creating a simple TCP server, and have a slot/function built which reads incoming text from a client (telnet connection) on a TCP socket. I've used the Fortune code examples to help me, but had to remove QDataStream since it didn't work as expected.

    I want my readCommand function to collected incoming characters from a telnet client connection, and once it finds a newline or return to remove the typed command from the input buffer, remove /n and /r, add it to my string list (commandList), and then echo the commands (seperate function). Here's what I've got so far:

    Qt Code:
    1. void MyServer::readCommand()
    2. {
    3. inBuffer += tcpSocketPtr->readAll();
    4.  
    5. // While newline is present, extract the command
    6. int nextNewlinePos;
    7. while ((nextNewlinePos = inBuffer.indexOf('\n')) > -1) {
    8. commandList << inBuffer.left(nextNewlinePos);
    9. inBuffer.remove(0,nextNewlinePos);
    10. // Get rid of /n /r if present somehow
    11. }
    12. if (commandList.size() > 0)
    13. {
    14. echoCommand();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Before I start stripping /n and /r etc. manually, my gut is telling me there is a better way to do this. Is QTextStream the way to go? Can someone provide a simple(r) alternative to what I am trying to achieve? Note that QTextStream's readline won't work since it will return the contents of inbuffer in the case that there is NO newline, or the max string length is reached. I somehow need to test for newlines still...
    Last edited by ocgltd; 28th September 2013 at 00:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Read text lines using QDataStream or QTextStream or neither from tcpsocket?

    Have a look at QIODevice::canReadLine()

    I would go for a QBuffer that always gets the available data from the socket (clearing network buffers as soon as possible) and is then used as the IO device for the text stream.

    Cheers,
    _

Similar Threads

  1. QTextStream cannot read £ characters?
    By KjellKod in forum Qt Programming
    Replies: 10
    Last Post: 15th August 2011, 14:58
  2. Replies: 1
    Last Post: 5th August 2009, 11:44
  3. Replies: 5
    Last Post: 27th May 2009, 12:49
  4. How to write to a TCPSocket with a QTextStream?
    By Denarius in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2009, 12:28
  5. Replies: 4
    Last Post: 8th January 2008, 18:41

Tags for this Thread

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.