Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing to sockets and partial write success
Forum Updated to NodeBB v4.3 + New Features

Writing to sockets and partial write success

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 619 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aatwo
    wrote on last edited by aatwo
    #1

    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.

    K 1 Reply Last reply
    0
    • A 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.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved