How to get a list of IP connected in same network (subnet) using Java How to get a list of IP connected in same network (subnet) using Java java java

How to get a list of IP connected in same network (subnet) using Java


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<255;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%29http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/

hope that helped


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.


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