开发者

return value of QTcpSocket::write(QByteArray& buf);

Dose this function always return buf.size() or -1? if not ,dose it mean I need recall the function to write the left data not be written?

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

Additionally, dose this function return immediately?


As with POSIX write(), the QIODevice::write() returns the number of bytes written. That can be any number between 0 and the buffer size. Also, in case of an error, it might return a negative number, which you should check for separately. QIODevice::write() does not block for sockets (they are set to non-blocking mode), the bytes are just added to a buffer and written later. To get a notification when bytes are written, you can connect to the bytesWritten(qint64) signal. To block until the bytes are actually written, you can use waitForBytesWritten() (usually not a good idea in the main/UI thread).


I quote Qt documentation:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜