Here , is the very simple code to get IP address of hosts connected with your system:

import java.net.*;

public class GetAll{

public static void main(String args[]){

try{

InetAddress localhost = InetAddress.getLocalHost();

// this code assumes IPv4 is used

byte[] ip = localhost.getAddress();

for (int i = 1; i <= 254; i++)
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);

if (address.isReachable(1000))
{
System.out.println(“Rechable = “+address.toString());
// machine is turned on and can be pinged
}
else if (!address.getHostAddress().equals(address.getHostName()))
{

// machine is known in a DNS lookup
System.out.println(“DNS = “+address.toString());
}
else
{
// the host address and host name are equal, meaning the host name could not be resolved
System.out.println(“Host name cannot be resolved = “+address.toString());
}
}
}
catch(Exception e)
{
}

}//end of main
}

Get IP address of all the hosts connected with your system

Leave a Reply

Your email address will not be published.