Java Local Address Get getLocalAddress()

Here you can find the source of getLocalAddress()

Description

This method returns the local IP address as a String.

License

Open Source License

Declaration

public static String getLocalAddress() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.*;

public class Main {
    /**//  www. j  av a2 s.  co m
     * This method returns the local IP address as a String. If for some reason
     * the local IP address can't be determined it returns <code>0.0.0.0</code>
     * <p>
     * An example of a situation where the local IP address can't be determined
     * is a machine without a network config and not connected to the internet
     * with a modem.
     */
    public static String getLocalAddress() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (Exception ex) {
            return "0.0.0.0";
        }
    }

    /**
     * Gets the local machines host name if it has one. If it does not have a
     * local host name then localhost is returned.
     */
    public static String getLocalHost() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (Exception _ex) {
            return "localhost";
        }
    }
}

Related

  1. getLocalAddr()
  2. getLocalAddr()
  3. getLocalAddress()
  4. getLocalAddress()
  5. getLocalAddress()
  6. getLocalAddress()