Java Code Examples for java.net.InetAddress#isLoopbackAddress()

The following examples show how to use java.net.InetAddress#isLoopbackAddress() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: netty-4.1.22   File: MacAddressUtil.java    License: Apache License 2.0 6 votes vote down vote up
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 2
Source Project: submarine   File: NetworkUtils.java    License: Apache License 2.0 6 votes vote down vote up
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 3
Source Project: PayAndroid   File: PayUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取客户端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 4
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 5
Source Project: AndroidUtils   File: IpUtil.java    License: The Unlicense 6 votes vote down vote up
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 6
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 7
Source Project: base-module   File: MacUtils.java    License: Apache License 2.0 6 votes vote down vote up
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 8
Source Project: HttpInfo   File: IPTools.java    License: Apache License 2.0 6 votes vote down vote up
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 9
Source Project: jsongood   File: Bytes.java    License: Apache License 2.0 6 votes vote down vote up
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 10
Source Project: jdk8u60   File: Util.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 11
/**
 * 获取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 12
Source Project: XFrame   File: XNetworkUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取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 Project: OpERP   File: AbstractEntityModel.java    License: MIT License 5 votes vote down vote up
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 Project: jeecg-boot-with-activiti   File: oConvertUtils.java    License: MIT License 5 votes vote down vote up
/**
 * @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
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 Project: big-c   File: MiniMRYarnCluster.java    License: Apache License 2.0 5 votes vote down vote up
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
/**
 * 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
private static boolean isNonloopbackLocalhost(InetAddress i) {
    if (!i.isLoopbackAddress()) {
        return i.getHostName().toLowerCase().equals("localhost");
    }
    return false;
}
 
Example 19
Source Project: BiglyBT   File: Result.java    License: GNU General Public License v2.0 4 votes vote down vote up
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 20
private static boolean isLocalHost(Host host)
{
    InetAddress hostAddress = host.getAddress();
    return hostAddress.isLoopbackAddress() || localAddresses.contains(hostAddress);
}