Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sockets

qt

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 "tcpSocket.write(buf_100_bytes)" , is it possible that I get 60 or something else?

Additionally, dose this function return immediately?

like image 224
jnblue Avatar asked Nov 26 '25 09:11

jnblue


1 Answers

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).

like image 114
Frank Osterfeld Avatar answered Nov 28 '25 22:11

Frank Osterfeld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!