Problem with reading in data from a QTCPSocket
Hello,
I'm getting data from a server in the format:
char* identifier
double timestamp
char* message
double n1
double n2
double n3
When I try to read it, i get 0 bytes.
This is my piece of code:
Code:
void MyClass::ReadData{
while(m_TcpSocket->bytesAvailable() > 0){
blockSize = m_TcpSocket->bytesAvailable();
char* id;
double time;
char* message;
double n1,n2,n3;
in >> id;
in >> time;
in >> message;
in >> n1 >> n2 >> n3;
package = id;
value = time;
package.append(value.toString());
emit messageReady(package);
}
}
messageReady just appends the string into a textfield/browserfield.
Re: Problem with reading in data from a QTCPSocket
In general you must check how many bytes are available before you start reading. You do it but you don't check if the blockSize value is enough to contain data you read.
Have you connected the readin from the socket to the QIODevice::readyRead() signal?
Re: Problem with reading in data from a QTCPSocket
Quote:
Originally Posted by
mcosta
In general you must check how many bytes are available before you start reading. You do it but you don't check if the blockSize value is enough to contain data you read.
Have you connected the readin from the socket to the
QIODevice::readyRead() signal?
Yes the ReadData is only executed when a readyRead is received
Re: Problem with reading in data from a QTCPSocket
are you getting data from your another application? I mean can you change your package structure a bit? Because good thing would be to send at the beginning of your package one int which value would be equal to the size of the whole package. Then if bytesAvailable() is less you are waiting fo anothe part of data to come and if its equal or greater it means you had received whole package and can read it correctly. What's more, I'm a bit suspicius that reading char * in your way can be not so good but i'm not sure...
Re: Problem with reading in data from a QTCPSocket
I found the problem.
I was trying to read it with the wron stream.
I should be reading it with QTextStream.