Java IP Address Get getLocalHostIP()

Here you can find the source of getLocalHostIP()

Description

Returns the IP address of the local host as string.

License

Open Source License

Return

the IP address, null if not available

Declaration

public static synchronized String getLocalHostIP() 

Method Source Code

//package com.java2s;
/*/* w ww .j  a  v a2  s  . com*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    /** IP (from network intf) for faster access. */
    protected static String m_IPNetworkInterface;
    /** IP for faster access. */
    protected static String m_IP;

    /**
     * Returns the IP address of the local host as string.
     *
     * @return      the IP address, null if not available
     */
    public static synchronized String getLocalHostIP() {
        String result;

        if (m_IP == null) {
            try {
                result = InetAddress.getLocalHost().getHostAddress();
            } catch (Exception e) {
                result = getIPFromNetworkInterface();
            }

            m_IP = result;
        } else {
            result = m_IP;
        }

        return result;
    }

    /**
     * Returns the IP address determined from the network interfaces (using
     * the IP address of the one with a proper host name).
     *
     * @return       the IP address
     */
    public static synchronized String getIPFromNetworkInterface() {
        String result;
        List<String> list;
        Enumeration<NetworkInterface> enmI;
        NetworkInterface intf;
        Enumeration<InetAddress> enmA;
        InetAddress addr;
        boolean found;

        result = null;

        if (m_IPNetworkInterface == null) {
            list = new ArrayList<>();
            found = false;
            try {
                enmI = NetworkInterface.getNetworkInterfaces();
                while (enmI.hasMoreElements()) {
                    intf = enmI.nextElement();
                    // skip non-active ones
                    if (!intf.isUp())
                        continue;
                    enmA = intf.getInetAddresses();
                    while (enmA.hasMoreElements()) {
                        addr = enmA.nextElement();
                        list.add(addr.getHostAddress());
                        if (addr.getHostName().indexOf(':') == -1) {
                            result = addr.getHostAddress();
                            found = true;
                            break;
                        }
                    }
                    if (found)
                        break;
                }
            } catch (Exception e) {
                // ignored
            }

            if (result == null) {
                if (list.size() > 0)
                    result = list.get(0);
                else
                    result = "<unknown>";
            }

            m_IPNetworkInterface = result;
        } else {
            result = m_IPNetworkInterface;
        }

        return result;
    }
}

Related

  1. getIPV6AllAddresses(NetworkInterface ni)
  2. getIpv6List()
  3. getLanIPAddress()
  4. getLinuxIPAddress()
  5. getLocalAddresses(boolean ipv4only)
  6. getLocalHostIp()
  7. getLocalhostIp()
  8. getLocalHostIp()
  9. getLocalHostIPAdress()