SRL Archive - Java Help and Tutorials

Get devices on LAN


Richard

2015-04-13 15:45:36

I'm looking for a slicker way to access all of the online devices on a network using Java. I've currently got a method which displays all device names which have an open ServerSocket of a given port. The main application, shown below, then goes through all the devices on the subnet, and checks for this open SS. If it connects, it adds to the list of available devices.

public class Hosts {

public static void main(String[] args){
//extract subnet from local IP
String[] localIP = null;
try {
localIP = InetAddress.getLocalHost().getHostAddress().split("\\.");
} catch (UnknownHostException e) {}
String subnet = "";
for (int i = 0; i < 3; i++)
subnet += localIP[i] + ".";

//display all available hosts with given port open
InetAddress[] available = getNetworkAddresses(subnet);
for (InetAddress addr : available)
System.out.println(addr.getHostName().split("\\.")[0]);
}

public static InetAddress[] getNetworkAddresses(String subnet){
final int PORT = 7777;
final int TIMEOUT = 20;

ArrayList<InetAddress> result = new ArrayList<InetAddress>();

//loops through all IPs on the subnet, searching for connecting socket
for (int i = 1; i < 255; i++){
try {
InetAddress host = InetAddress.getByName(subnet + i);
Socket s = new Socket();
s.connect(new InetSocketAddress(host, PORT), TIMEOUT);
s.close();

//successful connect if exception not thrown
result.add(host);
} catch (SocketTimeoutException e){
//if connection, next iteration
continue;
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
}
return result.toArray(new InetAddress[result.size()]);
}
}

The issue with this is that a) it requires each device to be listening (this isn't necessarily a bad thing) and b) the timeout on each socket call causes the system to take a while (I know I could use threads, but this isn't particularly slick).

Is there a more effective way to view all available devices? I've try pinging each address but this is both slow, and doesn't actually yield results

tls

2015-04-13 19:38:54

Have you tried INetAddress.getAllByName(null)?

Kasi

2015-04-13 19:44:31

Async methods?

grats

2015-04-13 19:56:55

You can check how nmap does it, it's GPL does pretty much everything you can ask for as far as network scanning

Richard

2015-04-13 20:06:30

From: tls
Have you tried INetAddress.getAllByName(null)?


This just seems to return the local machine (i.e. localhost).

From: Kasi
Async methods?


I considered this, but threading seems to get the job done reasonable quickly, I was just hoping for a slightly more slick method, which wouldn't be so dependant on timeouts if there was a slow network speed.

From: grats
You can check how nmap does it, it's GPL does pretty much everything you can ask for as far as network scanning


I'll have a look into it, thanks!

tls

2015-04-13 20:12:06

A better test than connecting by socket would be InetAddress.isReachable(timeout)

Kasi

2015-04-13 20:12:10

@Richard; Pretty sure async methods are the way to do stuff like this. You could even call it a convention in Android i guess. I can't think of any other way right now.

Richard

2015-04-13 20:33:07

From: tls
A better test than connecting by socket would be InetAddress.isReachable(timeout)


isReachable doesn't seem to work, while pinging through cmd does seem to. Not sure why

From: Kasi
@Richard; Pretty sure async methods are the way to do stuff like this. You could even call it a convention in Android i guess. I can't think of any other way right now.


Could you give me an example? Honestly not familiar with async side of concurrency

Kasi

2015-04-13 20:56:43

@Richard; Google Async / Future tasks Java. Should be able to find some decent guides that would help you a lot more than i could.

Edit: http://java.dzone.com/articles/javautilconcurrentfuture

Richard

2015-04-14 07:00:37

From: Kasi
@Richard; Google Async / Future tasks Java. Should be able to find some decent guides that would help you a lot more than i could.

Edit: http://java.dzone.com/articles/javautilconcurrentfuture


Thanks. I actually found a better way to perform it (still making use of async methods), but doesn't require the use of an open socket.

Thanks all for the help

tls

2015-04-14 13:53:21

From: Richard
Thanks. I actually found a better way to perform it (still making use of async methods), but doesn't require the use of an open socket.

Thanks all for the help


Sharing is caring.

Kasi

2015-04-14 17:26:03

From: Richard
Thanks. I actually found a better way to perform it (still making use of async methods), but doesn't require the use of an open socket.

Thanks all for the help


No problem.