美文网首页
嵌入式linux c/c++检测联网思路及代码

嵌入式linux c/c++检测联网思路及代码

作者: LinGeeHuy | 来源:发表于2022-05-03 14:26 被阅读0次

    最近做嵌入式linux有检测联网状态的需求,百度了一下其他方案,采用ping dns方案比较稳定合适,ping多个dns服务器,以及自己的后台服务器,任意一个能ping通,则说明设备已联网。

    QMutex paltform_main_mutex;
    int _net_connected = 0;
    int _check_networking_complete = 1;
    
    /**
     * @brief Set the net connected object
     *
     * @param connected
     */
    void set_net_connected(int connected)
    {
      QMutexLocker locker(&paltform_main_mutex);
      _net_connected = connected;
    }
    
    /**
     * @brief net connected
     *
     * @return int
     */
    int net_connected()
    {
      QMutexLocker locker(&paltform_main_mutex);
      return _net_connected;
    }
    
    /**
     * @brief Set the net connected object
     *
     * @param connected
     */
    void set_net_connected(int connected)
    {
      QMutexLocker locker(&paltform_main_mutex);
      _net_connected = connected;
    }
    
    /**
     * @brief check networking complete
     *
     * @return int
     */
    int check_networking_complete()
    {
      QMutexLocker locker(&paltform_main_mutex);
      return _check_networking_complete;
    }
    
    /**
     * @brief check_networking_thread
     *
     * @param arg
     * @return void*
     */
    static void *check_networking_thread(void *arg)
    {
      FILE *fp;
      char buffer[80];
    
      memset(buffer, 0, sizeof(buffer));
      fp = popen("ping 8.8.8.8 -c 3 |grep 'ttl='", "r");
      fgets(buffer, sizeof(buffer), fp);
      pclose(fp);
      if (strlen(buffer) > 0)
      {
        ALOGD("[%s]: google dns ok, device is networking", __func__);
        set_net_connected(1);
        set_check_networking_complete(1);
        return NULL;
      }
    
      memset(buffer, 0, sizeof(buffer));
      fp = popen("ping 114.114.114.114 -c 3 |grep 'ttl='", "r");
      fgets(buffer, sizeof(buffer), fp);
      pclose(fp);
      if (strlen(buffer) > 0)
      {
        ALOGD("[%s]: china dns ok, device is networking", __func__);
        set_net_connected(1);
        set_check_networking_complete(1);
        return NULL;
      }
     set_net_connected(0);
      ALOGD("[%s]: device is not network", __func__);
      set_check_networking_complete(1);
      return NULL;
    }
    
    /**
     * @brief check networking
     *
     */
    void check_networking()
    {
      if (!check_networking_complete())
      {
        return;
      }
      set_check_networking_complete(0);
      pthread_t _thrd;
      if (pthread_create(&_thrd, NULL, check_networking_thread, NULL))
      {
        ALOGE("create check_networking_thread failed, "
              "errno: %d (%s)",
              errno, strerror(errno));
        set_check_networking_complete(1);
        return;
      }
      pthread_detach(_thrd);
    }
    

    定时执行check_networking()方法进程,使用net_connected()判断是否联网。

    相关文章

      网友评论

          本文标题:嵌入式linux c/c++检测联网思路及代码

          本文链接:https://www.haomeiwen.com/subject/ptabyrtx.html