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. Can not send udp packet in QT

Can not send udp packet in QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.9k 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.
  • J Offline
    J Offline
    Jeffson
    wrote on last edited by VRonin
    #1

    Hi,all

    I want to use QUdpSocket to send udp packet to get config parameter from specific server,but It failed and I can not capture the sending packet using wireshark.here is my code:

    CGetConfig.cpp:

    CGetConfig::CGetConfig(const QString &conf_server,const uint16_t port)
    :m_conf_server(conf_server)
    ,m_port(port)
    {
        m_socket = NULL;
    }
    
    CGetConfig::~CGetConfig()
    {}
    
    void CGetConfig::init()
    {
    
        // create a QUDP socket
        m_socket = new QUdpSocket(this);
        m_socket->bind(QHostAddress::LocalHost, 12345);
    
        connect(m_socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    
        m_ip_addr = get_ip_address(m_conf_server);
    }
    
    bool CGetConfig::get_reflector(const QString &mac)
    {
      qDebug() << "CGetConfig::get_reflector():Entry\n";
      if(m_ip_addr.isEmpty())
      {
        qDebug() << "CGetConfig::get_reflector():ip address of cofnig server could not be resolved\n";
        return 0;
      }
    
      QString msg("id=1&mac=");
      msg+= mac;
      msg+= "&get_config=fw_type=v.1,cfg_ver=4,set_ver=0,ip=192.168.1.101";
      qDebug() << m_ip_addr;
      qDebug() << m_port;
      qDebug() << msg.toLatin1();
      int count = 0;
      while(count < 3)
      {
         int t = m_socket->writeDatagram(msg.toLatin1(), QHostAddress(m_ip_addr), m_port);
    
         count++;
     }
    }
    

    Main.cpp

    CGetConfig cfg(cfg_server,cfg_port);
    cfg.init();
    local_mac = "00d033120001";
    cfg.get_reflector(local_mac);
    

    Can anyone help me figure out the problem?

    jsulmJ 1 Reply Last reply
    0
    • J Jeffson

      Hi,all

      I want to use QUdpSocket to send udp packet to get config parameter from specific server,but It failed and I can not capture the sending packet using wireshark.here is my code:

      CGetConfig.cpp:

      CGetConfig::CGetConfig(const QString &conf_server,const uint16_t port)
      :m_conf_server(conf_server)
      ,m_port(port)
      {
          m_socket = NULL;
      }
      
      CGetConfig::~CGetConfig()
      {}
      
      void CGetConfig::init()
      {
      
          // create a QUDP socket
          m_socket = new QUdpSocket(this);
          m_socket->bind(QHostAddress::LocalHost, 12345);
      
          connect(m_socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
      
          m_ip_addr = get_ip_address(m_conf_server);
      }
      
      bool CGetConfig::get_reflector(const QString &mac)
      {
        qDebug() << "CGetConfig::get_reflector():Entry\n";
        if(m_ip_addr.isEmpty())
        {
          qDebug() << "CGetConfig::get_reflector():ip address of cofnig server could not be resolved\n";
          return 0;
        }
      
        QString msg("id=1&mac=");
        msg+= mac;
        msg+= "&get_config=fw_type=v.1,cfg_ver=4,set_ver=0,ip=192.168.1.101";
        qDebug() << m_ip_addr;
        qDebug() << m_port;
        qDebug() << msg.toLatin1();
        int count = 0;
        while(count < 3)
        {
           int t = m_socket->writeDatagram(msg.toLatin1(), QHostAddress(m_ip_addr), m_port);
      
           count++;
       }
      }
      

      Main.cpp

      CGetConfig cfg(cfg_server,cfg_port);
      cfg.init();
      local_mac = "00d033120001";
      cfg.get_reflector(local_mac);
      

      Can anyone help me figure out the problem?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Jeffson Does your main function contain QApplication and do you start an event loop calling exec() on it?
      You should check the return value of writeDatagram() to see whether it could send the data.
      Did bind() succeed (check its return value)?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • J Offline
        J Offline
        Jeffson
        wrote on last edited by VRonin
        #3

        @jsulm ,thank you for your reply.

        I checked the all the return value,all seems ok(writeDatagram and bind).
        bleow is my main function.

        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv)
            QString local_mac;
            uint16_t local_port=0;
            QString ref_ip=0;
            uint16_t ref_port=0;
            QString cfg_server("us-reflector.streamguys.com"); //default one
            uint16_t cfg_port=12345; //default one
            QString if_name;
            printf("CGetConfig::get_reflector():Entry\n");
            CGetConfig cfg(cfg_server,cfg_port);
            cfg.init();
            local_mac = "00d033120001";
            cfg.get_reflector(local_mac);
        
            exit(-1);
            return a.exec();
        }
        

        it is really wired.

        jsulmJ 1 Reply Last reply
        0
        • J Jeffson

          @jsulm ,thank you for your reply.

          I checked the all the return value,all seems ok(writeDatagram and bind).
          bleow is my main function.

          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv)
              QString local_mac;
              uint16_t local_port=0;
              QString ref_ip=0;
              uint16_t ref_port=0;
              QString cfg_server("us-reflector.streamguys.com"); //default one
              uint16_t cfg_port=12345; //default one
              QString if_name;
              printf("CGetConfig::get_reflector():Entry\n");
              CGetConfig cfg(cfg_server,cfg_port);
              cfg.init();
              local_mac = "00d033120001";
              cfg.get_reflector(local_mac);
          
              exit(-1);
              return a.exec();
          }
          

          it is really wired.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Jeffson said in Can not send udp packet in QT:

          exit(-1);
          return a.exec();

          Why do you call exit before starting the event loop? This just terminates your app.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • J Offline
            J Offline
            Jeffson
            wrote on last edited by
            #5

            @jsulm ,I want to quit the app after I send the packet,even though I delete that line,the result is the same.I can not capture the sending packet.

            jsulmJ 1 Reply Last reply
            0
            • J Jeffson

              @jsulm ,I want to quit the app after I send the packet,even though I delete that line,the result is the same.I can not capture the sending packet.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Jeffson What does m_ip_addr contain when you call get_reflector?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jeffson
                wrote on last edited by
                #7

                @jsulm the m_ip_addr is the DNS resolve of us-reflector.streamguys.com,the value is "75.102.43.54".
                thanks.

                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