PDA

View Full Version : Problem with QTextStream



eter
21st November 2006, 03:33
EDIT:

My problem is that canReadLine() returns FALSE even if there is text, I tried to append /n to my string and still the incoming text is not percieved as a "complete" line. Any ideas?

details below (sorry for the lenghty text)

I'm designing a program to communicate with a remote system. Server side, it reads messages that contain a header (a capital letter) followed by a space and then the appropriate data (i.e. A 12 ... A tells me what the client is sending, 12 is the value).

I use the << operator to read a word from the stream.




void readClient(){

QTextStream ts( this );

while ( canReadLine() ) {

QString str = "";

ts >> str;

if(str == "A"){
ts >> str;
emit logText( tr("Client > Audio code '%1' has been detected." ).arg(str) );
emit audio();
}

(...)
}


If I use a simple client application from the QT tutorials to send messages from the client to the server, this code works perfectly.

However, since the robot the client application will be running on doesn't need any sort of GUI and from what I understand you can't use the QSocket class unless you create a QApplication, I decided to use a simple tcp class I found on the internet for the client side app, which in theory does everything I need it to do.

Again, using this code for the client app, I know the client connects to the server correctly, and I know the message gets there because ts.read() returns the appropriate string, but the canReadLine() function still returns FALSE.

Here is my usage of the class ClientSocket.



void sendAudioCode( string inCode){
try{
ClientSocket s( "localhost", 4242 );
string outCode = "A ";
outCode.append(inCode);

s << outCode;
}
catch ( SocketException& ) {}

}


Finally, if anyone wants to consult this class, here is where I found it:
http://linuxgazette.net/issue74/tougher.html

Bitto
21st November 2006, 07:46
You can't reliably use QIODevice::canReadLine(), which is a QByteArray based function, with QTextStream's readLine(). In Qt 3 this works only sometimes for Latin-1 encoded text, because QIODevice::canReadLine() searches for the '\n' character in the raw data, and that happens to map to a '\n' unicode character using latin-1. For other character encodings, it breaks horribly. And in Qt 4, it doesn't work at all.

If you know your data is latin-1 or octet-based (which is often the case for sockets), you should use QIODevice::readLine() instead.

eter
21st November 2006, 15:54
You can't reliably use QIODevice::canReadLine(), which is a QByteArray based function, with QTextStream's readLine(). In Qt 3 this works only sometimes for Latin-1 encoded text, because QIODevice::canReadLine() searches for the '\n' character in the raw data, and that happens to map to a '\n' unicode character using latin-1. For other character encodings, it breaks horribly. And in Qt 4, it doesn't work at all.

If you know your data is latin-1 or octet-based (which is often the case for sockets), you should use QIODevice::readLine() instead.

thanks for the clarification.