Search This Blog


Thursday, February 27, 2014

How to Find IP address of Localhost, Server or Your Machine

In today's Java programming tutorial, we will learn some networking basics by exploring package. One of the simple Java network programming exercise, yet very useful, is to write a program to find IP address of local host in Java. Some time this question also asked as find IP address of Server on which your Java program is running, or find IP address of your machine using Java etc. In short, they all refer to localhost. For those who are entirely new in networking space, there are two things to identify a machine in a network, which could be LAN, WAN or Internet. First thing is DNS name and second thing is IP address. In Local Area Network, DNS is more generally replaced by hostname, which could be as simple as ReportingServer to or something else. Both this hostname and IP address are related to each other, which means given hostname, your computer can find IP address and vice-versa. The file they use for hostname to IP address resolution is /etc/host in both Windows 8 and Linux. Java provides API to get this IP address by providing hostname. Since localhost is used to refer the machine on which program is running, you can provide that to get IP address or your Server, or your desktop in Java. By the way, if you are doing support and need it for real purpose there are more easy windows and Linux commands to convert hostname to IP address and vice-versa, you can check them here.

Java program to find IP address of  Localhost

Java program to find IP address of localhost
Here is our sample program to show IP address of localhost. Key class to know is InetAddress, this class encapsulate hostname and IP address information. It provides a static method, called InetAddress.getLocalHost() to get an instance of this class corresponding to localhost. This may throw if name localhost is not specified in your /etc/host file, in general if there is any issue while resolving that hostname. Once you get instance of InetAddress, you can call getHostAddress() method to retrieve IP address of host. If you defined another computer name for your host, apart from default alias localhost, you can get that name by calling getHostName() method.

import java.util.Arrays;

 * Java program to find IP address and hostname of a machine. InetAddress of
 * package is used to get IP address and hostname in Java applications.
 * @author WINDOWS 8
public class IPAddress {

    public static void main(String args[]) {

        // First get InetAddress for the machine, here localhost
        InetAddress myIP = null;
        try {
            myIP = InetAddress.getLocalHost();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block

        // getHostAddress() returns IP address of a machine
        String IPAddress = myIP.getHostAddress();

        // getHostname returns DNS hostname of a machine
        String hostname = myIP.getHostName();

        System.out.printf("IP address of Localhost is %s %n", IPAddress);
        System.out.printf("Host name of your machine is %s %n", hostname);



IP address of Localhost is
Host name of your machine is Javarevisited

That's all on how to find IP address of your machine in Java.  As I said before, since your machine is also referred as localhost, you can use this program to answer question, which asks about finding IP address of localhost in Java. In next article, we will learn about how to find IP address of any host in Java program. I am also thinking to share some basics of socket programming using traditional IO and NIO, let me know if you guys are interested on that.

1 comment: