No, it's still wrong, you are discarding previous content of the buffer.
This is correct (by the way, there are numerous threads in this forum where I explain this particular situation, how come you didn't encounter any of the threads while using our search?):
void Cls::onSocketReadyRead() {
buffer.append(socket->readAll());
tryProcessData();
}
void Cls::tryProcessData() {
forever {
// for the sake of the example I'm assuming my record is always 24 bytes long
if(buffer.size()<24) return;
processRecord(record);
}
}
QByteArray buffer; // global or member variable
void Cls::onSocketReadyRead() {
buffer.append(socket->readAll());
tryProcessData();
}
void Cls::tryProcessData() {
forever {
// for the sake of the example I'm assuming my record is always 24 bytes long
if(buffer.size()<24) return;
QByteArray record = buffer.left(24);
processRecord(record);
}
}
To copy to clipboard, switch view to plain text mode
The above code has one small flaw introduced for purpose so that you need to understand completely what the code does before actually using it.
Bookmarks