Java program to get IP address
In this example we are gonna see how to get IP address of a System. The steps are as follows:
1) Get the local host address by calling
2) Get the IP address by calling
getLocalHost()
method of InetAddress class.2) Get the IP address by calling
getHostAddress()
method.import java.net.InetAddress;
class GetMyIPAddress
{
public static void main(String args[]) throws Exception
{
/* public static InetAddress getLocalHost()
* throws UnknownHostException: Returns the address
* of the local host. This is achieved by retrieving
* the name of the host from the system, then resolving
* that name into an InetAddress. Note: The resolved
* address may be cached for a short period of time.
*/
InetAddress myIP=InetAddress.getLocalHost();
/* public String getHostAddress(): Returns the IP
* address string in textual presentation.
*/
System.out.println("My IP Address is:");
System.out.println(myIP.getHostAddress());
}
}
Output:
My IP Address is:
115.242.7.243
not too much code but this is better
ReplyDeletethis code very useful
ReplyDelete