Example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address

List of usage examples for org.apache.http.conn.util InetAddressUtils isIPv4Address

Introduction

In this page you can find the example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address.

Prototype

public static boolean isIPv4Address(final String input) 

Source Link

Document

Checks whether the parameter is a valid IPv4 address

Usage

From source file:impl.underdark.transport.nsd.BnjUtil.java

public static InetAddress getLocalIpAddress() {
    try {//w  ww.ja  v a2 s  .  c  om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();

                if (inetAddress.isLoopbackAddress())
                    continue;

                if (!InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))
                    continue;

                //if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress() ) {
                //if (!inetAddress.isLoopbackAddress()
                //      && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()) )

                return inetAddress;
            } // for
        } // for
    } catch (SocketException ex) {
        Logger.error("nsd server failed to get local address");
    }
    return null;
}

From source file:org.nebula.service.core.HostAddress.java

public static String getLocalHost() throws Exception {

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface current = interfaces.nextElement();

        if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
            continue;
        }/*  www  . j a v  a  2s.c om*/
        Enumeration<InetAddress> addresses = current.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress current_addr = addresses.nextElement();
            if (current_addr.isLoopbackAddress()
                    || !InetAddressUtils.isIPv4Address(current_addr.getHostAddress())) {
                continue;
            }
            return current_addr.getHostName();
        }
    }

    throw new Exception("Failed to get local hostname");
}

From source file:com.common.utils.NetworkUtils.java

public static String getLocalHostIp() {
    String ips = "";//getString(R.string.ipaddr);
    try {// w  w  w .  j  a v a  2  s  .com
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {
            NetworkInterface nif = en.nextElement();
            Enumeration<InetAddress> inet = nif.getInetAddresses();

            while (inet.hasMoreElements()) {
                InetAddress ip = inet.nextElement();
                if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip.getHostAddress())) {

                    ips = ips + nif.getName() + ":" + ip.getHostAddress() + "  ";

                }
            }

        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return ips;

}

From source file:com.github.christiangda.utils.ip.IP.java

/**
 * @param ip String//from   www .  j  av  a2 s  . co m
 * @return boolean
 */
public static boolean isValidIPV4(String ip) {
    if (ip == null) {
        return false;
    }

    if (ip.isEmpty()) {
        return false;
    }

    return InetAddressUtils.isIPv4Address(ip);
}

From source file:com.seadee.library.receiver.NetworkStateReceiver.java

public static String getLocalIpAddress(Context context) {
    final String IPTAG = "getLocalIpAddress";
    String nullAddress = "0.0.0.0";
    try {/*from  www . jav a2 s . co m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                String ip_address = inetAddress.getHostAddress();
                if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address))
                    return ip_address;
            }
        }
    } catch (SocketException ex) {
        Log.e(IPTAG, ex.toString());
    }
    return nullAddress;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//  w  w  w . jav a2s  . c o  m
        String ipv4;
        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }
        }
    } catch (SocketException ex) {
    }

    return null;
}

From source file:com.github.christiangda.utils.ip.IP.java

/**
 * @param ip InetAddress//from   w  ww  .j a va  2  s  .  c  o m
 * @return boolean
 */
public static boolean isValidIPV4(InetAddress ip) {
    if (ip == null) {
        return false;
    }

    return InetAddressUtils.isIPv4Address(ip.getHostAddress());
}

From source file:com.seadee.degree.service.NetworkStateReceiver.java

public static String getLocalIpAddress(Context context) {
    final String IPTAG = "getLocalIpAddress";
    String nullAddress = "0.0.0.0";
    try {//from   w w  w  .j  ava2s  .  c  om
        if (SettingVarible.networkstate != SettingVarible.NETWORKSTATE.NONETWORK) {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    String ip_address = inetAddress.getHostAddress();
                    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address))
                        return ip_address;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(IPTAG, ex.toString());
    }
    return nullAddress;
}

From source file:eu.codebits.plasmas.util.NetworkInterfaces.java

/**
 * @param interfaceName eth0, wlan0 or NULL=use first interface 
 * @param useIPv4/*from   ww  w .j a v  a 2  s . c o m*/
 * @return address or empty string
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress(String interfaceName, boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (SocketException ex) {
    }
    return "";
}

From source file:com.marcosdiez.server.php.service.Network.java

public Network() {
    names = new LinkedList<String>();
    titles = new LinkedList<String>();
    adresses = new LinkedList<String>();
    try {/*from www  . j  av  a  2 s  .  c  o  m*/
        List<NetworkInterface> list = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface iface : list) {
            List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
            for (InetAddress address : addresses) {
                if (!address.isLoopbackAddress()) {
                    String host = address.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(host)) {
                        names.add(iface.getName());
                        titles.add(host + "(" + iface.getDisplayName() + ")");
                        adresses.add(host);
                    }
                }
            }
        }
        for (NetworkInterface iface : list) {
            List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
            for (InetAddress address : addresses) {
                if (address.isLoopbackAddress()) {
                    String host = address.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(host)) {
                        names.add(iface.getName());
                        titles.add(host + "(" + iface.getDisplayName() + ")");
                        adresses.add(host);
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    names.add("all");
    adresses.add("0.0.0.0");
    titles.add("0.0.0.0 (all)");
}