2012-07-06 59 views
1

我已经将我的代码修剪到最基本的要领,它非常简单直接。address.isReachable不会发现网络上的所有节点

我有以下代码:

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

这里是节点类,这是非常简单的:

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

    public String address; 
    public boolean restricted; 
} 

这是我主要的代码,它执行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; 

这是我的输出:

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不能立即收到并返回。这个问题是类似的,它帮助我了一点,但我仍然坚持:

我正在我在Mac工具,它似乎运作良好捡其他Mac ,iPod和路由器,但多数民众赞成在它。为什么我的程序无法检测网络上的其他设备?


下面是输出,我从我的网络工具,让我的手机上:

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 * 

我把在我的手机上的工具与工具我在写同意的*我电脑。我已经多次运行这个测试,每次在我的电脑和手机上都得到相同的输出,在测试过程中没有任何设备被添加或从网络中移除。

+0

你为什么要在catch块中创建并抛出Node?你为什么不记录异常? – EJP 2012-07-07 06:48:31

+0

因为唯一会引发异常的是如果它是一个受限制的IP,我想将它记录为受限制的IP,因此受限制的参数为true。 – John 2012-07-07 14:53:52

回答

1

后研究了几天我也碰到过这个作为一个好的解决方案:

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

上唯一的缺点是,它是取决于系统。我将成为唯一使用这个工具的人,这对我来说确实没有问题。