作为一项作业,我必须找到 LAN 上所有 Activity 的计算机。为此,我正在使用 InetAddress
类的 isReachable
函数。但问题是我什么都看不到。所以我尝试使用 Google 的 IP 设置 isReachable
,但仍然无法访问。
代码如下:
import java.net.*;
public class alive{
public static void main(String args[]){
try{
InetAddress ia = InetAddress.getByAddress(new byte[]{(byte)209, (byte)85, (byte)153, (byte)104});
boolean b = ia.isReachable(10000);
if(b){
System.out.println("Reachable");
}
else{
System.out.println("Unrachable");
}
}catch(Exception e){
System.out.println("Exception: " + e.getMessage());
}
}
}
输出是:无法访问
最佳答案
这里有一些关于为什么 isReachable() 可能并不总是按预期工作的细节
- http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html
- http://www.coderanch.com/t/206934/sockets/java/InetAdress-isReachable-Ping-Permissions
您的正确方法是使用 ICMP协议(protocol)。我相信这就是 ping 在国际上使用的东西。 Here is an example这让你开始。
关于java - InetAddress 类中的 isReachable 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4779367/