Example usage for java.net InetAddress getHostAddress

List of usage examples for java.net InetAddress getHostAddress

Introduction

In this page you can find the example usage for java.net InetAddress getHostAddress.

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation.

Usage

From source file:com.adamkruger.myipaddressinfo.NetworkInfoFragment.java

private static String InetAddressToString(InetAddress address) {
    String addressString = address.getHostAddress().toUpperCase(Locale.getDefault());
    if (InetAddressUtils.isIPv4Address(addressString)) {
        return addressString;
    } else {//from w  w w. ja  v  a 2s .  com
        int suffixPosition = addressString.indexOf('%');
        return suffixPosition < 0 ? addressString : addressString.substring(0, suffixPosition);
    }
}

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

/** Get IP For mobile */
public static String getMobileIP(final boolean useIPv4) {
    try {/*  w w  w  . ja v  a 2 s .  c o  m*/
        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:ca.frozen.curlingtv.classes.Utils.java

public static String getLocalIpAddress() {
    try {/*from  w  w  w.ja v a 2s  .  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 instanceof Inet4Address) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.starit.diamond.client.processor.LocalConfigInfoProcessor.java

static String getHostAddress() {
    String address = "127.0.0.1";
    try {/* ww w.  j ava2 s . com*/
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface ni = en.nextElement();
            Enumeration<InetAddress> ads = ni.getInetAddresses();
            while (ads.hasMoreElements()) {
                InetAddress ip = ads.nextElement();
                if (!ip.isLoopbackAddress() && ip.isSiteLocalAddress()) {
                    return ip.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("??", e);
    }
    return address;
}

From source file:com.cosmicpush.pub.push.LqNotificationPush.java

public static LqNotificationPush newPush(String topic, String summary, String trackingId, Throwable throwable,
        Collection<LqAttachment> attachments, String callbackUrl, Map<String, String> traits) {

    InetAddress remoteAddress = PushUtils.getLocalHost();
    LqExceptionInfo exceptionInfo = (throwable == null) ? null : LqExceptionInfo.create(throwable);

    return new LqNotificationPush(topic, summary, trackingId, ZonedDateTime.now(), exceptionInfo, attachments,
            callbackUrl, remoteAddress.getCanonicalHostName(), remoteAddress.getHostAddress(), traits);
}

From source file:com.alibaba.rocketmq.common.MixAll.java

private static String localhost() {
    try {//from  w ww  .  j av  a 2 s  . com
        InetAddress addr = InetAddress.getLocalHost();
        return addr.getHostAddress();
    } catch (UnknownHostException e) {
        throw new RuntimeException("get localhost fail", e);
    }
}

From source file:Main.java

public static InetAddress getLocalInetAddress() {
    InetAddress ip = null;
    try {/*from   www . j ava 2  s  . c  o m*/
        Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
        while (en_netInterface.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();
            Enumeration<InetAddress> en_ip = ni.getInetAddresses();
            while (en_ip.hasMoreElements()) {
                ip = en_ip.nextElement();
                if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                    break;
                else
                    ip = null;
            }
            if (ip != null) {
                break;
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return ip;
}

From source file:net.grinder.util.NetworkUtil.java

/**
 * Get the local host address, try to get actual IP.
 * /*  ww w  . j  a  v a2s .  c om*/
 * @return ip form of host address
 */
public static String getLocalHostAddress() {
    InetAddress localHost = null;
    try {
        localHost = InetAddress.getLocalHost();
    } catch (Exception e) {
        LOGGER.error("Error while get localhost address", e);
    }
    if (localHost != null && !localHost.isLoopbackAddress()) {
        return localHost.getHostAddress();
    }
    return getLocalHostAddress("www.google.com", 80);
}

From source file:es.auth.plugin.AbstractUnitTest.java

public static String getNonLocalhostAddress() {
    try {//  www .j a  v  a  2 s . co  m
        for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            final NetworkInterface intf = en.nextElement();
            if (intf.isLoopback() || !intf.isUp()) {
                continue;
            }
            for (final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                    .hasMoreElements();) {
                final InetAddress ia = enumIpAddr.nextElement();
                if (ia.isLoopbackAddress() || ia instanceof Inet6Address) {
                    continue;
                }
                return ia.getHostAddress();
            }
        }
    } catch (final SocketException e) {
        throw new RuntimeException(e);
    }
    System.out.println("ERROR: No non-localhost address available, will use localhost");
    return "localhost";
}

From source file:com.cellbots.logger.localServer.LocalHttpServer.java

/**
 * Returns the IP addresses of this device.
 * //  w w w  . j a va2s  .c o m
 * @return IP addresses as a String. Addresses are separated by newline.
 */
public static String getLocalIpAddresses() {
    String ipAddresses = "";
    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();
                if (!inetAddress.isLoopbackAddress()) {
                    String ipAddress = inetAddress.getHostAddress().toString();
                    if (ipAddress.split("\\.").length == 4) {
                        ipAddresses = ipAddresses + ipAddress + ":8080\n";
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("", ex.toString());
    }
    return ipAddresses;
}