Incomplete data in QDataStream when reading from QTcpSocket
-
Hi,
Did you consider using read transactions ?
-
@SGaist said in Incomplete data in QDataStream when reading from QTcpSocket:
Did you consider using read transactions ?
But then you have to make sure that your QDataStream stays alive all the time since this will not work with sequential devices otherwise:
void foo::onDataAvailable() { QDataStream ds(m_socket); ds.startTransaction() if (readFromDataStream(ds)) ds.commitTransaction(); else ds.rollbackTransaction(); }
I had this error in my codebase once and it took some time until I figured out what went wrong.
-
I just validated that calling mServerTask->getBufferX(i) twice does not modify the data. The function is a single line that returns a float. I'm not familiar with read transactions but I'll look into those to try and work around my issue. I'm still puzzled as to what happens with the QDataStream that makes my data "corrupted"...
-
@RBLL said in Incomplete data in QDataStream when reading from QTcpSocket:
out << mServerTask->getBufferX(i);
So when getBuffer() is const this line can not do anything harmful to wBufferX
-
The function was not declared const, but even when making it const, I have the same behavior. In any case, that is the get function : it does not modify the data:
const float ServerTask::getBufferX(size_t index) { return mBufferX[index]; }
Now, for debugging purpose, I added an extra loop in front to see if I would have different results like so:
for (size_t i = 0; i < BUFFERX_SIZE; i++) { wBufferX2[i] = ServerTask->getBufferX(i); } for (size_t i = 0; i < BUFFERX_SIZE; i++) { out << ServerTask->getBufferX(i); wBufferX[i] = ServerTask->getBufferX(i); }
Now, this is the what I have inside wBufferX2:
And this is what I have inside wBufferX:
-
@RBLL said in Incomplete data in QDataStream when reading from QTcpSocket:
wBufferX
Is this memory big enough? Are you on Linux? I would take a look with valgrind since this really looks like a memory allocation problem
-
-
Hi, it could be a transmission timing problem, about 350 floats is pretty close to MTU size (the normal Ethernet packet size). So it could be that while the first packet is xmitted,
out<<...
wreaks havoc with the rest of the data waiting to be xmitted (just guessing :-) -
Maybe that QByteArray is the culprit, you could try rewrite into more vanilla standard:
... { QByteArray block; QBuffer buffer(&block); buffer.open(QIODevice::WriteOnly); QDataStream out(&buffer); ...
at least you would expose more stuff to the debugger :-)