UDP reset
-
I have two exe files one is sender and another one is receiver. It is communicating as well. The data is sending and receiving correctly.
The question is, if the data is not detected for 2 seconds, the udp should reset and start again. How to do that?#include <QCoreApplication> #include <QTextStream> #include <QtNetwork/QUdpSocket> #include <QObject> #include <QThread> QUdpSocket *udpSocket; QTextStream output(stdout); QByteArray datagram,datagram2="10.22,27.6,30.4,0,0,12.34456678"; QHostAddress senderAddress2; quint16 senderPort2; void process_response() { if (udpSocket->state() == QUdpSocket::BoundState) { while (udpSocket->hasPendingDatagrams()) { datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = udpSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } udpSocket->reset(); senderAddress2 = senderAddress; senderPort2=senderPort; } } } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); output << "UDP Command Prompt is ready to receive data!" << "\n"; udpSocket = new QUdpSocket(); udpSocket->bind(QHostAddress::Any, 55000); bool state = QObject::connect(udpSocket, &QUdpSocket::readyRead, &process_response); //bool exitRequested = false; while (state) { process_response(); QThread::msleep(100); } return 0; }
-
@Christian-Ehrlicher @JonB @jsulm Now,it is working..
#include <QCoreApplication> #include <QTextStream> #include <QtNetwork/QUdpSocket> #include <QObject> #include <QThread> #include <QtSerialPort> #include <QtSerialPort/qserialportinfo.h> #include <QSerialPortInfo> QUdpSocket *udpSocket; QTextStream output(stdout); qreal x ; qreal y ; qreal z ; QString x2,y2,z2; QByteArray datagram,datagram2; QHostAddress senderAddress2; quint16 senderPort2; QTimer *timer; bool state; static bool state2=true; void reset_again(); void send_reply() { datagram2.clear(); x = static_cast<qreal>(rand()) / RAND_MAX; y = static_cast<qreal>(rand()) / RAND_MAX; z = static_cast<qreal>(rand()) / RAND_MAX; x2 = QString::number(x); y2 = QString::number(y); z2 = QString::number(z); datagram2.append(x2); datagram2.append(","); datagram2.append(y2); datagram2.append(","); datagram2.append(z2); datagram2.append(","); int bytes=udpSocket->writeDatagram(datagram2, senderAddress2, senderPort2); if(bytes) { qDebug()<<"Data is successfully sent to" << senderAddress2.toString() << ":" << senderPort2 << "-" <<datagram2; } } void process_response() { if (udpSocket->state() == QUdpSocket::BoundState) { while (udpSocket->hasPendingDatagrams()) { datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = udpSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } senderAddress2 = senderAddress; senderPort2=senderPort; send_reply(); } } } void event_loop(); void reset_again() { qDebug()<<"No data is detected"; udpSocket->close(); event_loop(); } void event_loop() { output << "UDP Command Prompt is ready to receive data!" << "\n"; udpSocket = new QUdpSocket(); timer = new QTimer(); udpSocket->bind(QHostAddress::Any, 10050); state=QObject::connect(udpSocket, &QUdpSocket::readyRead,udpSocket, &process_response); while(state) { process_response(); if(udpSocket->waitForReadyRead(500)); else { state=false; reset_again(); } } } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); event_loop(); return 0; }
-
I have two exe files one is sender and another one is receiver. It is communicating as well. The data is sending and receiving correctly.
The question is, if the data is not detected for 2 seconds, the udp should reset and start again. How to do that?#include <QCoreApplication> #include <QTextStream> #include <QtNetwork/QUdpSocket> #include <QObject> #include <QThread> QUdpSocket *udpSocket; QTextStream output(stdout); QByteArray datagram,datagram2="10.22,27.6,30.4,0,0,12.34456678"; QHostAddress senderAddress2; quint16 senderPort2; void process_response() { if (udpSocket->state() == QUdpSocket::BoundState) { while (udpSocket->hasPendingDatagrams()) { datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = udpSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } udpSocket->reset(); senderAddress2 = senderAddress; senderPort2=senderPort; } } } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); output << "UDP Command Prompt is ready to receive data!" << "\n"; udpSocket = new QUdpSocket(); udpSocket->bind(QHostAddress::Any, 55000); bool state = QObject::connect(udpSocket, &QUdpSocket::readyRead, &process_response); //bool exitRequested = false; while (state) { process_response(); QThread::msleep(100); } return 0; }
@Vijaykarthikeyan Use QTimer to check periodically whther you got any new data or not...
-
I have two exe files one is sender and another one is receiver. It is communicating as well. The data is sending and receiving correctly.
The question is, if the data is not detected for 2 seconds, the udp should reset and start again. How to do that?#include <QCoreApplication> #include <QTextStream> #include <QtNetwork/QUdpSocket> #include <QObject> #include <QThread> QUdpSocket *udpSocket; QTextStream output(stdout); QByteArray datagram,datagram2="10.22,27.6,30.4,0,0,12.34456678"; QHostAddress senderAddress2; quint16 senderPort2; void process_response() { if (udpSocket->state() == QUdpSocket::BoundState) { while (udpSocket->hasPendingDatagrams()) { datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = udpSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } udpSocket->reset(); senderAddress2 = senderAddress; senderPort2=senderPort; } } } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); output << "UDP Command Prompt is ready to receive data!" << "\n"; udpSocket = new QUdpSocket(); udpSocket->bind(QHostAddress::Any, 55000); bool state = QObject::connect(udpSocket, &QUdpSocket::readyRead, &process_response); //bool exitRequested = false; while (state) { process_response(); QThread::msleep(100); } return 0; }
-
@Vijaykarthikeyan
And while you are doing as @jsulm says I trust you will get rid ofwhile (state) { process_response(); QThread::msleep(100); }
@JonB yes,have to use if else block instead of while..if block contains the same and the else block should have the timer to reset the udp.. udp have the function reset()
-
@JonB yes,have to use if else block instead of while..if block contains the same and the else block should have the timer to reset the udp.. udp have the function reset()
@Vijaykarthikeyan
I don't know what you mean. You have aconnect()
to use signal and slot, but you don't allow the Qt event loop to run. You instead have a blockingwhile
loop (which never exits). AndQTimer
s won't work with this code.Nor do I know what "UDP reset" means....
-
@Vijaykarthikeyan
I don't know what you mean. You have aconnect()
to use signal and slot, but you don't allow the Qt event loop to run. You instead have a blockingwhile
loop (which never exits). AndQTimer
s won't work with this code.Nor do I know what "UDP reset" means....
@JonB as @jsulm said to user timer, but, i don't know how to connect timer with udp->reset()..it hits error
bool state = QObject::connect(udpSocket, &QUdpSocket::readyRead, &process_response); //bool exitRequested = false; if (state) { process_response(); QThread::msleep(100); } else { qDebug()<<"No Data detected"; qDebug()<<"Reset called"; QObject::connect(timer,&QTimer::timeout,udpSocket->reset()); timer->start(2000); }
-
@JonB as @jsulm said to user timer, but, i don't know how to connect timer with udp->reset()..it hits error
bool state = QObject::connect(udpSocket, &QUdpSocket::readyRead, &process_response); //bool exitRequested = false; if (state) { process_response(); QThread::msleep(100); } else { qDebug()<<"No Data detected"; qDebug()<<"Reset called"; QObject::connect(timer,&QTimer::timeout,udpSocket->reset()); timer->start(2000); }
@Vijaykarthikeyan
Maybe take some time to read Qt examples and documentation.QObject::connect(timer, &QTimer::timeout, udpSocket, &QUdpSocket::reset);
I do not know what calling
QIODevice::reset()
on aQUdpSocket
will do, if anything.As I previously said, I do not know what your
process_response(); QThread::msleep(100);
is doing here, or why you would want this. You have a slot on
readyRead()
, so why this extra code?Your
else
to thestate
return fromconnect()
has nothing to do with "No Data detected". However, sincestate
will always be true it will never be hit. Which is just as well, as what it does will reset (if that does anything at all) every 2 seconds. Which makes no sense.As also stated earlier, your
QTimer
will have no effect if yourmain()
code as shown does not start the Qt event loop. -
@JonB The reason why i have used msleep is that without msleep the receiving data is not in sync with sender side..the process_response() is another sort of code to send back the acknowledgement. Without msleep,it is sedning acknowledgement more than needed,,So, msleep is needed for synchronization. process_response won't have any effect to this current question.
-
@JonB The reason why i have used msleep is that without msleep the receiving data is not in sync with sender side..the process_response() is another sort of code to send back the acknowledgement. Without msleep,it is sedning acknowledgement more than needed,,So, msleep is needed for synchronization. process_response won't have any effect to this current question.
@Vijaykarthikeyan Qt is an asynchronous framework and you should use it as such instead of using endless loops and msleep. Connect a slot to readyRead signal and read the data there (as @JonB pointed out).
"So, msleep is needed for synchronization" - no, it's not. Use Qt like it is supposed to be used... -
@JonB The reason why i have used msleep is that without msleep the receiving data is not in sync with sender side..the process_response() is another sort of code to send back the acknowledgement. Without msleep,it is sedning acknowledgement more than needed,,So, msleep is needed for synchronization. process_response won't have any effect to this current question.
@Vijaykarthikeyan
What @jsulm and I are trying to explain is that to program properly with Qt you need to change your mindset.In the "traditional", synchronous approach you are presently using code does things like "wait" for data to arrive and "sleep" to cause delays. This "blocks" execution while the waits, sleeps etc. are going on.
Qt has an "event-driven", asynchronous paradigm. Code should not "wait" or "sleep", it should be "non-blocking". You don't wait for data to arrive: Qt emits
readyRead()
signal when data does arrive and you act on that with whatever data has arrived so far. If it's not enough, you keep what you have so far and the nextreadyRead()
tells you when more has arrived. If you need to "sleep", you set off aQTimer
. That emits atimeout()
signal when it expires, and you act on it at that point.It takes a little time to get used to this way round of programming if you are accustomed to the synchronous way. But after a bit it becomes quite natural. If you want to understand and take advantage of Qt programming you need to adjust to this approach.
-
@Vijaykarthikeyan Qt is an asynchronous framework and you should use it as such instead of using endless loops and msleep. Connect a slot to readyRead signal and read the data there (as @JonB pointed out).
"So, msleep is needed for synchronization" - no, it's not. Use Qt like it is supposed to be used...@jsulm Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
Received datagram from "::ffff:192.168.0.110" : 55000 - "10.22,27.6,30.4,0,0,12.34456678"
"0,S,M,A,0,0,S,S,4000,10000,#"
"0,S,M,A,0,0,S,S,4000,10000,#"see,on other side,the data is printing double sometimes when we don't use msleep as per your suggestion. It is not correct when receving 2 sides. that's why i used msleep
-
@Vijaykarthikeyan
What @jsulm and I are trying to explain is that to program properly with Qt you need to change your mindset.In the "traditional", synchronous approach you are presently using code does things like "wait" for data to arrive and "sleep" to cause delays. This "blocks" execution while the waits, sleeps etc. are going on.
Qt has an "event-driven", asynchronous paradigm. Code should not "wait" or "sleep", it should be "non-blocking". You don't wait for data to arrive: Qt emits
readyRead()
signal when data does arrive and you act on that with whatever data has arrived so far. If it's not enough, you keep what you have so far and the nextreadyRead()
tells you when more has arrived. If you need to "sleep", you set off aQTimer
. That emits atimeout()
signal when it expires, and you act on it at that point.It takes a little time to get used to this way round of programming if you are accustomed to the synchronous way. But after a bit it becomes quite natural. If you want to understand and take advantage of Qt programming you need to adjust to this approach.
@JonB ok..I'll correct it,but I can't get it what you have suggested for reset the program if data is not received. readyRead() is non retunable function,what else can we do with that
-
@JonB ok..I'll correct it,but I can't get it what you have suggested for reset the program if data is not received. readyRead() is non retunable function,what else can we do with that
@Vijaykarthikeyan said in UDP reset:
reset the program if data is not received
Sorry, but this just doesn't mean very much. Don't know what you're trying to do why. You can either call
reset()
or simply discard some already-received data if that is what you want to do. You can tell if some particular time has passed since data last received via aQTimer
if you need that.readyRead() is non retunable function,what else can we do with that
Again, don't know quite how to answer.
readyRead()
is a signal. No, it does not "return" a result, that's not how signals work. Youconnect()
a slot function to the signal and that gets called each time new data is received. As said several times, in the slot you accumulate data received and act on it when you have enough bytes for whatever you want. -
@JonB ok..I'll correct it,but I can't get it what you have suggested for reset the program if data is not received. readyRead() is non retunable function,what else can we do with that
@Vijaykarthikeyan said in UDP reset:
readyRead() is non retunable function,what else can we do with that
Please read and learn https://doc.qt.io/qt-6/signalsandslots.html , else it makes no sence to use Qt...
-
@Vijaykarthikeyan said in UDP reset:
readyRead() is non retunable function,what else can we do with that
Please read and learn https://doc.qt.io/qt-6/signalsandslots.html , else it makes no sence to use Qt...
Please read and learn https://doc.qt.io/qt-6/qiodevice.html#readyRead , else it makes no sense to use Qt...
its a udpsocket readyRead(),please go through it
-
Please read and learn https://doc.qt.io/qt-6/qiodevice.html#readyRead , else it makes no sense to use Qt...
its a udpsocket readyRead(),please go through it
@Vijaykarthikeyan Funny reply.
I know what readyRead is, but you apparently do not know what a signal is and how to use it. That's why I gave you the link. But it is up to you to learn and understand Qt basics or not, I'm out..."its a udpsocket readyRead()" - QUdpSocket is a subclass of QIODevice...
-
@Vijaykarthikeyan said in UDP reset:
reset the program if data is not received
Sorry, but this just doesn't mean very much. Don't know what you're trying to do why. You can either call
reset()
or simply discard some already-received data if that is what you want to do. You can tell if some particular time has passed since data last received via aQTimer
if you need that.readyRead() is non retunable function,what else can we do with that
Again, don't know quite how to answer.
readyRead()
is a signal. No, it does not "return" a result, that's not how signals work. Youconnect()
a slot function to the signal and that gets called each time new data is received. As said several times, in the slot you accumulate data received and act on it when you have enough bytes for whatever you want.@JonB no..u misunderstood my question..its a udpSocket->readyRead();
Here is my modified code:
#include <QCoreApplication> #include <QTextStream> #include <QtNetwork/QUdpSocket> #include <QObject> #include <QThread> #include <QtSerialPort> #include <QtSerialPort/qserialportinfo.h> #include <QSerialPortInfo> QUdpSocket *udpSocket; QTextStream output(stdout); QByteArray datagram,datagram2="10.22,27.6,30.4,0,0,12.34456678"; QHostAddress senderAddress2; quint16 senderPort2; QSerialPort *serial; QString portname; quint16 vendorId; quint16 productId; QTimer *timer; void reset_again(); void send_reply() { int bytes=udpSocket->writeDatagram(datagram2, senderAddress2, senderPort2); if(bytes) { qDebug()<<"Data is successfully sent to" << senderAddress2.toString() << ":" << senderPort2 << "-" <<datagram2; } } void process_response() { if (udpSocket->state() == QUdpSocket::BoundState) { while (udpSocket->hasPendingDatagrams()) { datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = udpSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } //udpSocket->reset(); senderAddress2 = senderAddress; senderPort2=senderPort; send_reply(); QThread::msleep(100); } } } int main() { output << "UDP Command Prompt is ready to receive data!" << "\n"; udpSocket = new QUdpSocket(); timer = new QTimer(); serial=new QSerialPort(); udpSocket->bind(QHostAddress::Any, 55000); if(QObject::connect(udpSocket, &QUdpSocket::readyRead,udpSocket, &process_response)) { //process_response(); QThread::msleep(100); } else { QObject::connect(timer,&QTimer::timeout,&reset_again); timer->setInterval(2000); } return 0; } void reset_again() { main(); }
-
@Vijaykarthikeyan Funny reply.
I know what readyRead is, but you apparently do not know what a signal is and how to use it. That's why I gave you the link. But it is up to you to learn and understand Qt basics or not, I'm out..."its a udpsocket readyRead()" - QUdpSocket is a subclass of QIODevice...
@jsulm And I know what it is and how it works.. but you don't even know the intention. If i didn't learned the basics, i couldn't came to this way. In this forum,most of the suggestions failed..i found the solutions myself . First, learn Qt basics and how to treat the help seekers..
This code never exist in any of the examples.. I have built a user interface which does not exist in any example.
-
@Vijaykarthikeyan Funny reply.
I know what readyRead is, but you apparently do not know what a signal is and how to use it. That's why I gave you the link. But it is up to you to learn and understand Qt basics or not, I'm out..."its a udpsocket readyRead()" - QUdpSocket is a subclass of QIODevice...
@jsulm Have you answered for my debug output question where i have mentioned it is printing 2 times.. firstly..dont run away with that question
-
@JonB no..u misunderstood my question..its a udpSocket->readyRead();
Here is my modified code:
#include <QCoreApplication> #include <QTextStream> #include <QtNetwork/QUdpSocket> #include <QObject> #include <QThread> #include <QtSerialPort> #include <QtSerialPort/qserialportinfo.h> #include <QSerialPortInfo> QUdpSocket *udpSocket; QTextStream output(stdout); QByteArray datagram,datagram2="10.22,27.6,30.4,0,0,12.34456678"; QHostAddress senderAddress2; quint16 senderPort2; QSerialPort *serial; QString portname; quint16 vendorId; quint16 productId; QTimer *timer; void reset_again(); void send_reply() { int bytes=udpSocket->writeDatagram(datagram2, senderAddress2, senderPort2); if(bytes) { qDebug()<<"Data is successfully sent to" << senderAddress2.toString() << ":" << senderPort2 << "-" <<datagram2; } } void process_response() { if (udpSocket->state() == QUdpSocket::BoundState) { while (udpSocket->hasPendingDatagrams()) { datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = udpSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } //udpSocket->reset(); senderAddress2 = senderAddress; senderPort2=senderPort; send_reply(); QThread::msleep(100); } } } int main() { output << "UDP Command Prompt is ready to receive data!" << "\n"; udpSocket = new QUdpSocket(); timer = new QTimer(); serial=new QSerialPort(); udpSocket->bind(QHostAddress::Any, 55000); if(QObject::connect(udpSocket, &QUdpSocket::readyRead,udpSocket, &process_response)) { //process_response(); QThread::msleep(100); } else { QObject::connect(timer,&QTimer::timeout,&reset_again); timer->setInterval(2000); } return 0; } void reset_again() { main(); }
@Vijaykarthikeyan said in UDP reset:
void reset_again()
{
main();
}Why are you calling main() here?! Never saw such thing. Don't do that!
In main you connect a slot to readyRead. That means: after each reset you will have one more connection and your slot will be called more than once. You also instantiate udp socket, timer and serial port there every time you call main(), so you also have a memory leak.
Also, remove the QThread::msleep(100); as already explained and start Qt event loop in main instead (like in almost every Qt application)! The code you posted is really strange, sorry for writing this, but you should really fix it.What do you actually want to "reset" and why?