Java address.isReachable-won';t发现网络上的所有节点

Java address.isReachable-won';t发现网络上的所有节点,java,networking,ping,Java,Networking,Ping,我已经把我的代码精简到了最基本的部分,非常简单和直接 我有以下代码: public ArrayList<Node> getNodes() throws IOException { ArrayList<Node> nodes = new ArrayList<Node>(); StringBuffer root = new StringBuffer(InetAddress.getLocalHost().getHostAddress());

我已经把我的代码精简到了最基本的部分,非常简单和直接

我有以下代码:

public ArrayList<Node> getNodes() throws IOException
{
    ArrayList<Node> nodes = new ArrayList<Node>();

    StringBuffer root = new StringBuffer(InetAddress.getLocalHost().getHostAddress());
    while(!root.toString().endsWith("."))
        root.deleteCharAt(root.length() - 1);
    //^^ this code gets the ip, for ex 127.0.0.1, and trims the last number, to make it
    //^^ 127.0.0.  <-- see the trailing 0

    for(int host = 0;host < 256; host++)
    {
        InetAddress address = InetAddress.getByName(root.toString() + host);
        try
        {
            if(address.isReachable(500)) // pings the address
                nodes.add(new Node(address.getHostAddress(), false));
        }catch(Exception e){new Node(address.getHostAddress(), true);}
    }

    return nodes;
}
这是我的主要代码,它执行getNodes()

案例1:
System.out.println(“搜索节点…”);
NodeDetector节点=新的NodeDetector();//这是一节课
//getNodes所在的位置
ArrayList nodes=node.getNodes();
Iterator it=nodes.Iterator();
while(it.hasNext())
{
System.out.println(“节点:“+it.next().address”);
}
System.out.println(“停止搜索节点…”);
打破

以下是我的输出:

Searching for nodes...
Node: 00.00.17.99
Node: 00.00.17.100
Node: 00.00.17.149
Node: 00.00.17.150 <-- this is my computer
Node: 00.00.17.154
Node: 00.00.17.156
Node: 00.00.17.254
stopped searching for nodes...
正在搜索节点。。。
节点:00.00.17.99
节点:00.00.17.100
节点:00.00.17.149

Node:00.00.17.150经过几天的研究,我发现这是一个不错的解决方案:

try
{
    Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 -W 250 " + address.getHostAddress());
    int returnVal = p1.waitFor();
    boolean reachable = (returnVal==0);


    if(reachable)
        nodes.add(new Node(address.getHostAddress(), false));
}catch(Exception e)
{
    new Node(address.getHostAddress(), true);
}

唯一的缺点是它依赖于系统。我将是唯一一个使用此工具的人,因此这对我来说真的没有问题。

经过几天的研究,我发现这是一个不错的解决方案:

try
{
    Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 -W 250 " + address.getHostAddress());
    int returnVal = p1.waitFor();
    boolean reachable = (returnVal==0);


    if(reachable)
        nodes.add(new Node(address.getHostAddress(), false));
}catch(Exception e)
{
    new Node(address.getHostAddress(), true);
}

唯一的缺点是它依赖于系统。我将是唯一一个使用此工具的人,因此这对我来说真的没有问题。

为什么要在catch块中创建并丢弃一个节点?为什么不记录异常?因为唯一会抛出的异常是受限IP,我想将其记录为受限IP,因此受限参数为true。为什么要在catch块中创建并丢弃节点?为什么不记录异常?因为唯一会抛出的异常是如果它是受限IP,我想将其记录为受限IP,因此受限参数为true。
00.00.17.99 <-- SMC Networks *
00.00.17.100 <-- XEROX *
00.00.17.133 <-- My Phone (Android)
00.00.17.134 <-- Intel
00.00.17.142 <-- Apple
00.00.17.149 <-- Apple *
00.00.17.150 <-- Apple * <-- this is my computer
00.00.17.154 <-- Apple *
00.00.17.155 <-- Intel
00.00.17.156 <-- Apple *
00.00.17.158 <-- Motorola Mobility
00.00.17.254 <-- Netopia *
try
{
    Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 -W 250 " + address.getHostAddress());
    int returnVal = p1.waitFor();
    boolean reachable = (returnVal==0);


    if(reachable)
        nodes.add(new Node(address.getHostAddress(), false));
}catch(Exception e)
{
    new Node(address.getHostAddress(), true);
}