Java IP Address Get getIPAddressFromNetworkInterfaces()

Here you can find the source of getIPAddressFromNetworkInterfaces()

Description

get IP Address From Network Interfaces

License

Open Source License

Declaration

public static String getIPAddressFromNetworkInterfaces() 

Method Source Code

//package com.java2s;
/*/*from  w ww.j  a  v  a 2 s . c om*/
 * Biocep: R-based Platform for Computational e-Science.
 *  
 * Copyright (C) 2007-2009 Karim Chine - karim.chine@m4x.org
 *  
 * Copyright (C) 2007 EMBL-EBI-Microarray Informatics
 *  
 * 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.Enumeration;

import java.util.Vector;

public class Main {
    public static String getIPAddressFromNetworkInterfaces() {
        Vector<String> IPs = new Vector<String>();
        try {
            NetworkInterface iface = null;
            for (Enumeration<?> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
                iface = (NetworkInterface) ifaces.nextElement();
                InetAddress ia = null;
                for (Enumeration<?> ips = iface.getInetAddresses(); ips.hasMoreElements();) {
                    ia = (InetAddress) ips.nextElement();
                    boolean matches = isValidIPAddress(ia.getHostAddress()) && !isLoopBackIP(ia.getHostAddress());
                    if (matches) {
                        IPs.add(ia.getHostAddress());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (IPs.size() == 1)
            return IPs.elementAt(0);
        return null;
    }

    public static boolean isValidIPAddress(String ipAddress) {
        String[] parts = ipAddress.split("\\.");
        if (parts.length != 4) {
            return false;
        }
        for (String s : parts) {
            int i = Integer.parseInt(s);
            if ((i < 0) || (i > 255)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isLoopBackIP(String ipAddress) {
        return ipAddress.startsWith("127.");
    }
}

Related

  1. getIpAddresses()
  2. getIpAddressesFromNic()
  3. getIpAddressExternal()
  4. getIPAddressFromBytes(byte[] bytes)
  5. getIPAddressFromNetworkInterface(String ifaceName)
  6. getIPAdresses()
  7. getIpByHost(String hostName)
  8. getIpByHost(String hostName)
  9. getIpByHost(String hostName)