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:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
        throws IOException {
    final String hostName = address.getHostName();
    final String resolvedHost = resolver.resolve(hostName);
    if (resolvedHost != null && !resolvedHost.equals(hostName)) {
        if (InetAddressUtils.isIPv6Address(resolvedHost)) {
            final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress();
            return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port, localAddress,
                    localPort);/*from  w w w .j a v  a2  s.  c om*/
        } else if (InetAddressUtils.isIPv4Address(resolvedHost)) {
            final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress();
            return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port, localAddress,
                    localPort);
        }
    }
    return defaultFactory.createSocket(address, port, localAddress, localPort);
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

private String readIP4AddressOfEmulator() throws SocketException {
    String inet4Address = "0.0.0.0";

    for (Enumeration<NetworkInterface> iter = NetworkInterface.getNetworkInterfaces(); iter
            .hasMoreElements();) {//from   w  w  w .j a  va2 s . c o m
        NetworkInterface nic = iter.nextElement();

        if (nic.getName().startsWith("eth")) {
            Enumeration<InetAddress> addresses = nic.getInetAddresses();
            addresses.nextElement(); // skip first
            if (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();

                String concreteAddressString = address.getHostAddress().toUpperCase(Locale.getDefault());
                if (InetAddressUtils.isIPv4Address(concreteAddressString)) {
                    inet4Address = concreteAddressString;
                }
            }
        }
    }
    return inet4Address;
}

From source file:com.DPFaragir.DPFUtils.java

public static String getIPAddress(boolean useIPv4) {
    try {//from w w  w  .j av  a 2 s  .c  o  m
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            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 (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:com.shreymalhotra.crazyflieserver.MainActivity.java

public String getLocalIpAddress() {
    try {//from w ww .  j av  a 2  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() &&
                // !inetAddress.isLinkLocalAddress() &&
                // inetAddress.isSiteLocalAddress() ) {
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    String ipAddr = inetAddress.getHostAddress();
                    return ipAddr;
                }
            }
        }
    } catch (SocketException ex) {
        Log.d("CrazyFlieServer", ex.toString());
    }
    return null;
}

From source file:org.kei.android.phone.cellhistory.towers.MobileNetworkInfo.java

/** Get IP For mobile */
public static String getMobileIP(final boolean useIPv4) {
    try {//from   w ww  .  j  av a  2 s .  com
        final List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (final NetworkInterface intf : interfaces) {
            final List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (final InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    final String sAddr = addr.getHostAddress().toUpperCase(Locale.US);
                    final boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            final int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (final Exception ex) {
        Log.e(MobileNetworkInfo.class.getSimpleName(), "Exception in Get IP Address: " + ex.getMessage(), ex);
    }
    return TowerInfo.UNKNOWN;
}

From source file:eu.musesproject.client.contextmonitoring.sensors.DeviceProtectionSensor.java

public String getIPAddress(boolean useIPv4) {
    try {//ww  w.j  av  a 2s.  c  o  m
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            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('%');
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:totalcross.android.ConnectionManager4A.java

public static String getLocalIpAddress() {
    String ipv4 = null;// ww w.  j a v  a  2  s .  c  om
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                // for getting IPV4 format
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress()))
                    return ipv4;
            }
        }
    } catch (Exception ex) {
        AndroidUtils.debug("IP Address" + ex.toString());
    }
    return null;
}

From source file:com.hg.development.apps.messagenotifier_v1.Utils.Utility.java

/**
 *
 * @return adresse IP v4 actuelle du tlphone.
 * @throws SocketException//w ww.j  a v a  2  s  . co m
 */
private String getLocalIpAddress() throws SocketException {
    String ipv4 = null;
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

        if (interfaces.size() > 0) {
            for (NetworkInterface ni : interfaces) {
                List<InetAddress> ialist = Collections.list(ni.getInetAddresses());

                if (ialist.size() > 0) {
                    for (InetAddress address : ialist) {
                        if (!address.isLoopbackAddress()
                                && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress()))
                            ;
                    }
                }

            }
        }

    } catch (SocketException ex) {
        throw ex;
    } finally {
        return ipv4;
    }
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * Get IP address from first non-localhost interface
 * /*from ww  w  . j  a v a2s. c o m*/
 * @param ipv4 true=return ipv4, false=return ipv6
 * @return address or empty string
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            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 (Exception e) {
        e.printStackTrace();
    }
    return "";
}