Hi there!
You need to make use of the tcpSocket->bytesAvailable(). And I think you need to keep track of your packet length, because its not guaranteed to arrive in one piece:
writing:
// fill your data...
out << data.size() << data;
QByteArray data;
// fill your data...
QDataStream out(tcpSocket);
out << data.size() << data;
To copy to clipboard, switch view to plain text mode
reading: You need a member variable in client: int nextBlockSize
void Client::readFortune()
{
if (nextBlockSize == 0) {
if (tcpsocket->bytesAvailable() < sizeof(int)) return;
in >> nextBlockSize;
}
if ((int)this->bytesAvailable() < nextBlockSize) return;
// do something with your data..
// Prepare for next block
nextBlockSize = 0;
}
void Client::readFortune()
{
QDataStream in(tcpsocket);
in.setVersion(QDataStream::Qt_4_0);
if (nextBlockSize == 0) {
if (tcpsocket->bytesAvailable() < sizeof(int)) return;
in >> nextBlockSize;
}
if ((int)this->bytesAvailable() < nextBlockSize) return;
QByteArray data = tcpsocket->read(nextBlockSize);
// do something with your data..
// Prepare for next block
nextBlockSize = 0;
}
To copy to clipboard, switch view to plain text mode
If there is a more elegant solution I would be happy to hear about it - But this one should work!
Joh
PS: I didn't compile the code.. pasted it from an old project and simplified it. there could be some syntax errors :->
Bookmarks