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. QUdpSocket : simple communication between 2 Qt applications
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket : simple communication between 2 Qt applications

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 4.1k 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
    MrShawn
    wrote on last edited by
    #2

    Just a guess but, you are not reading the data when it comes in, so it is still ready to be read. New data comes in but you haven't done any reading from the old data that was there before so it doesn't re-emit the signal.

    Try doing a readAll() in your lambda and see if that fixes it.

    ODБOïO 1 Reply Last reply
    4
    • M MrShawn

      Just a guess but, you are not reading the data when it comes in, so it is still ready to be read. New data comes in but you haven't done any reading from the old data that was there before so it doesn't re-emit the signal.

      Try doing a readAll() in your lambda and see if that fixes it.

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #3

      @MrShawn
      Thank you for the answer

      int main(int argc, char *argv[])
      {
      
          QApplication a(argc, argv);
      
          QUdpSocket sk; 
          QObject::connect(&sk,&QUdpSocket::readyRead,&sk,[&](){
      //        sk.open(QUdpSocket::ReadOnly);
              qDebug()<< "read : "<< sk.readAll(); // this happends only once
          });
          sk.bind(QHostAddress::Any,65222);
      
          MainWindow w; 
          w.show();
          return a.exec();
      }
      

      i tryed this, but no data is read , this is tha output :

      QIODevice::read (QUdpSocket): device not open
      read :
      

      I tryed to calling sk.open(QUdpSocket::ReadOnly);
      with this no more "device not open" message, but still no data and called only once.

      1 Reply Last reply
      0
      • ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #4

        I also tryed to move the QUDPSocket in my MainWindow widget class.
        i have

        private :
        QUdpSocket *sk;
        

        ctor :

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            sk = new QUdpSocket(this);
             sk->bind(QHostAddress::Any,65222);
             QObject::connect(sk,&QUdpSocket::readyRead,this,&MainWindow::skReady);
        }
        

        and the slot :

            void skReady(){
                sk->open(QUdpSocket::ReadOnly);
                
                    qDebug()<<"read :" << sk->readAll();
            }
        
        JonBJ 1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #5

          You need to read the datagram. Try the following in server

          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              QUdpSocket sk;
              sk.bind(QHostAddress::Any,65222);
          
              QObject::connect(&sk,&QUdpSocket::readyRead,[&sk](){
                  qDebug() << Q_FUNC_INFO << endl;
                  sk.receiveDatagram();
              });
          
          
              return a.exec();
          }
          

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          ODБOïO R 2 Replies Last reply
          3
          • ODБOïO ODБOï

            I also tryed to move the QUDPSocket in my MainWindow widget class.
            i have

            private :
            QUdpSocket *sk;
            

            ctor :

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                sk = new QUdpSocket(this);
                 sk->bind(QHostAddress::Any,65222);
                 QObject::connect(sk,&QUdpSocket::readyRead,this,&MainWindow::skReady);
            }
            

            and the slot :

                void skReady(){
                    sk->open(QUdpSocket::ReadOnly);
                    
                        qDebug()<<"read :" << sk->readAll();
                }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

            @LeLev
            I was just about to suggest as per @dheerendra --- you need to read the datagram properly.
            I think the trouble with trying readAll() etc. might be:

            The most common way to use this class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() / receiveDatagram() to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().

            1 Reply Last reply
            0
            • dheerendraD dheerendra

              You need to read the datagram. Try the following in server

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  QUdpSocket sk;
                  sk.bind(QHostAddress::Any,65222);
              
                  QObject::connect(&sk,&QUdpSocket::readyRead,[&sk](){
                      qDebug() << Q_FUNC_INFO << endl;
                      sk.receiveDatagram();
                  });
              
              
                  return a.exec();
              }
              
              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by ODБOï
              #7

              hi @dheerendra ,
              I don't understand,
              i my server i have nothing to read, i only want to write. The reciver (2nd app) has to read (sk.receiveDatagram() ). am i missing something or you misunderstood my question ..?

              JonBJ 1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #8

                Did you try the way I suggested ? Did that work ?

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                1
                • ODБOïO ODБOï

                  hi @dheerendra ,
                  I don't understand,
                  i my server i have nothing to read, i only want to write. The reciver (2nd app) has to read (sk.receiveDatagram() ). am i missing something or you misunderstood my question ..?

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

                  @LeLev
                  @dheerendra's code is the code to put into your client.
                  OIC, he wrote "server" --- it must have been a slip, he meant "client" :)

                  1 Reply Last reply
                  1
                  • ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by
                    #10

                    That worked thank you very much

                        void skReady(){      
                            QByteArray buffer;
                            buffer.resize(static_cast<int>(sk->pendingDatagramSize()));
                            QHostAddress sender("127.0.0.1");
                            quint16 senderPort=65111;
                            qDebug()<<sk->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);          
                        }
                    
                    1 Reply Last reply
                    1
                    • dheerendraD Offline
                      dheerendraD Offline
                      dheerendra
                      Qt Champions 2022
                      wrote on last edited by dheerendra
                      #11

                      Some confusion with where to place my code. Code has to be placed in App2(receiver) & not in the sender(App1)

                      1. Client is writing data. So what you have in sender is fine.
                      2. Receiver - You should place the code I suggested.

                      Then tell me.

                      Dheerendra
                      @Community Service
                      Certified Qt Specialist
                      http://www.pthinks.com

                      JonBJ 1 Reply Last reply
                      2
                      • dheerendraD dheerendra

                        Some confusion with where to place my code. Code has to be placed in App2(receiver) & not in the sender(App1)

                        1. Client is writing data. So what you have in sender is fine.
                        2. Receiver - You should place the code I suggested.

                        Then tell me.

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

                        @dheerendra
                        I'm only used to TCP. Never thought about terminology for UDP! I would have called the [UDP] server the side which is writing, not client, but maybe you know better than I :)
                        https://stackoverflow.com/questions/9951875/who-is-the-server-and-who-is-the-client-in-udp

                        1 Reply Last reply
                        2
                        • dheerendraD Offline
                          dheerendraD Offline
                          dheerendra
                          Qt Champions 2022
                          wrote on last edited by
                          #13

                          OK. Client & Server is only terminology.

                          1. Server is the one which is waiting for connection.
                          2. Client is the one which is initiating the connection.

                          Once connection is established client/server terminology w.r.t data transfer is blurred as both will start transferring the data.

                          Dheerendra
                          @Community Service
                          Certified Qt Specialist
                          http://www.pthinks.com

                          1 Reply Last reply
                          2
                          • dheerendraD dheerendra

                            You need to read the datagram. Try the following in server

                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                                QUdpSocket sk;
                                sk.bind(QHostAddress::Any,65222);
                            
                                QObject::connect(&sk,&QUdpSocket::readyRead,[&sk](){
                                    qDebug() << Q_FUNC_INFO << endl;
                                    sk.receiveDatagram();
                                });
                            
                            
                                return a.exec();
                            }
                            
                            R Offline
                            R Offline
                            reshu
                            wrote on last edited by
                            #14

                            @dheerendra i am using QT widget application to receive udp packets from another PC. my udp source is a labview VI. i am able to receive the data which i am sending .it is displaying in the debug window but not displaying in the user interface window.i tried textedit and listwidget. but not displaying anything. this is my first QT pgm. plz help.

                            JonBJ 1 Reply Last reply
                            0
                            • R reshu

                              @dheerendra i am using QT widget application to receive udp packets from another PC. my udp source is a labview VI. i am able to receive the data which i am sending .it is displaying in the debug window but not displaying in the user interface window.i tried textedit and listwidget. but not displaying anything. this is my first QT pgm. plz help.

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

                              @reshu
                              Raise a new topic for this (and you will need to show some code beyond what you have asked here before anyone can help you). It has nothing to do with this QUdpSocket issue.

                              R 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @reshu
                                Raise a new topic for this (and you will need to show some code beyond what you have asked here before anyone can help you). It has nothing to do with this QUdpSocket issue.

                                R Offline
                                R Offline
                                reshu
                                wrote on last edited by
                                #16

                                @JonB i am not able to find the link for a new topic

                                JonBJ 1 Reply Last reply
                                0
                                • R reshu

                                  @JonB i am not able to find the link for a new topic

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

                                  @reshu
                                  So when you go to https://forum.qt.io/category/10/general-and-desktop you do not see a big blue New Topic button at the top of the page?

                                  You seemed to be able to raise https://forum.qt.io/topic/113963/udp-data-reception ?

                                  R 1 Reply Last reply
                                  2
                                  • JonBJ JonB

                                    @reshu
                                    So when you go to https://forum.qt.io/category/10/general-and-desktop you do not see a big blue New Topic button at the top of the page?

                                    You seemed to be able to raise https://forum.qt.io/topic/113963/udp-data-reception ?

                                    R Offline
                                    R Offline
                                    reshu
                                    wrote on last edited by
                                    #18

                                    @JonB yes

                                    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