Below example shows how to get list of all IPs for the given host. By passing host name to InetAddress.getAllByName() method, you can get list of InetAddress objects. You have to call getHostAddress() method on each object to get all list of IPs.
HERE IS THE CODE :
-------------------------
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetAllIPsOfHostName {
public static void main(String[] args) {
try {
InetAddress[] myHost = InetAddress.getAllByName("www.google.com");
for(InetAddress host:myHost){
System.out.println(host.getHostAddress());
}
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
}
}
No comments:
Post a Comment