views:

191

answers:

6

How to get list of IP connected in LAN using Java.

+1  A: 

To list the hosts connected in a LAN you will need to ping all the available IP addresses on the subnet. But a ping message could be restricted by firewall thus safer way could be open a socket to each IP address in the LAN's IP address range.

Suhas Phartale
socket? to which port? and how to be so sure that firewall does not block it?
Gopi
What firewall? It's a LAN.
EJP
+1 for providing basic information to ping all ip's in subnet.
YoK
And the LAN could be split across multiple subnets.
Steve Kuo
+4  A: 

this should work when the hosts on your network react to ICMP packages (ping) (>JDK 5):

public void checkHosts(String subnet){
   int timeout=1000;
   for (int i=1;i<254;i++){
       String host=subnet + "." + i;
       if (InetAddress.getByName(host).isReachable(timeout)){
           System.out.println(host + " is reachable");
       }
   }
}

invoke the method for a subnet (192.168.0.1-254) like this:

checkHosts("192.168.0");

didnt test it but should work kinda like this. Obviously this only checks the 254 hosts in the last byte of the ip address...

check:

http://download-llnw.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29 http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/

hope that helped

smeg4brains
Good solution provided 'LAN' = 'class D IP subnet'.
EJP
Thanks for providing explanation.
org.life.java
A: 

If you mean a list of all hosts connected to the network, I think the only way that is guaranteed to work is to step through a list of IP addresses and ping them all.

That said, if you're looking for something more specific, there may be something you can look up (e.g. RMI's registry (LocateRegistry.getRegistry(host, port).list()).

Also, if you just want all the IP addresses that a given host has, have a look at NetworkInterface.getNetworkInterfaces().

Scott
Nope. LocateRegistry.getRegistry doesn't do any communications whatsoever. It just constructs a Registry stub.
EJP
Calling list() on it should, though. Anyway, it's a bit moot - I'm forgetting that by default RMI only allows objects to register with local registries, so you'd still need to check for a registry on every host. You'd need to use something like Bonjour to do service discovery - which was my point.
Scott
A: 

Since Java 1.5 there is a ping-like method in java.net.InetAddress: public boolean isReachable(int timeout). You could use that to iterate over all the IP Addresses in your subnet... java-doc

vicatcu
A: 

One of the problems here is that neither of the terms "LAN" and "connected" has a meaning in TCP/IP. The suggested technique of calling isReachable() on all the hosts in the class D subnet might work if your LAN corresponds precisely to a class-D subnet.

You might be better off looking at SAMBA, which can interrogate the LAN members via SMBs, so at least you'll be using a technique that has the same meaning for LAN that you do.

EJP
A: 

.isReachable() method is an excellent solution!!!! I was thinking to ping the network broadcast address (eg: x.y.z.255) and check for DUP packets in order to see what hosts are on the subnet.

Daniel Voina