PDA

View Full Version : Problem with reading in data from a QTCPSocket



Denarius
23rd April 2009, 12:36
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:


void MyClass::ReadData{
while(m_TcpSocket->bytesAvailable() > 0){
blockSize = m_TcpSocket->bytesAvailable();
QDataStream in(m_TcpSocket);
in.setVersion(QDataStream::Qt_4_0);

QVariant value;
QString package;
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.

mcosta
23rd April 2009, 13:24
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?

Denarius
23rd April 2009, 13:38
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

faldzip
23rd April 2009, 14:00
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...

Denarius
24th April 2009, 08:54
I found the problem.
I was trying to read it with the wron stream.
I should be reading it with QTextStream.