Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UDP reset
Forum Updated to NodeBB v4.3 + New Features

UDP reset

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 4 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ JonB

    @Vijaykarthikeyan
    You have decided that you know how to write the Qt program your way, and don't seem to like the advice you are getting here. Best of luck.

    In this forum,most of the suggestions failed..i found the solutions myself .

    Then you have discovered that the suggestions here are worthless and you would be best sticking to finding the solutions for yourself. Nobody wants to suggest stuff to be told it's not appreciated.

    FWIW, calling main() again won't work right.

    V Offline
    V Offline
    Vijaykarthikeyan
    wrote on last edited by
    #23

    @JonB For sometimes calling main() works successfully.Whenever I raised the topic you guys never take that question and giving feedbacks about someother topics which is irrelevant to the topic. Even in this question, my question is to reset the udp whenevr there is no data is coming.But, you are accusing me about the code. If that was my problem,why shouldn't i raised that topic? because that's not the problem which im seeking to? still you guys ain't directly answering the question. If you know the answer,please tell that afterwards you can accuse me

    JonBJ Christian EhrlicherC 2 Replies Last reply
    0
    • V Vijaykarthikeyan

      @JonB For sometimes calling main() works successfully.Whenever I raised the topic you guys never take that question and giving feedbacks about someother topics which is irrelevant to the topic. Even in this question, my question is to reset the udp whenevr there is no data is coming.But, you are accusing me about the code. If that was my problem,why shouldn't i raised that topic? because that's not the problem which im seeking to? still you guys ain't directly answering the question. If you know the answer,please tell that afterwards you can accuse me

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #24

      @Vijaykarthikeyan
      Nobody has "accused" you of anything, or been impolite. Merely trying to help.

      I have already asked a couple of times what you mean by "reset the UDP", maybe if you answered that it would help. If you want to discard any data already received then do so. If you want to close the socket and re-open it then do so. If you want to destroy the UDP socket object and create a new one afresh then do so. No call to main() a second time.

      V 1 Reply Last reply
      1
      • V Vijaykarthikeyan

        @JonB For sometimes calling main() works successfully.Whenever I raised the topic you guys never take that question and giving feedbacks about someother topics which is irrelevant to the topic. Even in this question, my question is to reset the udp whenevr there is no data is coming.But, you are accusing me about the code. If that was my problem,why shouldn't i raised that topic? because that's not the problem which im seeking to? still you guys ain't directly answering the question. If you know the answer,please tell that afterwards you can accuse me

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #25

        There is no thing like a 'reset' for an udp connection. If you want a timeout use a QTimer as already told you. Everything else was already told - don't block the qt's event loop but use proper signals and slots. Or use another framework which does not need a running event loop (if you did one).

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        V 1 Reply Last reply
        2
        • JonBJ JonB

          @Vijaykarthikeyan
          Nobody has "accused" you of anything, or been impolite. Merely trying to help.

          I have already asked a couple of times what you mean by "reset the UDP", maybe if you answered that it would help. If you want to discard any data already received then do so. If you want to close the socket and re-open it then do so. If you want to destroy the UDP socket object and create a new one afresh then do so. No call to main() a second time.

          V Offline
          V Offline
          Vijaykarthikeyan
          wrote on last edited by
          #26

          @JonB As I already told @jsulm again explaining my issue. resetting the udp means instead of auto calling the event loop by app.exec(), there should be some condition to enter /restart the event loop. no worry about the delays.

          Acutally,this code is a part of receiver,another part is the sender which sends the data asynchronously..we don't get the data ,indeed the correct data all the time. If there is no data or any data get corrupted during transmission, this receiver should reset means it should enter/restarts the main loop again which is an alternative to app.exec(). Delay is not a big problem.So, ignore any event loop delays which are all caused by msleep and all.

          this is the requirement which I'm trying to figure out. For sometimes it works.

          So,instead of the event loop app.exec(), i want to do some condition to run that event loop

          Christian EhrlicherC 1 Reply Last reply
          0
          • V Vijaykarthikeyan

            @JonB As I already told @jsulm again explaining my issue. resetting the udp means instead of auto calling the event loop by app.exec(), there should be some condition to enter /restart the event loop. no worry about the delays.

            Acutally,this code is a part of receiver,another part is the sender which sends the data asynchronously..we don't get the data ,indeed the correct data all the time. If there is no data or any data get corrupted during transmission, this receiver should reset means it should enter/restarts the main loop again which is an alternative to app.exec(). Delay is not a big problem.So, ignore any event loop delays which are all caused by msleep and all.

            this is the requirement which I'm trying to figure out. For sometimes it works.

            So,instead of the event loop app.exec(), i want to do some condition to run that event loop

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #27

            There is no need for restarting the main event loop. It will not automagically re-sync something.
            Also udp is stateless. If you want more you have to create your own protocol on top of udp or even better tcp.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            V 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              There is no thing like a 'reset' for an udp connection. If you want a timeout use a QTimer as already told you. Everything else was already told - don't block the qt's event loop but use proper signals and slots. Or use another framework which does not need a running event loop (if you did one).

              V Offline
              V Offline
              Vijaykarthikeyan
              wrote on last edited by
              #28

              @Christian-Ehrlicher Yes, but my requirement is to restart the main eventloop whenever there is no incoming data from sender side. I also added modified the code and already posted instead of former one where I have used timer and proper signal slot connection. Can I again send it to you,sir

              Christian EhrlicherC 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                There is no need for restarting the main event loop. It will not automagically re-sync something.
                Also udp is stateless. If you want more you have to create your own protocol on top of udp or even better tcp.

                V Offline
                V Offline
                Vijaykarthikeyan
                wrote on last edited by
                #29

                @Christian-Ehrlicher yes..it is because of app.exec() at the end.Am i right? sir

                1 Reply Last reply
                0
                • V Vijaykarthikeyan

                  @Christian-Ehrlicher Yes, but my requirement is to restart the main eventloop whenever there is no incoming data from sender side. I also added modified the code and already posted instead of former one where I have used timer and proper signal slot connection. Can I again send it to you,sir

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #30

                  @Vijaykarthikeyan said in UDP reset:

                  but my requirement is to restart the main eventloop whenever there is no incoming data from sender side

                  This is stupid, wrong and absolutely not needed and has nothing to do with a 'udp reset'.

                  Bye

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  V 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @Vijaykarthikeyan said in UDP reset:

                    but my requirement is to restart the main eventloop whenever there is no incoming data from sender side

                    This is stupid, wrong and absolutely not needed and has nothing to do with a 'udp reset'.

                    Bye

                    V Offline
                    V Offline
                    Vijaykarthikeyan
                    wrote on last edited by
                    #31

                    @Christian-Ehrlicher then,how to restart the udp like watchdog timer

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      Vijaykarthikeyan
                      wrote on last edited by
                      #32

                      @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;
                      }
                      
                      
                      
                      
                      
                      
                      1 Reply Last reply
                      0
                      • V Vijaykarthikeyan has marked this topic as solved on

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved