Unsolved UDP continuous transmission
-
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 -
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().
-
@reshu
As @FluentCoding has said. Also check yourQIODevice::bytesWritten()
etc. so you don't pile up too many outgoings. -
thanks .let me try these options
-
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 -
@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
-
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; }
-
![alt text]( image url)
this is my output.i should be able to stop transmission and resume again on clicking start button. -
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.