You could simply use the readLine(...) variants:
qint64 QIODevice::readLine(char * data, qint64 maxSize)
QByteArray QIODevice::readLine(qint64 maxSize = 0)
Note, read line functionality would be invoked on your QTcpSocket instance, and not the QDataStream. See the documentation for the precise details here inline:
A terminating '\0' byte is always appended to data, so maxSize must be larger than 1. Data is read until either of the following conditions are met: The first '\n' character is read. maxSize - 1 bytes are read. The end of the device data is detected.
This way, you would need to work such an issue around, and the code could become a lot simpler.
One further note that may be helpful:
bool QIODevice::canReadLine() const [virtual]
Basically, this method would guarantee you do not try to run a line before that is available. This can come to handy in certain scenarios.