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(){
while ( canReadLine() ) {
ts >> str;
if(str == "A"){
ts >> str;
emit logText( tr("Client > Audio code '%1' has been detected." ).arg(str) );
emit audio();
}
(...)
}
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();
}
(...)
}
To copy to clipboard, switch view to plain text mode
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& ) {}
}
void sendAudioCode( string inCode){
try{
ClientSocket s( "localhost", 4242 );
string outCode = "A ";
outCode.append(inCode);
s << outCode;
}
catch ( SocketException& ) {}
}
To copy to clipboard, switch view to plain text mode
Finally, if anyone wants to consult this class, here is where I found it:
http://linuxgazette.net/issue74/tougher.html
Bookmarks