Congratulations to our 2022 Qt Champions!

QUdpSocket stop receiving data

  • Hi everyone,

    I am currently trying to receive data in QT from Matlab within a localhost. The Matlab application is sending 176 bytes of data at a certain period. I have been observing the data received by Qt, using a variable that keep counts of the total number of bytes received. Initially, Qt is receiving the data without any problem. However, after sometime Qt stop receiving the data although Matlab is sending the data properly. What could have stopped the QUdpSocket from receiving the data (QUdpSocket stop emitting readyRead() signal)?

    And is there a way to overcome this issue? For example, initializing a timer that reset the socket if no data is received in a period of time

    1)The QUdpSocket is running in a worker thread.
    2) Qt Version 5.3
    3) The number of bytes received by Qt before it stops receiving varies. In first trial it managed to receive a total of 41kB. Second iteration 32kB and so on.

  • Is it possible something else is getting the udp data? UDP data is only sent once and can only be received once, but any UDP socket can receive and keep it.

    Maybe you have some other app on your system intercepting that data after a while?

  • Qt Champions 2017

    I suspect the issue with reading. Can you check how you are processing the data ? You may not be reading the data in some condition. unless you read the data readyRead signal will not be emitted.

    Can you check if thread is still running ? I know this is remote chance.

  • Hi guys,

    Thanks for the input. I think I have solved the issue. The threads are running fine. It turned out that, this problem occurs when I create the UDP socket in the worker thread class without passing its parent.

    Instead of declaring :
    @QUdpSocket *socket = new QUdpSocket;@

    It should be:
    @QUdpSocket *socket = new QUdpSocket(this);@

    After fixing this, I could continuously receive data with a period of 25ms without any disruption.

  • That's kinda weird. I'm glad it's solved for now, but I can't really see any cases where having a parent would matter for receiving data.

    Especially since it would receive some but not all the data. Very weird indeed. Glad it's working though.

  • Exactly. I find it very weird though. And I am still not sure how this could solve the problem. But I have tested more than once and it is no longer failing. If I remove the parent, it fails.

  • Just out of curiousity, what type of object is "this"?

  • "this" is just a class I created myself to receive and process the data. After processing, it emit signals by transmitting the data received as a parameter to some other class. I have inherited the class from QObject though and it runs in a worker thread

  • Hmm, so no reason there. Oh well, I'll stop thinking about it now since it's working for you. I just hate not knowing why something works, lol. :)

  • Same here. :)

  • Lifetime Qt Champion


    Just an educated guess, you wrote that the socket is in a worker thread so I presume that it's a worker object that you used moveToThread on ? If so, the socket was not moved before since it wasn't parented to the worker object and thus was not in the correct thread to work.

  • Yes, I used moveToThread from the main thread. That makes so much sense. Thanks SGaist

  • Hey is it possible to share how you are sending data from matlab to QT .How did you set up both environment? It would be nice if you can share something.

  • @Surizer
    Hi and welcome to this forum

    Just make you aware that the post is rather old. Furthermore from the user's profile you can see that the user might no longer hang out in the forum. His last activity was about 2 years according to his profile.

    Possibly you start a new thread with a link to this thread here. May be someone else knows a solution.

Log in to reply