List of usage examples for java.net InetAddress isLoopbackAddress
public boolean isLoopbackAddress()
From source file:Main.java
/** * Get all device ip addresses//from w w w . j ava 2 s. c om * @return {@link array} */ public static String[] getLocalIpAddress() { ArrayList<String> addresses = new ArrayList<String>(); 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()) { //IPAddresses.setText(inetAddress.getHostAddress().toString()); boolean isIPv4 = isIPv4Address(inetAddress.getHostAddress().toString()); if (isIPv4) { addresses.add(inetAddress.getHostAddress().toString()); } } } } } catch (SocketException ex) { String LOG_TAG = null; Log.e(LOG_TAG, ex.toString()); } return addresses.toArray(new String[0]); }
From source file:com.common.utils.NetworkUtils.java
public static String getLocalHostIp() { String ips = "";//getString(R.string.ipaddr); try {/*from w ww . j a v a 2s. 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: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 {/*ww w .ja va 2s . c o m*/ 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 getWifiIpv6() { try {// w w w.j av a 2s . 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() && inetAddress instanceof Inet6Address) { String[] ipv6 = inetAddress.getHostAddress().split("%"); String port = ipv6[1]; if (port.matches("wlan\\d+")) { return ipv6[0]; } } } } } catch (SocketException ex) { /*CONSUME*/ } return null; }
From source file:impl.underdark.transport.nsd.BnjUtil.java
public static InetAddress getLocalIpAddress() { try {/*from w w w. j a v 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()) continue; if (!InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) continue; //if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress() ) { //if (!inetAddress.isLoopbackAddress() // && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()) ) return inetAddress; } // for } // for } catch (SocketException ex) { Logger.error("nsd server failed to get local address"); } return null; }
From source file:Main.java
public static String getGPRSIP() { String ip = null;/*from www . ja v a 2 s .c o m*/ try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { for (Enumeration<InetAddress> enumIpAddr = en.nextElement().getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { ip = inetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); ip = null; } return ip; }
From source file:cn.leancloud.diamond.server.utils.SystemConfig.java
private static String getHostAddress() { String address = "127.0.0.1"; try {//from w w w .java2s. 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) { } return address; }
From source file:Main.java
public static NetworkInterface getActiveNetworkInterface() { Enumeration<NetworkInterface> interfaces = null; try {//w w w. ja va2 s . co m interfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return null; } while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); Enumeration<InetAddress> inetAddresses = iface.getInetAddresses(); /* Check if we have a non-local address. If so, this is the active * interface. * * This isn't a perfect heuristic: I have devices which this will * still detect the wrong interface on, but it will handle the * common cases of wifi-only and Ethernet-only. */ if (iface.getName().startsWith("w")) { //this is a perfect hack for getting wifi alone while (inetAddresses.hasMoreElements()) { InetAddress addr = inetAddresses.nextElement(); if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) { Log.d("LSSDP", "DisplayName" + iface.getDisplayName() + " Name " + iface.getName()); return iface; } } } } return null; }
From source file:Main.java
public static String getIpWithoutWifi() { try {/*from ww w.j a va 2 s .co m*/ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumeration = intf.getInetAddresses(); en.hasMoreElements();) { InetAddress inetAddress = enumeration.nextElement(); if (inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { Log.w(TAG, "getIpWithoutWifi error:" + e.getMessage()); } return null; }
From source file:Main.java
/** get first valid ipv4 address of this device */ public static String getLocalIpAddress() { try {//from w ww. ja va 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() && validIP(inetAddress.getHostAddress())) { return inetAddress.getHostAddress(); } } } } catch (Exception e) { Log.e(TAG, e.toString()); } return null; }