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:Main.java

/**
 * Get the local IP address.//from  w ww. j a va 2s.  co m
 * 
 * @return string containing the current local IP address
 */
public static String getLocalIpAddress() {
    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()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        android.util.Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

@Nullable
public static String getLocalIPAddress() {
    try {//w  ww  .j  a  v  a2  s .c  o  m
        for (Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces(); eni
                .hasMoreElements();) {
            NetworkInterface ni = eni.nextElement();
            for (Enumeration<InetAddress> eia = ni.getInetAddresses(); eia.hasMoreElements();) {
                InetAddress ia = eia.nextElement();
                if (!ia.isLoopbackAddress() && (ia instanceof Inet4Address)) {
                    return ia.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getIpAddress() {
    try {/*ww  w  .ja va  2  s .  com*/
        for (Enumeration<?> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = (NetworkInterface) en.nextElement();
            for (Enumeration<?> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {

                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("system", ex.toString());
    }
    return null;
}

From source file:Main.java

public static String localIPAddress() {
    try {/*ww w  .  ja va  2s  .  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();
                if (!inetAddress.isLoopbackAddress()) {
                    String ipAddress = inetAddress.getHostAddress().toString();
                    int p = ipAddress.indexOf("%");
                    if (p > 0)
                        ipAddress = ipAddress.substring(0, p);
                    //if (MagicBooleans.trace_mode) System.out.println("--> localIPAddress = "+ipAddress);
                    return ipAddress;
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return "127.0.0.1";
}

From source file:Main.java

/** get first valid ipv4 address of this device */
public static String getLocalIpAddress() {
    try {/*from  w  w w .  ja  va2 s.c  o  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();
                if (!inetAddress.isLoopbackAddress() && validIP(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, e.toString());
    }
    return null;

}

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

public static String getLocalHostIp() {
    String ips = "";//getString(R.string.ipaddr);
    try {/*from  w  w  w  .  jav  a  2 s .  co  m*/
        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.seadee.library.receiver.NetworkStateReceiver.java

public static String getLocalIpAddress(Context context) {
    final String IPTAG = "getLocalIpAddress";
    String nullAddress = "0.0.0.0";
    try {//from   ww  w.  j a  va  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();
                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 getIpAddress() {
    try {//  w  ww . j  a  v a  2 s  .co  m
        Enumeration en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {
            NetworkInterface ex = (NetworkInterface) en.nextElement();
            Enumeration enumIpAddr = ex.getInetAddresses();

            while (enumIpAddr.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }

        return null;
    } catch (SocketException var4) {
        var4.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getDomainAddress(final String domain) {
    try {/*from   ww w  .ja va 2 s . c om*/
        ExecutorService exec = Executors.newCachedThreadPool();
        Future<String> fs = exec.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                InetAddress inetAddress;
                try {
                    inetAddress = InetAddress.getByName(domain);
                    return inetAddress.getHostAddress();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
        return fs.get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    try {//ww w.j av a  2 s . co m
        String ipv4;

        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            if (ni.getName().toLowerCase().contains("usbnet"))
                continue;
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }

        }

    } catch (SocketException ex) {
        Log.d("socket_err", ex.toString());
    }
    return null;
}