Thursday 6 March 2014

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

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

/*
 * @author Pandey
 */
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
            e.printStackTrace();
        }

        // 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);

    }

}

Output:
IP address of Localhost is 192.168.1.41
Host name of your machine is Jaachase

No comments:

Post a Comment