简体   繁体   English

如何使用nslookup检查Java中的Internet可用性?

[英]How to check Internet availability in Java using nslookup?

Currently I am using URL() 目前,我正在使用URL()

public boolean isInternetAvailable(){
    try {
    URL url = new URL("http://www.google.com");
    HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();
    urlConnect.setConnectTimeout(5000);
    Object objData = urlConnect.getContent();
    return true;
    } catch (Exception e) {}
    return false;
}

But In the requirement, we don't want to use any URL. 但在要求中,我们不想使用任何URL。 We want to ping localhost if connection is available than return true otherwise flase. 如果连接可用,我们要ping通localhost,否则返回true。

For nslookup I am using 对于nslookup我正在使用

try
    {
      InetAddress inetAddress = InetAddress.getByName("localhost");

      System.out.println("Host: " +inetAddress.getHostName());
      System.out.println("IP Address: " +inetAddress.getHostAddress());
      System.out.println("IP Address: " +inetAddress.isSiteLocalAddress());
    }
    catch (UnknownHostException e)
    {
      e.printStackTrace();
    }

But I am not understand how to check the connection availability with nslookup. 但是我不明白如何使用nslookup检查连接可用性。

Please suggest best approach for it. 请提出最佳方法。 Thanks 谢谢

There are several catches to this question: 这个问题有几个陷阱:

  1. Starting with the most specific one - connecting to localhost : even if your computer does not have a network card, it will be able to resolve localhost on a loopback interface and connect to itself (if there is an open port). 从最具体的一个开始-连接到localhost :即使您的计算机没有网卡,它也可以在环回接口上解析localhost并连接到自身(如果有开放端口)。
  2. Your DNS may be down/misconfigured, so you cannot resolve example.com but you can connect to it by IP ( 93.184.216.34 ) - does that mean than "internet is not available"? 您的DNS可能已关闭/配置错误,因此您无法解析example.com但可以通过IP( 93.184.216.34 )连接到它-这是否意味着“互联网不可用”?
  3. The firewall in your company may be blocking certain sites, but allowing other - does that mean than "internet is not available"? 您公司中的防火墙可能阻止了某些站点,但允许其他站点-是不是“互联网不可用”?
  4. The server of example.com is down while all the other sites in the world work fine. example.com的服务器已关闭,而世界上所有其他站点均正常运行。 Does that mean than "internet is available" or not? 这是否意味着“ Internet可用”?
  5. The firewall in your company may be allowing HTTP connections only on standard ports 80 and 443 and disallowing other. 您公司的防火墙可能只允许在标准端口80和443上进行HTTP连接,而不允许其他端口。 Thus, http://example.com connects, but http://example.com:12345 does not. 因此, http://example.com连接,但http://example.com:12345不会。 Does that mean than "internet is available" or not? 这是否意味着“ Internet可用”?

So the only question you can actually ask is whether you can connect to a particular host on a particular port using its domain name and/or its IP address. 因此,您真正要问的唯一问题是,是否可以使用其域名和/或IP地址连接到特定端口上的特定主机。

Figured out a final solution using NetworkInterface: 使用NetworkInterface找出最终解决方案:

Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces();
    while(eni.hasMoreElements()) {
        Enumeration<InetAddress> eia = eni.nextElement().getInetAddresses();
        while(eia.hasMoreElements()) {
            InetAddress ia = eia.nextElement();
            if (!ia.isAnyLocalAddress() && !ia.isLoopbackAddress() && !ia.isSiteLocalAddress()) {
                if (!ia.getHostName().equals(ia.getHostAddress()))
                    return true;
            }
        }
    }

I have got solution from here:- Java Quickly check for network connection 我从这里得到了解决方案: -Java快速检查网络连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM