0

I just had difficult time with readyRead() signal from a QUdpSocket and although I found a solution I am still not sure of what I did, I would like to have a more precise idea of why my solution actually works. Here is my situation :

I am connected to a server that emits notifications. When I receive them, i.e. when my QUdpSocket emits onReadyRead(), I handle the notification : unpack data, check where it comes from, etc. This is done in a custom object inheriting QObject.

Then this object emits a signal 'notificationReceived' so that an other object can process the notification : notificationReceived() signal is connected to a processNotification() slot.

processNotification() in turns performs some actions, including performing a new GET request to the server. The server receives the request and responds to it. However the readyRead() signal from the QUdpSocket is not emitted in my application and it acts like if it had never received the datagram from the server.

BUT when I call 'my_socket->hasPendingDatagrams()' it returns true and I can read the datagram that is actually there and corresponds to what I wanted in the GET request :

MyCustomObject::MyCustomObject(QObject* parent){
    // constructor
    my_socket = new QUdpSocket(this);

    if (!m_socket->bind(QHostAddress::Any, m_port, QAbstractSocket::ShareAddress))
        qDebug() << "Could not bind to port" << m_port << m_socket->errorString();

    connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}

void MyCustomObject::onReadyRead(){
    QHostAddress hostAddress;
    QByteArray data;
    quint16 port;

    while (m_socket->hasPendingDatagrams()) {
        data.resize(my_socket->pendingDatagramSize());
        my_socket->readDatagram(data.data(), data.size(), &hostAddress, &port);
    }

    processResponse(data, hostAddress, port);
}

void MyCustomObject::processNotification(QByteArrayData){
    // do some checking and reading of data
    // when finished:
    getNewData();
}

void MyCustomObject::getNewData(){
    // instantiate data, hostAdress, port. In other words :
    // create the request in the form of a datagram to be sent
    my_socket->writeDatagram(data, hostAdress, port);
}

So I tried to learn about readyRead() signal and understand why it was not emitted in this case. Particularly this article gave me a clue : http://www.qtforum.org/article/1118/2/signal-readyread-doesn-t-work.html

It tells about thread issues. But my application runs from a QGuiApplication and everything is done in a single thread (as far as I know, I don't know much about threads actually).

In the end, my solution was : instead of performing my GET request, after receiving the notificationReceived() signal, directly from the processNotification() slot, I call the GET request using a QTimer::singleShot. So I do something like (simplified) :

void processNotification(QByteArray data){
    // check some conditions
    // process the data, and when finished:

   QTimer::singleShot(1000, this, SLOT(getData()));
}

And getRequest() takes care of sending a GET to the server, whose response this time is detected (my_socket emits readyRead() signal). Though I am still not sure why.

Note: it does not matter how much signal(), slot() are emitted/received between notificationReceived() first emitted signal and the call to getRequest() from the final trigerred slot(), if I don't use a QTimer in the end it does not work.

3
  • You might be incorrectly assuming that the HTTP server has actually sent a UDP datagram to you. Use Wireshark to confirm that such a datagram has in fact reached your system. Side note: when you're passing Qt containers and other value types around, pass them by const reference. Thus you should have void processNotification(const QByteArray &data), etc. You can also hold the socket by value, there's no reason to hold it by pointer. Dec 1, 2016 at 22:55
  • Yes I am sure I the server sends a datagram, first because I see that it is send on the server side, second because if I call my_socket->hasPendingDatagrams() I actually receive it on the client. The problem is only that onReadyRead() is not emitted even though some data are waiting to be read.
    – MBoh
    Dec 2, 2016 at 5:59
  • OK then, what's the exact OS version & build, and what Qt version is it on? Dec 2, 2016 at 14:28

1 Answer 1

1

I think that your problem was that you were doing everything in the same thread and event loop.

i.e. in your readyRead() slot you were calling processResponse(...) and that would be executed immediately, then when processResponse was signaling "notificationReceived()" and calling processNotification() and that in turn would finally call getNewData().

All those calls would be done "in line" (see this link about Qt's DirectConnetion for SIGNALs) so you were basically requesting for newData while still being in the readyRead() function call.

If the server responded before your code flow returned to the normal event loop then you would be missing the new readyRead() call.

Why using a QTimer solved your issue? because basically you were deferring the getNewData and your call stack would leave the readyRead() and go to normal event loop before you actually requested some new data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.