PDA

View Full Version : Serialized QByteArray length



giantdragon
21st May 2011, 16:54
I want to send serialized QByteArray (may have arbitrary size) via TCP socket. I have read that signal readyRead() can be emitted even not all data available and user must manually check it by calling bytesAvailable(). But I didn't found how to get array's size on receiver side.

squidge
21st May 2011, 17:49
Once way would be to send the array size first, and then send the array. That way you know the size of the array on the receiver side.

giantdragon
21st May 2011, 18:17
What is minimal size when readyRead signal emitted? Is it guaranteed more than 4 bytes?

squidge
21st May 2011, 21:03
It could be 1 byte.

If your array will be less than 255 characters then send 1 byte for the length.
If your array will be less than 65535 characters then send 2 bytes for the length and ensure you have 2 bytes before assuming you have the length.
etc.

giantdragon
21st May 2011, 23:10
It could be 1 byte.

If your array will be less than 255 characters then send 1 byte for the length.
If your array will be less than 65535 characters then send 2 bytes for the length and ensure you have 2 bytes before assuming you have the length.
etc.
Thanks.
I think it will be better to send always 32-bit interger (qint32) and check bytesAvailable >= 4.