Pages

Tuesday 1 April 2014

How to get All IP address of given host name using Java

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();
       }

}

}


KEEP SHARING KNOWLEDGE

No comments:

Post a Comment