简体   繁体   中英

geting ip address of all host connected to LAN in java

I want to develop a LAN search tool. For that I need all connected host IPs. For that I'm using this, but it did not work for me. It gives all result not reachable, even my own IP.

Please help me whats going wrong in this

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

Your code seems fine, try increasing the timeout .

4s-5s timeout would be great, it should at least show your own IP as reachable.


Also, isReachable 's documentation says:

Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status.

So, if other cases fail make sure there are no firewall issues.

Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM