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

public static String getLocalIpAddress() throws SocketException {
    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();
            }/*from www  .  j  a v  a 2s  . c  o m*/
        }
    }
    return null;
}

From source file:SystemUtils.java

public static final String getRawAddress(InetSocketAddress inetSocketAddress) {
    InetAddress address = inetSocketAddress.getAddress();
    return address != null ? address.getHostAddress() : inetSocketAddress.getHostName();
}

From source file:Main.java

public static String getHostIp() {
    try {/*from  w  ww . ja v a 2 s . com*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements();) {
                InetAddress inetAddress = ipAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:net.itransformers.idiscover.discoveryhelpers.xml.SnmpGetNameForXslt.java

public static String getIPbyName(String hostname) {
    try {//w  ww. ja v  a  2s.c o m
        java.net.InetAddress inetAdd = java.net.InetAddress.getByName(hostname);
        return inetAdd.getHostAddress();
    } catch (java.net.UnknownHostException uhe) {
        //handle exception
        return "";
    }

}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from  w w  w. j  ava2  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();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        System.out.println("WifiPreference IpAddress" + ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*from   w  w w .  j a va  2 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();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from  w  w  w.j ava2s  . 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()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        throw new RuntimeException(ex);
    }

    return null;
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null && wifiInfo.getIpAddress() != 0) {
        return android.text.format.Formatter.formatIpAddress(wifiInfo.getIpAddress());
    } else {//w  ww . j ava 2  s  . c om
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                        String ipAddress = inetAddress.getHostAddress();
                        if (!TextUtils.isEmpty(ipAddress) && !ipAddress.contains(":")) {
                            return ipAddress;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from  w w w.  jav a2s.com
        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().split("[%]")[0];
                }
            }
        }
    } catch (SocketException ex) {
    }
    return null;
}

From source file:Main.java

public static String getLocalIP() {
    try {//from  w  w w  . jav a2 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()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("bevelop", null, ex);
    }
    return "";
}