Java IP Address Get getIPFromNetworkInterface()

Here you can find the source of getIPFromNetworkInterface()

Description

Returns the IP address determined from the network interfaces (using the IP address of the one with a proper host name).

License

Open Source License

Return

the IP address

Declaration

public static synchronized String getIPFromNetworkInterface() 

Method Source Code

//package com.java2s;
/*/*from   w  w  w. j ava 2s .c o  m*/
 *   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;

    /**
     * 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. getIPByInterfaceName(String name)
  2. getIpByName(String name)
  3. getIpByNetworkInterfaceName(String name)
  4. getIPFromHash(final long ipHash)
  5. getIPFromInterface(String ni)
  6. getIpHostnameLocal()
  7. getIPList(String address)
  8. getIPList(String s, String s1)
  9. getIPs()