Friday, 9 October 2015

Java InetAddress : Represent IP address


InetAddress represents IP address. Many Java networking classes use this class.

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


import java.net.InetAddress;

public class Main {
 public static void main(String args[]) {
  InetAddress address = InetAddressUtil.getByName("www.google.com");

  System.out.println(address);
 }
}


Output

www.google.com/64.233.169.99

InetAddress class provides number of methods to get

There are several usefull methods other than mentioned above, go through the javadoc for InetAddress class for more information.

Note
Always use hostname than IP addresses, since hostnames change rarely.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment