如何使用nslookup检查Java中的Internet可用性?
目前我正在使用URL() 但是在需求中,我们不想使用任何URL。如果连接可用,我们希望ping localhost,否则返回true 对于我使用的nslookup如何使用nslookup检查Java中的Internet可用性?,java,Java,目前我正在使用URL() 但是在需求中,我们不想使用任何URL。如果连接可用,我们希望ping localhost,否则返回true 对于我使用的nslookup try { InetAddress inetAddress = InetAddress.getByName("localhost"); System.out.println("Host: " +inetAddress.getHostName()); System.out.println("
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();
}
但我不知道如何使用nslookup检查连接可用性
请建议最好的方法。谢谢这个问题有几个要点:
本地主机
:即使您的计算机没有网卡,它也能够解析网络上的本地主机并连接到自身(如果有打开的端口)example.com
,但您可以通过IP连接到它(93.184.216.34
)-这是否意味着“internet不可用”example.com
的服务器已关闭,而世界上所有其他站点都工作正常。这意味着“互联网可用”还是“不可用”http://example.com
连接,但http://example.com:12345
没有。这意味着“互联网可用”还是“不可用”因此,您真正可以问的唯一问题是,您是否可以使用其域名和/或IP地址连接到特定端口上的特定主机。使用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;
}
}
}
枚举eni=NetworkInterface.getNetworkInterfaces();
而(eni.hasMoreElements()){
枚举eia=eni.nextElement().getInetAddresses();
while(eia.hasMoreElements()){
InetAddress ia=eia.nextElement();
如果(!ia.isAnyLocalAddress()&&&!ia.isLoopbackAddress()&&!ia.isSiteLocalAddress()){
如果(!ia.getHostName().equals(ia.getHostAddress()))
返回true;
}
}
}
我从这里得到了解决方案:-检查本地主机是否可用如何证明Internet可用?嗨,Adam,我们可以假设DNS服务器随时可用,可能重复:通常
nslookup
是本地系统上程序的一个众所周知的名称,而不是服务nslookup
将对网络执行一些DNS查询或从本地系统(主机
文件)检索信息,但根本不能保证Internet可用性。如果您想从Java运行nslookup
,首先读取总是最重要的。我不想使用任何域名来检查internet可用性。
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;
}
}
}