Results 1 to 6 of 6

Thread: readLine() terminates with the '.' character

  1. #1
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Default readLine() terminates with the '.' character

    Hi All,
    I have a problem with the readLine()-function, it stops reading when it encounters the ‘.’ character. My program connects to a server, and then gives a command to the server.
    ( I have verified that the command is sent to the server, since I have another terminal window open and connected to the server with the telnet IPadress Port command. This terminal displays the "answer" from the terminal)

    The server answers with a string like this 1011.354 1011.312. 1011.100 I have a Qstring variable mystring and a QtcpSocket variable tcpSocket:
    mystring=tcpSocket->readLine();
    qDebug <<mystring; gives output: 1011
    When I add the if (tcpSocket->canReadLine()) before the readLine() the readLine() is never executed.
    Any help is deeply appreciated!
    Regards
    gQt

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readLine() terminates with the '.' character

    What does QTcpSocket::readAll() return? Maybe "1011" is all what's in the buffer?

    I hope you remember that data might arrive in chunks.

  3. #3
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readLine() terminates with the '.' character

    If I think correctly the readLine() canReadLine() expect s "\r\n" "\n" to determine if a line is complete.

  4. #4
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: readLine() terminates with the '.' character

    The problem is that only part of the line has arrived. The slot connected to readyRead() should just return if it cannot read any lines...

    Qt Code:
    1. void SocketClass::readyReadSlot()
    2. {
    3. if (!socket->canReadLine())
    4. return;
    5. do {
    6. QByteArray line = socket->readLine();
    7. } while (socket->canReadLine());
    8. }
    To copy to clipboard, switch view to plain text mode 
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  5. #5
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Default Re: readLine() terminates with the '.' character

    Hi, thanks for your replies!
    When the computer connects to the server with telnet, the terminal displays the string like for example:
    1011.354 1011.312. 1011.100
    after the command (send) is executed. The cursor is placed at the beginning of the next line so the string ends with CR and LF. When the Qt-program writes send (with CR and LF) to the server the terminal displays a string with the same format as above so the buffer has to include the full string. When the program tries to read the buffer with readLine(), the Qstring/QbyteArray variable (I also tried your do while loop Bitto) contains only 1011.
    I find this strange since readLine() works fine when the program connects to and reads from another server. The only difference I can think of is that this other server has strings with data that starts with a $ not a number.

  6. #6
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Smile Re: readLine() terminates with the '.' character

    “What does QTcpSocket::readAll() return? Maybe "1011" is all what's in the buffer?
    I hope you remember that data might arrive in chunks.”

    You are right jacek I had to do several waitForReadyRead():

    Qt Code:
    1. While(!tcpSocket->canReadLine()){
    2. tcpSocket->waitForReadyRead(100)
    3. }
    4. mystring=tcpSocket->readLine();
    To copy to clipboard, switch view to plain text mode 
    Now it works
    Thanks!
    gQt
    Last edited by jpn; 30th May 2008 at 11:39. Reason: missing [code] tags

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.