Friday, June 11, 2010

Getting Local IP Address in Java

An IP (Internet Protocol) address is a numerical unique identification number which is assigned to the devices participating in the network for communication. To get IP address of our own

In our following example program we are getting system's IP by getting the Host address of the local host.

InetAddress ownIP=InetAddress.getLocalHost(); gets the

local host with IP address. Now we can get the IP address with this object by calling method getHostAddress() on the InetAddress object.

Here is the full example code of GetOwnIP.java as follows:




import
java.util.*;
import java.lang.*;
import java.net.*;

public class GetOwnIP
{
public static void main(String args[]) {
try{
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my system is : "+ownIP.getHostAddress());
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
}
}
}

No comments:

Post a Comment