List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:Main.java
public static String getIpAddress() { try {/*from w ww . j ava 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 instanceof Inet4Address) { // if (!inetAddress.isLoopbackAddress() && inetAddress // instanceof Inet6Address) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String getLocalIpBy3G() { try {/*from w w w .ja v a 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() && inetAddress instanceof Inet4Address) { // if (!inetAddress.isLoopbackAddress() && inetAddress // instanceof Inet6Address) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Get IP address from first non-localhost interface * @param ipv4 true=return ipv4, false=return ipv6 * @return address or empty string//w w w. j a va 2s.c o m */ @SuppressLint("DefaultLocale") public static String getIPAddress() { 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 (isIPv4) return sAddr; } } } } catch (Exception ex) { } return ""; }
From source file:Main.java
public static String localIPAddress() { try {//from w w w . j a v a 2 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()) { 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
public static String getIpAddress() { try {//from w w w. j a v a 2 s . c om 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
/** * Get the local IP address.//from w w w . j a va2 s . com * * @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
public static NetworkInterface getActiveNetworkInterface(Context c) { try {/*from w w w . j a v a2s . 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()) break; // break inner loop, continue with outer loop return intf; // this is not the loopback and it has an IP address assigned } } } catch (SocketException e) { e.printStackTrace(); } // nothing found return null; }
From source file:Main.java
public static String getIpAddress() { String ipaddress = ""; try {/*from w w w.jav a 2 s .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()) { ipaddress = ipaddress + ";" + inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { ipaddress = null; Log.e("WifiPreference IpAddress", ex.toString()); } return ipaddress; }
From source file:Main.java
public static String getIpAddress() { try {/*from w ww . j a va2s . 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 getGPRS_IP() { try {/*from w w w . j ava 2 s . c om*/ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enu = intf.getInetAddresses(); enu.hasMoreElements();) { InetAddress inetAddress = enu.nextElement(); if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) { if (inetAddress.getHostAddress().toString().contains("::")) { continue; } return inetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return null; }