Saturday, November 2, 2013

Get IP Address

In Java, it is not hard to get the IP address or host name of the local or remote machine. If your program has to retrieve the IP address or host name of a machine, you will use the InetAddress class. The instance of the InetAddress stores IP address and host name of the machine. The getLoopbackAddress method of the InetAddress will return the loop back address and the localhost name of the local machine. These information are embedded in an InetAddress object. Besides getLoopbackAddress method, you can use the getLocalHost method to get the IP address and the host name that you use to connect to your network. See the example code below.

import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

public class GetIP{
public static void main(String[] args) {
   try
    {

        InetAddress addr1 = InetAddress.getLoopbackAddress();
        InetAddress addr2 = InetAddress.getLocalHost();
        System.out.println("Host name:"+addr1.getHostName());
        System.out.println("IP:"+addr1.getHostAddress());
        System.out.println("Host name:"+addr2.getHostName());
        System.out.println("IP:"+addr2.getHostAddress());
        
        
           }catch(UnknownHostException e) {e.printStackTrace(); }

}
}


get local and remote machine IP address




The two methods above are able to get the IP addresses and host names of the local machine only. If you want to get the IP addresses of a remote machine (e.g. IP addresses of a web server), you will use the getByName method. This method accepts the host name of the remote machine. You will use the URL class to point to the remote host.

public static void getIPfromURL(String strurl){
try {
URL url=new URL(strurl);
InetAddress address = InetAddress.getByName(url.getHost());
System.out.println(address.getHostAddress());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

2 comments:

  1. hello,
    for the first codes i have
    Host name:localhost
    IP:127.0.0.1
    Host name:samraa
    IP:127.0.1.1
    what is the differents between two IP , how i can get my external IP (externe)
    thansk

    ReplyDelete
    Replies
    1. The two IP addresses are bounded to the loop back interface. It is not a great difference between those IP addresses. You might want to read http://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution to get more information about this bug in Linux OS.

      The following code will list all IP addresses bounded to your local machine.
      Enumeration Enet;
      try {
      Enet= NetworkInterface.getNetworkInterfaces();

      while(Enet.hasMoreElements()){
      NetworkInterface ni=(NetworkInterface) Enet.nextElement();
      Enumeration< InetAddress> Einet = ni.getInetAddresses();
      while(Einet.hasMoreElements()) {
      InetAddress ia= (InetAddress) Einet.nextElement();
      System.out.println(ia.getHostAddress());
      }
      }
      } catch (SocketException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }

      Delete