Receive Data From UdpSocket In Thread
-
when using Readyread() Signal In MAINWINDOW working good my problem when i move it to thread didnt emit data
CODE:udpreceiver.h
#ifndef UDPRECEIVER_H #define UDPRECEIVER_H #include <QObject> #include <QDebug> #include <QThread> #include <QUdpSocket> class UdpReceiver : public QObject { Q_OBJECT public: explicit UdpReceiver(QObject *parent = nullptr); ~UdpReceiver(); signals: void SendDataToGui(QByteArray frame); //to Send Feed Back To THE USER InERFACE public slots: void readPendingDiagrams(); private: QUdpSocket *socket; }; #endif // UDPRECEIVER_H
udpreceiver.cpp
#include "udpreceiver.h" #include <QThread> #include <QDebug> UdpReceiver::UdpReceiver(QObject *parent) : QObject(parent) { //constractor qDebug() << "UDP Construction thread:" << QThread::currentThread(); //intialize socket = new QUdpSocket(this); socket->bind(36000, QUdpSocket::ShareAddress); socket->connect(socket, SIGNAL(readyRead()), this, SLOT(readPendingDiagrams())); } UdpReceiver::~UdpReceiver() { qDebug() << "UDP DE-Construction thread:" << QThread::currentThread(); } void UdpReceiver::readPendingDiagrams() { qDebug() << "UDP READ thread:" << QThread::currentThread(); QByteArray Data; while (socket->hasPendingDatagrams()){ Data.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(Data.data(), Data.size(),&sender, &senderPort); qDebug() << "Message from: " << sender.toString(); qDebug() << "Message port: " << senderPort; qDebug() << "Message: " << Data; qDebug() << sizeof (Data); } emit SendDataToGui(Data); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QThread> #include <QUdpSocket> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_connect_clicked(); void on_subscrip_clicked(); void on_info_clicked(); void ReciveDataFromThread(QByteArray array); private: Ui::MainWindow *ui; QThread *UDPThread; QUdpSocket *Sendsocket ; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "udpreceiver.h" #include <QThread> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { qDebug() << "GUI thread:" << QThread::currentThread(); ui->setupUi(this); //SEND Socket Sendsocket = new QUdpSocket(this); //output data Socket Work as server Sendsocket->bind(36000); //to send on 36000 //Start Recive Socket UDPThread = new QThread(this); UdpReceiver *udpreciver = new UdpReceiver; //now this live in Mainwindow udpreciver->moveToThread(UDPThread); //now leaved in another thread connect(UDPThread,&QThread::started,udpreciver,&UdpReceiver::readPendingDiagrams); UDPThread->start(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_connect_clicked() { QByteArray Data; Data.append("10 04 00 00 04 10 03"); QByteArray n = Data.fromHex(Data); qDebug()<<"n:"<<n ; Sendsocket->writeDatagram(n, QHostAddress("192.168.50.55"),36000); //[0] Define The Lisner IP AND PORT } void MainWindow::on_subscrip_clicked() { } void MainWindow::on_info_clicked() { } void MainWindow::ReciveDataFromThread(QByteArray array) { }
Thanks,
-
Hi
For me - your bind returned false and gave this error
QNativeSocketEngine::hasPendingDatagrams() was called in QAbstractSocket::UnconnectedStateI then changed
UdpReceiver::UdpReceiver(QObject *parent) : QObject(parent)
{
//constractor
qDebug() << "UDP Construction thread:" << QThread::currentThread();//intialize socket = new QUdpSocket(this); qDebug() << "bind:" << socket->bind(QHostAddress::LocalHost,36000, QUdpSocket::ShareAddresst); // localhost addded qDebug() << "conn:" << socket->connect(socket, SIGNAL(readyRead()), this, SLOT(readPendingDiagrams()));
}
and in mainWinow
Sendsocket->writeDatagram(n, QHostAddress::LocalHost,
36000); //[0] Define The Lisner IP AND PORTand then it works
GUI thread: QThread(0x199eb0) UDP Construction thread: QThread(0x199eb0) bind: true conn: true UDP READ thread: QThread(0x7ae870) UDP READ thread: QThread(0x7ae870) Message from: "127.0.0.1" Message port: 36000 Message: "\x10\x04\x00\x00\x04\x10\x03"
even with udpreciver->moveToThread(UDPThread);
-
this is the output :
GUI thread: QThread(0x1e9220) UDP Construction thread: QThread(0x1e9220) bind: false conn: true UDP READ thread: QThread(0x891a00)
here i am send and recive data from another pc on the network after modifying this line the send socket didn't work.
Sendsocket->writeDatagram(n, QHostAddress::LocalHost,36000); //[0] Define The Lisner IP AND PORT
thanks,
-
this is the output :
GUI thread: QThread(0x1e9220) UDP Construction thread: QThread(0x1e9220) bind: false conn: true UDP READ thread: QThread(0x891a00)
here i am send and recive data from another pc on the network after modifying this line the send socket didn't work.
Sendsocket->writeDatagram(n, QHostAddress::LocalHost,36000); //[0] Define The Lisner IP AND PORT
thanks,
@Marco-Flad said in Recive Data From UDpSocket In Thread:
bind: false
Your bind fails again. it seems. you have to find out why.
Also if you test on 2 pc, Im not sure QHostAddress:: LocalHost can be correct for both.
Did you try with your original
Sendsocket->writeDatagram(n, QHostAddress("192.168.50.55"),36000);I assume the 192.168.50.55 is the IP of the other pc ?
-
@Marco-Flad said in Recive Data From UDpSocket In Thread:
bind: false
Your bind fails again. it seems. you have to find out why.
Also if you test on 2 pc, Im not sure QHostAddress:: LocalHost can be correct for both.
Did you try with your original
Sendsocket->writeDatagram(n, QHostAddress("192.168.50.55"),36000);I assume the 192.168.50.55 is the IP of the other pc ?
@mrjj said in Recive Data From UDpSocket In Thread:
of
yes i tried it and worked in send and receive in mainwindow.cpp but not in thread
yes this ip of 2nd pc -
@mrjj said in Recive Data From UDpSocket In Thread:
of
yes i tried it and worked in send and receive in mainwindow.cpp but not in thread
yes this ip of 2nd pcSo on other pc, you run the same program and it reports
false for bind ?It works for me with thread - on the same machine running the app 2 times.
and sending from one to the other.I cannot test between 2 pc currently.
-
So on other pc, you run the same program and it reports
false for bind ?It works for me with thread - on the same machine running the app 2 times.
and sending from one to the other.I cannot test between 2 pc currently.
@mrjj
i used this to get the errorif(!socket->bind(36000, QUdpSocket::ShareAddress)){ qInfo() << socket->errorString(); }
the Debugger output is :
"The bound address is already in use"
this mean this address used by the Send Socket
i change the send socket to be :
Sendsocket->bind(QHostAddress("169.168.50.55"),36000);
the Debugger output is :
GUI thread: QThread(0x1849220) UDP Construction thread: QThread(0x1849220) bind: true conn: true QNativeSocketEngine::bind() was not called in QAbstractSocket::UnconnectedState "Unknown error"
-
@mrjj
i used this to get the errorif(!socket->bind(36000, QUdpSocket::ShareAddress)){ qInfo() << socket->errorString(); }
the Debugger output is :
"The bound address is already in use"
this mean this address used by the Send Socket
i change the send socket to be :
Sendsocket->bind(QHostAddress("169.168.50.55"),36000);
the Debugger output is :
GUI thread: QThread(0x1849220) UDP Construction thread: QThread(0x1849220) bind: true conn: true QNativeSocketEngine::bind() was not called in QAbstractSocket::UnconnectedState "Unknown error"
@Marco-Flad
Hi did you find out why
"The bound address is already in use" ?