We are quite confused about the behavior of QIODevice::write
in general and the QTcpSocket
implementation specifically. There is a similar question already, but the answer is not really satisfactory. The main confusion stems from the there mentioned bytesWritten
signal respectively the waitForBytesWritten
method. Those two seem to indicate the bytes that were written from the buffer employed by the QIODevice
to the actual underlying device (there must be such buffer, otherwise the method would not make much sense). The question then is though, if the number returned by QIODevice::write
corresponds with this number, or if in that case it indicates the number of bytes that were stored in the internal buffer, not the bytes written to the underlying device. If the number returned would indicate the bytes written to the internal buffer, we would need to employ a pattern like the following to ensure all our data is written:
void writeAll(QIODevice& device, const QByteArray& data) {
int written = 0;
do {
written = device.write(data.constData() + written, data.size() - written);
} while(written < data.size());
}
However, this will insert duplicate data if the return value of QIODevice::write
corresponds with the meaning of the bytesWritten
signal. The documentation is very confusing about this, as in both methods the word device is used, even though it seems logical and the general understanding, that one actually indicates written to buffer, and not device.
So to summarize, the question is: Is the number returned bye QIODevice::write
the number of bytes written to the underlying device, and hence its save to call QIODevice::write
without checking the returned number of bytes, as everything is stored in the internal buffer. Or does it indicate how much bytes it could store internally and a pattern like the above writeAll
has to be employed to safely write all data to the device?
(UPDATE: Looking at the source, the QTcpSocket::write
implementation actually will never return less bytes than one wanted to write, so the writeAll
above is not needed. However, that is specific to the socket and this Qt version, the documentation is still confusing...)