Writing to sockets and partial write success



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


  • Moderators

    @aatwo

    Why aren't you checking with error() and/or errorString() before you are thinking about traps to find the problem?

    Are you sure that the byteArray is completely as expected?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.