Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday, 29 June 2011

Program to get IP address in java

    getLocalHost() function is used to display Name and IP address of local system.
    getByName(" ") function is used to display Name and IP address of a given website.
    getAllByName() function is used to display more than one IP address of  a given website. 

import java.net.*;
public class Main{

    public Main(){
    }
    
    public static void main(String[] args) {
    try
    {    
     InetAddress add= InetAddress.getLocalHost();


     System.out.println("LOCAL HOST NAME & IP ADDRESS: "+add);
     InetAddress add1=InetAddress.getByName("facebook.com");
     System.out.println("NAME AND IP ADDRESS OF THE STRING: "+add1);
     InetAddress ias[]=InetAddress.getAllByName("google.com");
     for(int i=0;i<ias.length;i++)
     {
         System.out.println(ias[i]);
     }
     String s=add.getHostName();
     System.out.println("NAME OF THE HOST: "+s);
     String s1=add.getHostAddress();
     System.out.println("ADDRESS OF HOST: "+s1);
     byte[] a=add.getAddress();
     for(int j=0;j<a.length;j++)
     {
     System.out.println(a[j]);
    }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
}

Output:
LOCAL HOST NAME & IP ADDRESS: nw1-120/165.165.1.120
NAME AND IP ADDRESS OF THE STRING: facebook.com/69.63.189.11
google.com/74.125.236.50
google.com/74.125.236.49
google.com/74.125.236.52
google.com/74.125.236.51
google.com/74.125.236.48
NAME OF THE HOST: nw1-120
ADDRESS OF HOST: 165.165.1.120
-91
-91
1
120
BUILD SUCCESSFUL (total time: 0 seconds)


No comments:

Post a Comment

Followers