Reputation: 3807
I have trimmed my code down to the bare essentials, its pretty simple and straight forward.
I have the following code:
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;
}
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...
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:
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.
Upvotes: 1
Views: 909
Reputation: 3807
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.
Upvotes: 1