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 continuous transmission
Qt 6.11 is out! See what's new in the release blog

UDP continuous transmission

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 3.1k Views 2 Watching
  • 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.
  • R Offline
    R Offline
    reshu
    wrote on last edited by
    #1

    Hi
    i need to send a 256 bytes UDP data continuously on clicking a " send data" button. i should be able to update some fields in the datagram on varying a slider. and sholud be able to stop udp transmission on clicking a stop button. i am able to send the data continuously using a while loop but it gets frozen after sometime and i am not able to access any button anymore. please resolve this issue
    Reshu

    JonBJ 1 Reply Last reply
    1
    • FluentCodingF Offline
      FluentCodingF Offline
      FluentCoding
      wrote on last edited by FluentCoding
      #2

      You should implement Multithreading. Also, I don't think that it is a good idea to do it in a while loop. Maybe you should use a timer so that you don't flood your network traffic :/

      The thing is that if you send the UDP data in the main thread then you are blocking this thread where the whole application runs including the GUI process so you aren't able to do anything while the data gets sent continously. Also to mention its generally a bad pattern to run heavy processes in a while loop imo.

      Take a look at QThread for more details. If you don't know the concept of threads, thread pools or concurrency in general, I would suggest you to take a look at this wikipedia article or this or any other which covers this topic.

      If you don't want to use Multithreading, you could also call this while the udp process so that the events still get proceed, but i wholeheartedly advise against it: QEventLoop::processEvents().

      1 Reply Last reply
      2
      • R reshu

        Hi
        i need to send a 256 bytes UDP data continuously on clicking a " send data" button. i should be able to update some fields in the datagram on varying a slider. and sholud be able to stop udp transmission on clicking a stop button. i am able to send the data continuously using a while loop but it gets frozen after sometime and i am not able to access any button anymore. please resolve this issue
        Reshu

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @reshu
        As @FluentCoding has said. Also check your QIODevice::bytesWritten() etc. so you don't pile up too many outgoings.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          reshu
          wrote on last edited by
          #4

          thanks .let me try these options

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            I don't think you need a QThread, 256 bytes should be sent so fast the GUI wouldn't even notice, just set a QTimer at 0 secs and connect it to a slot that sends the data

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • FluentCodingF Offline
              FluentCodingF Offline
              FluentCoding
              wrote on last edited by
              #6

              @VRonin He send's it very often so I think that it would be a reasonable option to use a QThread but I'm sure that your option is good too, if not even better :D

              1 Reply Last reply
              0
              • R Offline
                R Offline
                reshu
                wrote on last edited by
                #7

                Hi
                sorry for the delay. i tried using thread. still the data is getting transmitted continuously and i am not able to stop the thread.please help```

                Dialog.h

                #define DIALOG_H
                
                #include <QDialog>
                #include "mythread.h"
                #include <QThread>
                #include <QUdpSocket>
                
                QT_BEGIN_NAMESPACE
                namespace Ui { class Dialog; }
                QT_END_NAMESPACE
                
                class Dialog : public QDialog
                {
                    Q_OBJECT
                
                public:
                    explicit Dialog(QWidget *parent = 0);
                    ~Dialog();
                    MyThread *mThread;
                private:
                    Ui::Dialog *ui;
                    QUdpSocket *udpsocket;
                
                public slots:
                
                private slots:
                    void on_startButton_clicked();
                    void on_stopButton_clicked();
                    void update();
                };
                #endif // DIALOG_H
                
                
                
                **dialog.cpp**
                #include "dialog.h"
                #include "ui_dialog.h"
                #include "mythread.h"
                #include <iostream>
                #include <QUdpSocket>
                #include <QThread>
                #include <QTimer>
                
                
                
                Dialog::Dialog(QWidget *parent)
                    : QDialog(parent)
                    , ui(new Ui::Dialog)
                {
                    ui->setupUi(this);
                
                    mThread = new MyThread(this);
                    connect(mThread,SIGNAL(started()),this, SLOT(update()),Qt::QueuedConnection);
                
                }
                
                Dialog::~Dialog()
                {
                    delete ui;
                }
                
                
                void Dialog::on_startButton_clicked()
                {
                    mThread->start();
                
                }
                void Dialog::update()
                
                {
                std::cout<<"signal called"<<std::endl;
                udpsocket = new QUdpSocket(0);
                udpsocket->moveToThread(this-> thread());
                for(int i=0;i<1000;i++){
                    QByteArray Data;
                     Data.resize(255);
                    for (int j=0;j<255;j++)
                     {
                        Data[j]=0;
                     }
                    udpsocket->writeDatagram(Data,QHostAddress("192.168.10.12"),55200);
                    qDebug() << "transmitted msg:"<< Data.toHex(' ').split(' ');
                    QThread::msleep(1000);
                }
                
                }
                void Dialog::on_stopButton_clicked()
                {
                    mThread->Stop=true;
                  //  mThread->wait();
                 //  mThread->terminate();
                
                }
                
                **Main.cpp**
                #include <iostream>
                #include "dialog.h"
                
                #include <QApplication>
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    Dialog w;
                    w.show();
                    std::cout<<"am in main"<<std::endl;
                    return a.exec();
                }
                
                **mythread.h**#ifndef MYTHREAD_H
                #define MYTHREAD_H
                
                #include <QThread>
                
                class MyThread : public QThread
                {
                    Q_OBJECT
                public:
                   explicit MyThread(QObject *parent=0 , bool b=false);
                
                    bool Stop;
                signals:
                    void ValueChanged(int);
                public slots:
                private:
                
                };
                
                #endif // MYTHREAD_H
                
                **mythread.cpp**
                #include "mythread.h"
                #include<QtCore>
                #include <QUdpSocket>
                #include <QDebug>
                #include <iostream>
                
                MyThread::MyThread(QObject *parent, bool b):
                  QThread(parent),Stop(b)
                {
                std::cout<<"am in thread"<<std::endl;
                }
                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  reshu
                  wrote on last edited by
                  #8

                  ![alt text](udpth4.png image url)
                  this is my output.i should be able to stop transmission and resume again on clicking start button.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    If you really insist on using threads, please take the time to read the QThread documentation. Your code does not respect the basic threading design and you are still blocking since you are using a blocking queued connection.

                    You really should follow @VRonin's advice and use a QTimer.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3

                    • Login

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