问题描述
作为一项任务,我必须找到 LAN 上所有活动的计算机.为此,我使用了 InetAddress
类的 isReachable
函数.但问题是我没有显示任何东西可以访问.因此,我尝试使用 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());
}
}
}
输出是:Unreachable
推荐答案
这里有一些关于为什么 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
对您来说正确的方法是使用 ICMP协议.我相信这就是 ping 在内部使用的.这里是一个例子开始了.
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 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!