⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Warning: hasPendingDatagrams called in UnconnectedState. Huh?

  • Qt 4.8.0 on Windows.

    I am attempting to read data with a QUdpSocket. The socket has been bound to a port with bind(). When I attempt to read datagrams in a loop, e.g.:

    @ while (socket_.hasPendingDatagrams())
    socket_.readDatagram(data, sizeof(data));

    No datagrams are read (despite Wireshark showing data arriving with a correct destination port) and the console displays:

    @QNativeSocketEngine::hasPendingDatagrams() was called in QAbstractSocket::UnconnectedState

    Obviously hasPendingDatagrams() is called in UnconnectedState -- it's a UDP socket. It's never connected.

    Additionally, when I try using the event loop instead, readyRead() is never emitted.

    Using Winsock directly, I have no issues. I do not want to do that. Nothing about that console message makes sense, and everything I've done is 100% as per the documentation. How do I convince Qt that UDP sockets are supposed to be in UnconnectedState, and actually receive data on the socket? As it stands I have not been able to successfully use a QUdpSocket to perform the seemingly simple task of receiving data.

  • Have you bound the QUdpSocket to a port with QUdpSocket::bind()?

  • Did you check the return value of bind()?

  • how to check the return value of Qt bind()

  • Lifetime Qt Champion

    Please, look at the function documentation. It returns a boolean value.

Log in to reply