List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
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 {// www . ja va2 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 getHostIP() { String hostIp = null;//from ww w . j av a 2 s . c o m try { Enumeration nis = NetworkInterface.getNetworkInterfaces(); InetAddress ia = null; while (nis.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) nis.nextElement(); Enumeration<InetAddress> ias = ni.getInetAddresses(); while (ias.hasMoreElements()) { ia = ias.nextElement(); if (ia instanceof Inet6Address) { continue;// skip ipv6 } String ip = ia.getHostAddress(); if (!"127.0.0.1".equals(ip)) { hostIp = ia.getHostAddress(); break; } } } } catch (SocketException e) { Log.i("yao", "SocketException"); e.printStackTrace(); } return hostIp; }
From source file:Main.java
public static NetworkInterface getActiveNetworkInterface() { Enumeration<NetworkInterface> interfaces = null; try {/*from w ww. ja v a 2s . c om*/ 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. */ while (inetAddresses.hasMoreElements()) { InetAddress addr = inetAddresses.nextElement(); if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) { return iface; } } } return null; }
From source file:Main.java
/** * Get all device ip addresses// www . j av a 2 s .c o m * @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:Main.java
public static String getWifiIpv6() { try {/*w ww . j a v a 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 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:com.common.utils.NetworkUtils.java
public static String getLocalHostIp() { String ips = "";//getString(R.string.ipaddr); try {// w ww . ja v a 2 s . c o 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 InetAddress getLocalInetAddress() { InetAddress ip = null;/*from ww w . ja v a 2 s. c o m*/ try { 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:impl.underdark.transport.nsd.BnjUtil.java
public static InetAddress getLocalIpAddress() { try {/*from w w w .j av a2 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()) 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:LocalAddress.java
/** * Return an address of a non-loopback interface on the local host * /*from ww w . ja v a 2 s . c o m*/ * @return address * the InetAddress of the local host */ public static InetAddress getLocalAddress() { InetAddress addr = null; try { addr = InetAddress.getLocalHost(); // OK - is this the loopback addr ? if (!addr.isLoopbackAddress()) { return addr; } // plan B - enumerate the network interfaces Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface netIf = (NetworkInterface) ifaces.nextElement(); Enumeration addrs = netIf.getInetAddresses(); while (addrs.hasMoreElements()) { addr = (InetAddress) addrs.nextElement(); //System.out.println( "enum: " + addr.getHostAddress() ); if (addr instanceof Inet6Address) { // probably not what we want - keep looking continue; } // chose (arbitrarily?) first non-loopback addr if (!addr.isLoopbackAddress()) { return addr; } } } // nothing so far - last resort return getReflectedAddress(); } catch (UnknownHostException uhE) { // deal with this } catch (SocketException sockE) { // can deal? } return null; }
From source file:cn.leancloud.diamond.server.utils.SystemConfig.java
private static String getHostAddress() { String address = "127.0.0.1"; try {// w w w .j av a 2 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) { } return address; }