Friday 9 October 2015

InetAddress : Get hostname from the IP address

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressUtil {

 public static InetAddress getByName(String hostName) {
  try {
   return InetAddress.getByName(hostName);
  } catch (UnknownHostException e) {
   System.out.println(e);
  }
  return null;
 }
 
 public static String getHostName(String ip){
  InetAddress address = getByName(ip);
  return address.getHostName();
 }

}

public class Main {
 public static void main(String args[]) {
  String hostName = InetAddressUtil.getHostName("64.233.169.99");
  System.out.println(hostName);
 }
}


If the address you look up does not have a hostname, getHostName() simply returns the dotted quad address you supplied.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment