【问题标题】:C++ Check Internet Connection on Linux UbuntuC++ 检查 Linux Ubuntu 上的 Internet 连接
【发布时间】:2017-06-05 11:00:24
【问题描述】:

有没有办法在 Linux Ubuntu 上检查 Internet 连接?因为这个只适用于Windows--programmatically check whether my machine has internet access or not

或者使用 Poco 库。

【问题讨论】:

  • this solution 有什么问题,在同一个问题中给出?使用套接字库连接到已知地址。
  • @CodyGray 我们如何实现它?
  • 使用套接字库,就像我已经说过的那样。我想这真的是一个“给我的代码”问题。
  • 好的。谢谢!我找到了正确答案。

标签: c++ networking poco-libraries


【解决方案1】:

Linux下可以使用route命令查看

FILE *output;

if(!(output = popen("/sbin/route -n | grep -c '^0\\.0\\.0\\.0'","r")))
{
    return 1;
}
unsigned int i;
fscanf(output,"%u",&i);
if(i == 1)
       cerr<<"There is internet connection\n";
else if(i == 0)
       cerr<<"There is no internet connection\n";
pclose(output);

你可以找到更多关于它的信息here

【讨论】:

  • 它只会给你设置默认路由的信息。但如果您可以访问互联网 IP 地址将无法保证。因此,您应该打开与 Internet 中众所周知的 IP 地址的连接,例如 8.8.8.8 / 8.8.8.4(谷歌 DNS 服务器)
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 2013-03-15
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
相关资源
最近更新 更多