Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can not send udp packet in QT



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


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

    @Jeffson What does m_ip_addr contain when you call get_reflector?



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


Log in to reply