AdSense

AdSense3

Monday, 24 November 2014

Java InetAddress class

Java InetAddress class represents an IP address. The java.net.InetAddress class provides methods to get the IP of any host name for example www.kundansingh0510.blogspot.in, www.google.com, www.facebook.com etc.

Commonly used methods of InetAddress class

MethodDescription
public static InetAddress getByName(String host) throws UnknownHostExceptionit returns the instance of InetAddress containing LocalHost IP and name.
public static InetAddress getLocalHost() throws UnknownHostExceptionit returns the instance of InetAdddress containing local host name and address.
public String getHostName()it returns the host name of the IP address.
public String getHostAddress()it returns the IP address in string format.

Example of Java InetAddress class

Let's see a simple example of InetAddress class to get ip address of

www.kundansingh0510.blogspot.in

 website.
  1. import java.io.*;  
  2. import java.net.*;  
  3. public class InetDemo{  
  4. public static void main(String[] args){  
  5. try{  
  6. InetAddress ip=InetAddress.getByName("www.kundansingh0510.blogspot.in");  
  7.   
  8. System.out.println("Host Name: "+ip.getHostName());  
  9. System.out.println("IP Address: "+ip.getHostAddress());  
  10. }catch(Exception e){System.out.println(e);}  
  11. }  
  12. }  

Output:
Host Name: www.kundansingh0510.blogspot.in
IP Address: 206.51.231.148

No comments:

Post a Comment