Return Value QTcpSocket :: write (QByteArray & buf);

Dosing this function always returns buf.size () or -1? if not, does the dose mean that I need to be reminded that the left data recording function is not being recorded?

for example, if I have 100 bytes of QByteBuffer. when I call "tcpSocket.write (buf_100_bytes)", is it possible that I get 60 or something else?

Also, does the dose of this function return immediately?

+2
source share
2 answers

As with POSIX write (), QIODevice :: write () returns the number of bytes written. It can be any number from 0 to the size of the buffer. In addition, in the event of an error, it may return a negative number, which you must check separately. QIODevice :: write () does not block sockets (they are set to non-blocking mode), bytes are simply added to the buffer and written later. To be notified when writing bytes, you can connect to a bytesWritten (qint64) signal. To lock until bytes are actually written, you can use waitForBytesWritten () (usually this is not a good idea in the main / user interface thread).

+2
source

I quote the Qt documentation:

Writes no more than maxSize bytes of data from data to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.

This means that it will return the number of bytes written or -1 in case of an error. You can get an error by calling the error () method or by connecting to the error () signal.

0
source

All Articles