Java Code Examples for java.net.InetAddress#isLoopbackAddress()
The following examples show how to use
java.net.InetAddress#isLoopbackAddress() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetUtil.java From NetworkMapper with GNU General Public License v2.0 | 6 votes |
static public String getIPs() { String interfaces=""; 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()) { interfaces=interfaces+"[IP]: "+ inetAddress.getHostAddress() +"\n"; } } } } catch (SocketException ignored) { } return interfaces; }
Example 2
Source File: VenvyDeviceUtil.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 6 votes |
/** * 获取IP地址 */ 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() && (inetAddress instanceof Inet4Address)) { return inetAddress.getHostAddress(); } } } } catch (Exception ex) { VenvyLog.e(TAG, ex); } return null; }
Example 3
Source File: Util.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Returns a list of all the addresses on the system. * @param inclLoopback * if {@code true}, include the loopback addresses * @param ipv4Only * it {@code true}, only IPv4 addresses will be included */ static List<InetAddress> getAddresses(boolean inclLoopback, boolean ipv4Only) throws SocketException { ArrayList<InetAddress> list = new ArrayList<InetAddress>(); Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netInf : Collections.list(nets)) { Enumeration<InetAddress> addrs = netInf.getInetAddresses(); for (InetAddress addr : Collections.list(addrs)) { if (!list.contains(addr) && (inclLoopback ? true : !addr.isLoopbackAddress()) && (ipv4Only ? (addr instanceof Inet4Address) : true)) { list.add(addr); } } } return list; }
Example 4
Source File: Bytes.java From jsongood with Apache License 2.0 | 6 votes |
private static InetAddress localAddress() { try { Enumeration<NetworkInterface> itfs = NetworkInterface.getNetworkInterfaces(); if (itfs != null) { while (itfs.hasMoreElements()) { NetworkInterface itf = itfs.nextElement(); for (Enumeration<InetAddress> ads = itf.getInetAddresses(); ads.hasMoreElements();) { InetAddress ad = ads.nextElement(); if (ad == null || ad.isLoopbackAddress()) continue; String ip = ad.getHostAddress(); if ("0.0.0.0".equals(ip) || "127.0.0.1".equals(ip)) continue; return ad; } } } } catch (Throwable e) { } return null; }
Example 5
Source File: IPTools.java From HttpInfo with Apache License 2.0 | 6 votes |
public static ArrayList<InetAddress> getLocalIPv4Addresses() { ArrayList<InetAddress> foundAddresses = new ArrayList<>(); Enumeration<NetworkInterface> ifaces; try { ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { foundAddresses.add(addr); } } } } catch (SocketException e) { e.printStackTrace(); } return foundAddresses; }
Example 6
Source File: MacUtils.java From base-module with Apache License 2.0 | 6 votes |
private static InetAddress getInetAddress() { try { Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); // To prevent phone of xiaomi return "10.0.2.15" if (!ni.isUp()) continue; Enumeration<InetAddress> addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress inetAddress = addresses.nextElement(); if (!inetAddress.isLoopbackAddress()) { String hostAddress = inetAddress.getHostAddress(); if (hostAddress.indexOf(':') < 0) return inetAddress; } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
Example 7
Source File: IpUtil.java From AndroidUtils with The Unlicense | 6 votes |
public static InetAddress getIpAddress() { try { for (Enumeration<NetworkInterface> enNetI = NetworkInterface .getNetworkInterfaces(); enNetI.hasMoreElements(); ) { NetworkInterface netI = enNetI.nextElement(); for (Enumeration<InetAddress> enumIpAddr = netI .getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) { return inetAddress; } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
Example 8
Source File: UpnpUtil.java From TVRemoteIME with GNU General Public License v2.0 | 6 votes |
public static String getIP() throws SocketException { String ipaddress = ""; for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().toLowerCase().equals("eth0") || intf.getName().toLowerCase().equals("wlan0")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { ipaddress = inetAddress.getHostAddress().toString(); if (!ipaddress.contains("::")) {// ipV6的地址 Log.e(TAG, ipaddress); return ipaddress; } } } } else { continue; } } return ipaddress; }
Example 9
Source File: PayUtils.java From PayAndroid with Apache License 2.0 | 6 votes |
/** * 获取客户端ip * @return * 成功 ip * 失败 null */ public static String getIpAddress() { 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() && inetAddress instanceof Inet4Address) { // if (!inetAddress.isLoopbackAddress() && inetAddress // instanceof Inet6Address) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { e.printStackTrace(); } return null; }
Example 10
Source File: NetworkUtils.java From submarine with Apache License 2.0 | 6 votes |
public static String findAvailableHostAddress() throws UnknownHostException, SocketException { String submarineServerIP = System.getenv("SUBMARINE_LOCAL_IP"); if (submarineServerIP != null) { return submarineServerIP; } InetAddress address = InetAddress.getLocalHost(); if (address.isLoopbackAddress()) { for (NetworkInterface networkInterface : Collections .list(NetworkInterface.getNetworkInterfaces())) { if (!networkInterface.isLoopback()) { for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress a = interfaceAddress.getAddress(); if (a instanceof Inet4Address) { return a.getHostAddress(); } } } } } return address.getHostAddress(); }
Example 11
Source File: MacAddressUtil.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
private static int scoreAddress(InetAddress addr) { if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) { return 0; } if (addr.isMulticastAddress()) { return 1; } if (addr.isLinkLocalAddress()) { return 2; } if (addr.isSiteLocalAddress()) { return 3; } return 4; }
Example 12
Source File: XNetworkUtils.java From XFrame with Apache License 2.0 | 5 votes |
/** * 获取IP地址 * <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p> * * @param useIPv4 是否用IPv4 * @return IP地址 */ public static String getIPAddress(boolean useIPv4) { try { for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) { NetworkInterface ni = nis.nextElement(); // 防止小米手机返回10.0.2.15 if (!ni.isUp()) continue; for (Enumeration<InetAddress> addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) { InetAddress inetAddress = addresses.nextElement(); if (!inetAddress.isLoopbackAddress()) { String hostAddress = inetAddress.getHostAddress(); boolean isIPv4 = hostAddress.indexOf(':') < 0; if (useIPv4) { if (isIPv4) return hostAddress; } else { if (!isIPv4) { int index = hostAddress.indexOf('%'); return index < 0 ? hostAddress.toUpperCase() : hostAddress.substring(0, index).toUpperCase(); } } } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
Example 13
Source File: AbstractEntityModel.java From OpERP with MIT License | 5 votes |
public String getHostAddress() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface .getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface network = networkInterfaces.nextElement(); if (!network.isUp() || network.isLoopback() || network.isVirtual()) continue; Enumeration<InetAddress> addresses = network.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress netAddr = addresses.nextElement(); if (!netAddr.isLoopbackAddress() && (netAddr instanceof Inet4Address)) { return netAddr.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return "localhost"; }
Example 14
Source File: oConvertUtils.java From jeecg-boot-with-activiti with MIT License | 5 votes |
/** * @return 本机IP * @throws SocketException */ public static String getRealIp() throws SocketException { String localip = null;// 本地IP,如果没有配置外网IP则返回它 String netip = null;// 外网IP Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// 是否找到外网IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 外网IP netip = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP localip = ip.getHostAddress(); } } } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } }
Example 15
Source File: IOUtils.java From openmessaging-storage-dledger with Apache License 2.0 | 5 votes |
public static String getLocalhostByNetworkInterface() throws SocketException { List<String> candidatesHost = new ArrayList<String>(); Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); // Workaround for docker0 bridge if ("docker0".equals(networkInterface.getName()) || !networkInterface.isUp()) { continue; } Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress address = addrs.nextElement(); if (address.isLoopbackAddress()) { continue; } //ip4 highter priority if (address instanceof Inet6Address) { candidatesHost.add(address.getHostAddress()); continue; } return address.getHostAddress(); } } if (!candidatesHost.isEmpty()) { return candidatesHost.get(0); } return null; }
Example 16
Source File: MiniMRYarnCluster.java From big-c with Apache License 2.0 | 5 votes |
public static String getResolvedMRHistoryWebAppURLWithoutScheme( Configuration conf, boolean isSSLEnabled) { InetSocketAddress address = null; if (isSSLEnabled) { address = conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_PORT); } else { address = conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_PORT); } address = NetUtils.getConnectAddress(address); StringBuffer sb = new StringBuffer(); InetAddress resolved = address.getAddress(); if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) { String lh = address.getHostName(); try { lh = InetAddress.getLocalHost().getCanonicalHostName(); } catch (UnknownHostException e) { //Ignore and fallback. } sb.append(lh); } else { sb.append(address.getHostName()); } sb.append(":").append(address.getPort()); return sb.toString(); }
Example 17
Source File: AddressUtils.java From TorrentEngine with GNU General Public License v3.0 | 4 votes |
/** * checks if the provided address is a global-scope ipv6 unicast address */ public static boolean isGlobalAddressV6(InetAddress addr) { return addr instanceof Inet6Address && !addr.isAnyLocalAddress() && !addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && !addr.isMulticastAddress() && !addr.isSiteLocalAddress() && !((Inet6Address)addr).isIPv4CompatibleAddress(); }
Example 18
Source File: Result.java From BiglyBT with GNU General Public License v2.0 | 4 votes |
public static String adjustLink( String link ) { if ( link == null || link.length() < 5 ){ return( link ); } char c = link.charAt(0); if ( c == 'h' || c == 'H' || c == 'f' || c == 'F' ){ if ( MetaSearchManagerFactory.getSingleton().getProxyRequestsEnabled()){ try{ String host = new URL( link ).getHost(); if ( AENetworkClassifier.categoriseAddress( host ) != AENetworkClassifier.AT_PUBLIC ){ return( link ); } InetAddress ia = HostNameToIPResolver.hostAddressToInetAddress( host ); if ( ia != null ){ if ( ia.isLoopbackAddress() || ia.isLinkLocalAddress() || ia.isSiteLocalAddress()){ return( link ); } } }catch( Throwable e ){ } return( "tor:" + link ); } } return( link ); }
Example 19
Source File: LimitedLocalNodeFirstLocalBalancingPolicy.java From stratio-cassandra with Apache License 2.0 | 4 votes |
private static boolean isLocalHost(Host host) { InetAddress hostAddress = host.getAddress(); return hostAddress.isLoopbackAddress() || localAddresses.contains(hostAddress); }
Example 20
Source File: JMXInterfaceBindingTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
private static boolean isNonloopbackLocalhost(InetAddress i) { if (!i.isLoopbackAddress()) { return i.getHostName().toLowerCase().equals("localhost"); } return false; }