Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UDP communication
Qt 6.11 is out! See what's new in the release blog

UDP communication

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 3 Posters 4.4k Views
  • 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.
  • JonBJ JonB

    @Vijaykarthikeyan said in UDP communication:

    @JonB I have used two sockets- send_socket,socket

    The socket (member?) variable you declare in Sender::Sender() and use in processResponse() is not bound/connected, it is simply a default empty socket. Why should it receive anything you send? Also you call send_socket->pendingDatagramSize() but socket->readDatagram().

    If it still doesn't work, maybe try getting/connecting the second socket via connectToHost()?

    Also,I couldn't understand the line :

    Your sending code outputs that unconditionally after writeDatagram() without checking the result code,

    how to check this,i didnt get it

    Look at the documentation for writeDatagram(). The reason it returns a result is for people to check it.... Same for readDatagram().

    V Offline
    V Offline
    Vijaykarthikeyan
    wrote on last edited by
    #12

    @JonB On looking through documentation writeDatagram() returns the number of bytes it sent..

    on my case,the number of bytes returned every time are 30:

    30
    Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
    30
    Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
    30

    1 Reply Last reply
    0
    • V Vijaykarthikeyan

      @JonB ok..I have changed the mistakes..still receiving function is not working

      #include "sender.h"
      #include <QtNetwork/QUdpSocket>
      #include <QMessageBox>
      #include <exception>
      #include <QTimer>
      #include <QObject>
      
      
      Sender::Sender()
          {
              send_socket = new QUdpSocket(this);
              socket = new QUdpSocket(this);
              send_socket->bind(QHostAddress("192.168.0.110"),500);
              socket->bind(QHostAddress("192.168.0.110"),4500);
      
              connect(socket, &QUdpSocket::readyRead, this, &Sender::processResponse);
      
              QTimer* timer = new QTimer(this);
              connect(timer, &QTimer::timeout, this, &Sender::sendDatagram);
              timer->start(1000); // Adjust the interval as needed (milliseconds)
      
      
          }
      
          void Sender::sendDatagram()
          {
                  send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
                  qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA;
                  
          }
      
          void Sender::processResponse()
          {
           
                     if (socket->hasPendingDatagrams())
                      {
                          datagram.resize(socket->pendingDatagramSize());
                          QHostAddress senderAddress;
                          quint16 senderPort;
                          socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                          QString data = QString::fromUtf8(datagram);
      
                          qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data;
                      }
                      else
                      {
                          qDebug() << "No pending datagrams.";
                      }
          }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #13

      @Vijaykarthikeyan
      You have chosen not to verify the writeDatagram() (and for that matter the readDatagram(). Nor do you check the bind() results. I also suggested you try with connectToHost(). I leave this to you now, you have your own way of developing.

      V 2 Replies Last reply
      0
      • JonBJ JonB

        @Vijaykarthikeyan
        You have chosen not to verify the writeDatagram() (and for that matter the readDatagram(). Nor do you check the bind() results. I also suggested you try with connectToHost(). I leave this to you now, you have your own way of developing.

        V Offline
        V Offline
        Vijaykarthikeyan
        wrote on last edited by
        #14

        @JonB Where I have chosen? You said to check the return of writeDatagram().. I thought it is debugging the return byte. How could you tell me that i have chosen not to verify? I have gone through all that documentation ,tried with different ip's. Also they have came out well.. I came here to seek help to clear my doubt? how could you say like this? If the suggestion is clear and I didn't go for that..it is agreeable. But, I have tried all the ways you have said and you are accusing me I'm not interested? Is that the right way of concluding the topic

        Christian EhrlicherC 1 Reply Last reply
        0
        • V Vijaykarthikeyan

          @JonB Where I have chosen? You said to check the return of writeDatagram().. I thought it is debugging the return byte. How could you tell me that i have chosen not to verify? I have gone through all that documentation ,tried with different ip's. Also they have came out well.. I came here to seek help to clear my doubt? how could you say like this? If the suggestion is clear and I didn't go for that..it is agreeable. But, I have tried all the ways you have said and you are accusing me I'm not interested? Is that the right way of concluding the topic

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #15

          When you want to write to a socket there is no need for bind() as written in the documentation. There is even a big red note about it. And the documentation also show you what to do to receive an udp packet. And moreover you can find two examples there which show your requested functionality.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          V 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            When you want to write to a socket there is no need for bind() as written in the documentation. There is even a big red note about it. And the documentation also show you what to do to receive an udp packet. And moreover you can find two examples there which show your requested functionality.

            V Offline
            V Offline
            Vijaykarthikeyan
            wrote on last edited by
            #16

            @Christian-Ehrlicher ok ...i modified it..removed that binding line for sender. on looking through documentation,i have checked the return statement for both writeDatagram() and readDatagram().The former one returns 30 bytes but the latter one returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to the connect function which connects readyRead() with receving function. but upon debugging that also,it gives ture..

            where can i found the error?

            Christian EhrlicherC 1 Reply Last reply
            0
            • V Vijaykarthikeyan

              @Christian-Ehrlicher ok ...i modified it..removed that binding line for sender. on looking through documentation,i have checked the return statement for both writeDatagram() and readDatagram().The former one returns 30 bytes but the latter one returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to the connect function which connects readyRead() with receving function. but upon debugging that also,it gives ture..

              where can i found the error?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #17

              @Vijaykarthikeyan said in UDP communication:

              returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to

              This does not match since your code correctly calls readDatagram() when the receive function was called due to readyRead.
              Post yor actual code and a proper error description.

              /edit: also try out the examples and make sure the windows firewall does not block the udp packets

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              V 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Vijaykarthikeyan said in UDP communication:

                returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to

                This does not match since your code correctly calls readDatagram() when the receive function was called due to readyRead.
                Post yor actual code and a proper error description.

                /edit: also try out the examples and make sure the windows firewall does not block the udp packets

                V Offline
                V Offline
                Vijaykarthikeyan
                wrote on last edited by Vijaykarthikeyan
                #18

                @Christian-Ehrlicher i have turned off all the firewall securities,sir..examples in qt documentation explains separate separate modules.Here is the modified code:

                #include "sender.h"
                #include <QtNetwork/QUdpSocket>
                #include <QMessageBox>
                #include <exception>
                #include <QTimer>
                #include <QObject>
                
                
                Sender::Sender()
                    {
                        send_socket = new QUdpSocket(this);
                        socket = new QUdpSocket(this);
                        qDebug()<<socket->bind(QHostAddress::Any,55000);
                        connect(socket, &QUdpSocket::readyRead, this, &Sender::processResponse);
                        QTimer* timer = new QTimer(this);
                        connect(timer, &QTimer::timeout, this, &Sender::sendDatagram);
                        timer->start(1000); // Adjust the interval as needed (milliseconds)
                
                
                    }
                
                    void Sender::sendDatagram()
                    {
                            qDebug()<<send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
                            qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA;
                            //processResponse();
                    }
                
                    void Sender::processResponse()
                    {   if (socket->state() == QUdpSocket::BoundState)
                            {
                                while (socket->hasPendingDatagrams()) {
                
                                    datagram.resize(socket->pendingDatagramSize());
                                    QHostAddress senderAddress;
                                    quint16 senderPort;
                                    int bytesRead = socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                                    QString data = QString::fromUtf8(datagram);
                
                                    qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data;
                                    if (bytesRead == -1)
                                    {
                                        qDebug() << "Failed to read datagram!";
                                    }
                                    else
                                    {
                                        QString data = QString::fromUtf8(datagram);
                                        qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data;
                
                                    }
                                }
                            }
                    }
                
                
                
                Christian EhrlicherC 1 Reply Last reply
                0
                • V Vijaykarthikeyan

                  @Christian-Ehrlicher i have turned off all the firewall securities,sir..examples in qt documentation explains separate separate modules.Here is the modified code:

                  #include "sender.h"
                  #include <QtNetwork/QUdpSocket>
                  #include <QMessageBox>
                  #include <exception>
                  #include <QTimer>
                  #include <QObject>
                  
                  
                  Sender::Sender()
                      {
                          send_socket = new QUdpSocket(this);
                          socket = new QUdpSocket(this);
                          qDebug()<<socket->bind(QHostAddress::Any,55000);
                          connect(socket, &QUdpSocket::readyRead, this, &Sender::processResponse);
                          QTimer* timer = new QTimer(this);
                          connect(timer, &QTimer::timeout, this, &Sender::sendDatagram);
                          timer->start(1000); // Adjust the interval as needed (milliseconds)
                  
                  
                      }
                  
                      void Sender::sendDatagram()
                      {
                              qDebug()<<send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
                              qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA;
                              //processResponse();
                      }
                  
                      void Sender::processResponse()
                      {   if (socket->state() == QUdpSocket::BoundState)
                              {
                                  while (socket->hasPendingDatagrams()) {
                  
                                      datagram.resize(socket->pendingDatagramSize());
                                      QHostAddress senderAddress;
                                      quint16 senderPort;
                                      int bytesRead = socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                                      QString data = QString::fromUtf8(datagram);
                  
                                      qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data;
                                      if (bytesRead == -1)
                                      {
                                          qDebug() << "Failed to read datagram!";
                                      }
                                      else
                                      {
                                          QString data = QString::fromUtf8(datagram);
                                          qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data;
                  
                                      }
                                  }
                              }
                      }
                  
                  
                  
                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #19

                  @Vijaykarthikeyan said in UDP communication:

                  examples in qt documentation explains separate separate modules.

                  I don't see what this should matter. You should test if it works. Merging the two functions into one class (for whatever abstruse reason) is a no brainer afterwards.

                  Why do you use port 55000 and QHostAddress::Any on the one side and receiver_port and receiver_host on the other?

                  Why do you check for a socket state in your slot? No example does this.

                  How does your debug output look like now?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  V 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @Vijaykarthikeyan said in UDP communication:

                    examples in qt documentation explains separate separate modules.

                    I don't see what this should matter. You should test if it works. Merging the two functions into one class (for whatever abstruse reason) is a no brainer afterwards.

                    Why do you use port 55000 and QHostAddress::Any on the one side and receiver_port and receiver_host on the other?

                    Why do you check for a socket state in your slot? No example does this.

                    How does your debug output look like now?

                    V Offline
                    V Offline
                    Vijaykarthikeyan
                    wrote on last edited by
                    #20

                    @Christian-Ehrlicher ok.. now, I corrected my code and finally it works. It's because the mismatch between the Ip address and ports.. I have to use 5 digit port number instead of 4..

                    1 Reply Last reply
                    0
                    • V Vijaykarthikeyan has marked this topic as solved on
                    • JonBJ JonB

                      @Vijaykarthikeyan
                      You have chosen not to verify the writeDatagram() (and for that matter the readDatagram(). Nor do you check the bind() results. I also suggested you try with connectToHost(). I leave this to you now, you have your own way of developing.

                      V Offline
                      V Offline
                      Vijaykarthikeyan
                      wrote on last edited by
                      #21

                      @JonB This is the code which successfully ran but without your suggestions and you are accusing me that i knows nothing..That's funny..you people arguing and accusing me that my codes are wrong this wont work that wont work. IT it don't work,why can't you suggest the loophole to be corrected. but,other than this, you guys directing the topic to somewhere else

                      #include "test.h"
                      #include <QDataStream>
                      #include <QTimer>
                      #include <QtNetwork/QUdpSocket>
                      MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      {
                      socket = new QUdpSocket(this);
                      socket->bind(QHostAddress::LocalHost, receiverPort);
                      receiveDatagram();
                      }

                      void MainWindow::receiveDatagram()
                      {
                      if (socket->state() == QUdpSocket::BoundState)
                      {

                          while (socket->hasPendingDatagrams())
                          {
                              QHostAddress senderAddress(QHostAddress::LocalHost);
                              quint16 senderPort;
                              datagram.resize(socket->pendingDatagramSize());
                              socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                              //emit data_received(datagram);
                              qDebug()<<datagram;
                              sendDatagram();
                          }
                      }
                      

                      }

                      void MainWindow::sendDatagram()

                      {
                      QHostAddress receiverAddress(QHostAddress::LocalHost);

                      socket->writeDatagram(array, receiverAddress, receiverPort);
                      qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<array;
                      

                      }

                      JonBJ 1 Reply Last reply
                      0
                      • V Vijaykarthikeyan

                        @JonB This is the code which successfully ran but without your suggestions and you are accusing me that i knows nothing..That's funny..you people arguing and accusing me that my codes are wrong this wont work that wont work. IT it don't work,why can't you suggest the loophole to be corrected. but,other than this, you guys directing the topic to somewhere else

                        #include "test.h"
                        #include <QDataStream>
                        #include <QTimer>
                        #include <QtNetwork/QUdpSocket>
                        MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        {
                        socket = new QUdpSocket(this);
                        socket->bind(QHostAddress::LocalHost, receiverPort);
                        receiveDatagram();
                        }

                        void MainWindow::receiveDatagram()
                        {
                        if (socket->state() == QUdpSocket::BoundState)
                        {

                            while (socket->hasPendingDatagrams())
                            {
                                QHostAddress senderAddress(QHostAddress::LocalHost);
                                quint16 senderPort;
                                datagram.resize(socket->pendingDatagramSize());
                                socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                                //emit data_received(datagram);
                                qDebug()<<datagram;
                                sendDatagram();
                            }
                        }
                        

                        }

                        void MainWindow::sendDatagram()

                        {
                        QHostAddress receiverAddress(QHostAddress::LocalHost);

                        socket->writeDatagram(array, receiverAddress, receiverPort);
                        qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<array;
                        

                        }

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

                        @Vijaykarthikeyan Please now get help from other people than me. I'm done.

                        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
                        • Unsolved