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. Receive Data From UdpSocket In Thread
Forum Updated to NodeBB v4.3 + New Features

Receive Data From UdpSocket In Thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.0k Views 1 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.
  • M Offline
    M Offline
    Marco Flad
    wrote on last edited by aha_1980
    #1

    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,

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      For me - your bind returned false and gave this error
      QNativeSocketEngine::hasPendingDatagrams() was called in QAbstractSocket::UnconnectedState

      I 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 PORT

      and 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);

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Marco Flad
        wrote on last edited by
        #3

        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,

        mrjjM 1 Reply Last reply
        0
        • M Marco Flad

          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,

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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 ?

          M 1 Reply Last reply
          0
          • mrjjM mrjj

            @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 ?

            M Offline
            M Offline
            Marco Flad
            wrote on last edited by
            #5

            @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

            mrjjM 1 Reply Last reply
            0
            • M Marco Flad

              @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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Marco-Flad

              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.

              M 1 Reply Last reply
              0
              • mrjjM mrjj

                @Marco-Flad

                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.

                M Offline
                M Offline
                Marco Flad
                wrote on last edited by Marco Flad
                #7

                @mrjj
                i used this to get the error

                    if(!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"
                
                mrjjM 1 Reply Last reply
                0
                • M Marco Flad

                  @mrjj
                  i used this to get the error

                      if(!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"
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Marco-Flad
                  Hi did you find out why
                  "The bound address is already in use" ?

                  1 Reply Last reply
                  0

                  • Login

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