address.isReachable不会发现网络上的所有节点 [英] address.isReachable won't discover all nodes on a network

查看:160
本文介绍了address.isReachable不会发现网络上的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将代码精简到基本要领,它非常简单直接.

I have trimmed my code down to the bare essentials, its pretty simple and straight forward.

我有以下代码:

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;
}

这是节点类,这很简单:

Here is the node class, which is pretty simple:

public class Node 
{
    public Node(String address, boolean restricted)
    {
        this.address = address;
        this.restricted = restricted;
    }

    public String address;
    public boolean restricted;
}

这是我的主要代码,它执行getNodes():

Here is my main code, which executes getNodes():

case 1:
    System.out.println("Searching for nodes...");
    NodeDetector node = new NodeDetector(); // this is the class
                                           //where getNodes resides
    ArrayList<Node> nodes = node.getNodes();

    Iterator<Node> it = nodes.iterator();
    while(it.hasNext())
    {
        System.out.println("Node: "+it.next().address);
    }

    System.out.println("stopped searching for nodes...");
    break;


这是我的输出:


Here is my output:

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...


现在是问题所在

我从手机上下载了一个网络节点发现工具,它至少可以找到5个节点.我尝试更改超时值,但仍然没有运气.当我ping通使用手机而非计算机上的网络工具找到的地址时,便会立即收到并返回ping.这个问题是相似的,它对我有所帮助,但我仍然被困住:


Now here's the problem

I have a network node discovery tool i downloaded on my phone and it can find at least 5 more nodes. I tried changing the timeout value but still no luck. When i ping an address that is found with the network tool on my phone and not on my computer, the ping is instantly received and returned. This question is similar and it has helped me a bit, but I'm still stuck:

我在Mac上运行我的工具,拾起其他Mac,iPod和路由器似乎效果很好,但仅此而已.为什么我的程序无法检测到网络上的其他设备?

I am running my tool on a mac, it seems to work well picking up other macs, iPods and routers but thats about it. Why can't my program detect the other devices on the network?

这是我从手机上的网络工具获得的输出:

Here is the output i get from my network tool on my phone:

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 *


我在手机上的工具与我在计算机上编写的工具一致的地方加了*.我已经进行了几次测试,每次在计算机和手机上都得到相同的输出,因此在测试过程中没有从网络中添加或删除任何设备.


I put an * where the tool on my phone agrees with the tool i am writing on my computer. I have ran this test a couple of times, i get the same output every time on both my computer and on my phone, no devices were added or removed from the network during the tests.

推荐答案

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

After a couple days of research I have come across this as an ok solution:

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);
}

唯一的缺点是它与系统有关.我将是唯一使用此工具的人,对我来说真的没有问题.

The only drawback on this is that it is system dependent. Im going to be the only one using this tool so that really is no problem for me.

这篇关于address.isReachable不会发现网络上的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆