PDA

View Full Version : readLine() terminates with the '.' character



gQt
28th May 2008, 15:48
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

jacek
28th May 2008, 16:35
What does QTcpSocket::readAll() return? Maybe "1011" is all what's in the buffer?

I hope you remember that data might arrive in chunks.

MaximA
28th May 2008, 17:58
If I think correctly the readLine() canReadLine() expect s "\r\n" "\n" to determine if a line is complete.

Bitto
28th May 2008, 21:09
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...



void SocketClass::readyReadSlot()
{
if (!socket->canReadLine())
return;
do {
QByteArray line = socket->readLine();
} while (socket->canReadLine());
}

gQt
29th May 2008, 12:40
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.

gQt
30th May 2008, 11:31
“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():


While(!tcpSocket->canReadLine()){
tcpSocket->waitForReadyRead(100)
}
mystring=tcpSocket->readLine();

Now it works :)
Thanks!
gQt