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. 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 3.6k 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.
  • V Offline
    V Offline
    Vijaykarthikeyan
    wrote on last edited by Vijaykarthikeyan
    #1

    How to send and receive the data through UDP communication in same .cpp file? I'm using local host for both sender and receiver. I have my code below where all the initialization of variables and declarations made in header file. So, no need to bother about that

    #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);
    
            connect(send_socket, &QUdpSocket::readyRead, this, &Sender::processResponse);
    
            if (send_socket->bind(QHostAddress::LocalHost, 4500)) {
                qDebug() << "Listening for incoming data on port :"<<4500;
            } else {
                qDebug() << "Error binding UDP socket!";
            }
    
            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;
                //processResponse();
        }
    
        void Sender::processResponse()
        {
                    send_socket->bind(QHostAddress("192.168.0.110"),4500);
                    datagram.resize(send_socket->pendingDatagramSize());
                    QHostAddress senderAddress;
                    quint16 senderPort;
                    socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                    qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << datagram;
        }
        }
    
    
    JonBJ 1 Reply Last reply
    0
    • V Vijaykarthikeyan

      How to send and receive the data through UDP communication in same .cpp file? I'm using local host for both sender and receiver. I have my code below where all the initialization of variables and declarations made in header file. So, no need to bother about that

      #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);
      
              connect(send_socket, &QUdpSocket::readyRead, this, &Sender::processResponse);
      
              if (send_socket->bind(QHostAddress::LocalHost, 4500)) {
                  qDebug() << "Listening for incoming data on port :"<<4500;
              } else {
                  qDebug() << "Error binding UDP socket!";
              }
      
              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;
                  //processResponse();
          }
      
          void Sender::processResponse()
          {
                      send_socket->bind(QHostAddress("192.168.0.110"),4500);
                      datagram.resize(send_socket->pendingDatagramSize());
                      QHostAddress senderAddress;
                      quint16 senderPort;
                      socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                      qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << datagram;
          }
          }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Vijaykarthikeyan
      My only comment is that you have already bound send_socket in the constructor. I don't know what you are doing re-binding send_socket during processResponse(), which is called from readyRead() so socket must be already bound correctly?

      V 1 Reply Last reply
      0
      • JonBJ JonB

        @Vijaykarthikeyan
        My only comment is that you have already bound send_socket in the constructor. I don't know what you are doing re-binding send_socket during processResponse(), which is called from readyRead() so socket must be already bound correctly?

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

        @JonB Ok.. I removed that line and executed. but, the output is like this :

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

        JonBJ 1 Reply Last reply
        0
        • V Vijaykarthikeyan

          @JonB Ok.. I removed that line and executed. but, the output is like this :

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

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

          @Vijaykarthikeyan And? What is wrong with that output?

          V 1 Reply Last reply
          0
          • JonBJ JonB

            @Vijaykarthikeyan And? What is wrong with that output?

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

            @JonB It is only sending. I have also included the receiving function in the same cpp file. both having common host and port.does it possible to send and receive in same cpp file

            JonBJ 1 Reply Last reply
            0
            • V Vijaykarthikeyan

              @JonB It is only sending. I have also included the receiving function in the same cpp file. both having common host and port.does it possible to send and receive in same cpp file

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

              @Vijaykarthikeyan
              First thing is to check return result from send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);.

              V 1 Reply Last reply
              0
              • JonBJ JonB

                @Vijaykarthikeyan
                First thing is to check return result from send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);.

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

                @JonB this line ```
                send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
                qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA;

                
                gives the output continously..obviously its correct:
                
                Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                JonBJ 1 Reply Last reply
                0
                • V Vijaykarthikeyan

                  @JonB this line ```
                  send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
                  qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA;

                  
                  gives the output continously..obviously its correct:
                  
                  Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                  Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                  Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                  Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Vijaykarthikeyan said in UDP communication:

                  obviously its correct:

                  How so? Your sending code outputs that unconditionally after writeDatagram() without checking the result code, so it tells us nothing about whether it was sent. In the time taken for you to ask about this and me to type a reply you could have made the change to test it. Up to you.

                  For sending/receiving to same host you might want to read e.g. https://stackoverflow.com/questions/18584679/testing-udp-on-localhost.

                  P.S.
                  I'm not an expert, but maybe you don't receive a datagram on the same socket as you send it on? Maybe you need to connect a second, separate socket to receive?

                  V 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Vijaykarthikeyan said in UDP communication:

                    obviously its correct:

                    How so? Your sending code outputs that unconditionally after writeDatagram() without checking the result code, so it tells us nothing about whether it was sent. In the time taken for you to ask about this and me to type a reply you could have made the change to test it. Up to you.

                    For sending/receiving to same host you might want to read e.g. https://stackoverflow.com/questions/18584679/testing-udp-on-localhost.

                    P.S.
                    I'm not an expert, but maybe you don't receive a datagram on the same socket as you send it on? Maybe you need to connect a second, separate socket to receive?

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

                    @JonB I have used two sockets- send_socket,socket

                    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

                    JonBJ 1 Reply Last reply
                    0
                    • V Vijaykarthikeyan

                      @JonB I have used two sockets- send_socket,socket

                      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

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

                      @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 2 Replies Last reply
                      0
                      • 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
                        #11

                        @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 1 Reply Last reply
                        0
                        • 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

                                          • Login

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