Friday, 15 November 2013

Get IP address in Java using InetAddress

The following example illustrates getting IP address in Java using InetAddress class.

import java.net.*;
class GetIP
{
    public static void main(String args[]) throws Exception
    {
        InetAddress i=InetAddress.getByName(args[0]);
        System.out.println(new String(i.getHostAddress()));
    }
}
getByName(args[0]): This static method of InetAddress class gets the IP address of the given host name which has to be in the format, google.com for example.

No comments:

Post a Comment