0
votes

I have this program that does measurements over time by communicating with a device that is connected by cable directly to my computers network adapter. The communication is using UDP.

The problem here is that after N seconds the program fails to pick up a packet it is expecting (A request packet is sent to the device, and then it waits for the reply). I have a variable that decides how long to wait between each measurement, and by changing this I don't change N, so the number of packets sent is not relevant. If I restart the application and run a new sequence (as in a long string of measurements with delays in between), it simply stops after N seconds again.

Observed values for N: ~200s, ~320s. (It changes sometimes (possibly when rebooting))

I capture packets using wireshark, and according to that the packet is actually received by the network adapter, so it's not the device that is failing here. My application is using Qt and is written in C++.

I have my own UDP class that is using QUdpSocket which extends QAbstractSocket which in turn extends QIODevice, and I see that the readyRead signal is declared in the QIODevice class, this is the signal that I expect but fail to receive.

So what went wrong? Why is Qt not reacting? And why is it after a set amount of time? Please let me know if more specific details/code is needed.

Part of the constructor for my udp class

socket = new QUdpSocket();
socket->connectToHost(QHostAddress(*ipaddr), COM_LUX_PORT);    
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));

The readyRead slot in my udp class

void Udp::readyRead(){
    // when data comes in
    QByteArray buffer;
    buffer.resize(socket->pendingDatagramSize());

    QHostAddress sender;
    quint16 senderPort;

    socket->readDatagram(buffer.data(), buffer.size(),
                         &sender, &senderPort);

    qDebug() << "1Message: " << buffer.toHex();
    emit messageReceived(buffer);
}

Signals and slots in the header

signals:
    /*!
      Signal for transferring the received messages
    */
    void messageReceived(QByteArray buffer);
public slots:
    void readyRead();
    void readyReadSock2();
    void reConnectToIp(QString addr);

private slots:
    void slotTimeout();

EDIT#1: I found this similar question, but there is a major difference, because I receive packets just fine until a certain point.

EDIT#2: Some relevant code.

1
Yes code is needed. There is UDP software that run for years, what you observe should not be happening.Benjamin T
There, I added some code, but the "clogging" I experience happens before this point in the code when the problem arises.Jontahan
Just an idea. Did you try to use bind() instead of connectToHost()? Also you might want to check the error() signal.Benjamin T
Will do, thanks! I'll update as soon as I gather more information, might take some minutes due to the delayed nature of the problem, though.Jontahan
@Jontahan Yes, that is the source of the problem. No, I think you should remove the blocking altogether and use the API asynchronouslyIlBeldus

1 Answers

0
votes

Ilbeldus provided the solution.

The fact that I was frequently blocking the thread seems to be the cause. I still don't get why it only fails after a couple of minutes and why it fails after the same amount of time every time (so explanations are welcome). The problem is however solved. Thanks for your help, all!