问题描述
作为一项任务,我必须找到局域网上所有活着的计算机。我正在使用 isReachable
函数 InetAddress
类。但问题是没有任何东西可以显示给我。所以我尝试使用Google的IP isReachable
,但仍然无法访问。
As an assignment I have to find all the alive computers on a LAN. For which I am using isReachable
function of InetAddress
class. But problem is that nothing is shown reachable to me. So I tried to have isReachable
with Google's IP but still this is unreachable.
以下是代码:
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()可能无法始终工作的详细信息预期
Here are some details on why isReachable() might not always work as expected
- http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html
- http://www.coderanch.com/t/206934/sockets/java/InetAdress-isReachable-Ping-Permissions
正确的方法是使用协议。我相信这就是 ping 使用internatlly。 开始了。
The correct way for you is to use the ICMP protocol. This is what ping uses internatlly, I believe. Here is an example that get you started.
这篇关于InetAddress类中的isReachable问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!