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. QUdpSocket::waitForBytesWritten() always returns false.
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket::waitForBytesWritten() always returns false.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.9k 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.
  • M Offline
    M Offline
    mnbv
    wrote on last edited by
    #1

    When I do something like this (Qt 4.8):

    @
    QUdpSocket socket;
    socket.connectToHost(address, port);
    socket.write(data, size);
    socket.waitForBytesWritten();
    @

    The call to write() succeeds and returns size, but waitForBytesWritten() always returns false and socket.errorString() yields "Unknown error". Why does this happen and how do I wait for the data to be sent without using an event loop? I can find nothing in the documentation that indicates that waitForBytesWritten() does not work for QUdpSocket.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mnbv
      wrote on last edited by
      #2

      I apologize if this is a silly question with an obvious answer but I still am wondering about this. Thanks!

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wallyh010
        wrote on last edited by
        #3

        I am experiencing the exact same problem with QTcpSocket.
        In the source code below, the data is successfully sent to the server, but waitForBytesWritten always returns false.

        @
        bool TCPClient::sendData(const char* dataToSend, qint64 bytesToSend)
        {
        QMutexLocker writeMutexLocker(&mWriteMutex);

        Logger::getInstance()->write(LogLevel::Info,
                                    "TCPClient::sendData(), bytes: " +
                                    QString::number(bytesToSend));
        
        qint64 numBytesSent = mTCPSocket.write(dataToSend, bytesToSend);
        mTCPSocket.flush();
        if(-1 == numBytesSent)
        {
          Logger::getInstance()->write(LogLevel::Info,
                                       "TCPClient::sendData(): ERROR!");
        }
        
        // send synchronously
        bool bytesWrittenStatus = mTCPSocket.waitForBytesWritten(5000);
        if(false == bytesWrittenStatus)
        {
            QString errorString = mTCPSocket.errorString();
            Logger::getInstance()->write(LogLevel::Info,
                                       "TCPClient::sendData(): ERROR! Failed to "
                                       "write to the TCP Socket! " + errorString);
        }
        return bytesWrittenStatus;
        

        }
        @

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zslane
          wrote on last edited by
          #4

          I ran into this problem as well, and for me the reason waitForBytesWritten() failed--even though the correct number of bytes were sent over the socket--was because I was calling it from a thread other than the one which created the socket object.

          Apparently the socket methods that "wait" for I/O states to change are very thread-sensitive, most likely due to the dependence on being in sync with the correct event loop.

          1 Reply Last reply
          0

          • Login

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