Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions
Writing to sockets and partial write success
aatwo last edited by aatwo
Hi guys. I have a question about writing to sockets (e.g. QTcpSocket, QLocalSocket etc). I have an app that writes to a QTcpSocket using the function:
QByteArray myData; // Setup myData qint64 writeCount = mySocket->write( myData );
There seems to be a very rare case where sometimes the buffer will only partially write successfully. For example if myData is 100 bytes long, it might only write 50 bytes and return 50. And this must be pretty rare cause I don't think I've ever seen any networking code written using Qt that actually bothers to check the return value of QIODevice::Write().
So two questions.
What can be a cause of this (to help me reproduce it)? I'm led to believe this can be caused by a heavy network load, but I've tried to reproduce it out of curiosity using test apps without success.
And what's the proper fix? Can I just block and keep calling write until every byte has been written to the socket successfully? Or do I need to set a timer and try to write any remaining data later?
Kind regards, Aaron.
koahnig last edited by
Are you sure that the byteArray is completely as expected?