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

The following are Jave code examples for showing how to use isLoopbackAddress() of the java.net.InetAddress class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: ViewDebugHelper   File: NetworkUtil.java   Source Code and License Vote up 6 votes
/** 获得IP信息 */
public static String getOneIPV4() {
	ArrayList<String> list = new ArrayList<String>();
	String result = "ip get failed";
	try {
		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 instanceof Inet4Address) {
					if (!inetAddress.isLoopbackAddress()) {
						list.add(inetAddress.getHostAddress());
					}
				}
			}
		}
	} catch (SocketException ex) {
	}
	if (list.size() > 0)
		result = list.get(0);
	return result;
}
 
Example 2
Project: CXJPadProject   File: NormalUtil.java   Source Code and License Vote up 6 votes
public static String getLocalIpAddressV4() {
        String ip = "";
        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() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))  //这里做了一步IPv4的判定
                    if (!inetAddress.isLoopbackAddress()
                            && inetAddress instanceof Inet4Address) {
                        ip = inetAddress.getHostAddress().toString();
                        return ip;
                    }
                }
            }
        } catch (SocketException e) {
//            Log.i("SocketException--->", ""+e.getLocalizedMessage());
            return "ip is error";
        }
        return ip;
    }
 
Example 3
Project: amap   File: MIP_NetworkUtils.java   Source Code and License Vote up 6 votes
/**
 * 获取GPRS连接的情况的IP
 * 
 * @return
 */
private static String getGPRSIpAddress()
{
    
    String ip = "";
    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.getHostAddress() != null)
                {
                    ip = inetAddress.getHostAddress().toString();
                }
            }
        }
    }
    catch (SocketException ex)
    {
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return ip == null ? "" : ip;
}
 
Example 4
Project: lams   File: ServletRequestContext.java   Source Code and License Vote up 6 votes
public boolean displayStackTraces() {
    ServletStackTraces mode = deployment.getDeploymentInfo().getServletStackTraces();
    if (mode == ServletStackTraces.NONE) {
        return false;
    } else if (mode == ServletStackTraces.ALL) {
        return true;
    } else {
        InetSocketAddress localAddress = getExchange().getSourceAddress();
        if(localAddress == null) {
            return false;
        }
        InetAddress address = localAddress.getAddress();
        if(address == null) {
            return false;
        }
        if(!address.isLoopbackAddress()) {
            return false;
        }
        return !getExchange().getRequestHeaders().contains(Headers.X_FORWARDED_FOR);
    }

}
 
Example 5
Project: screen-share-to-browser   File: NetUtil.java   Source Code and License Vote up 6 votes
public static String getIp() {
    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.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 6
Project: android-lite-utils   File: DevicesUtils.java   Source Code and License Vote up 6 votes
/**
 * 获取设备IP
 *
 * @return
 */
public String getDevicesIp() {
    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) {
        ex.printStackTrace();
    }
    return null;
}
 
Example 7
Project: RLibrary   File: Network.java   Source Code and License Vote up 6 votes
/**
 * 获取GSM网络的IP地址
 */
public static String getMobileIP() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = (NetworkInterface) en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    String ipaddress = inetAddress.getHostAddress().toString();
                    return ipaddress;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("getMobileIP", "Exception in Get IP Address: " + ex.toString());
    }
    return "";
}
 
Example 8
Project: ADBToolKitsInstaller   File: MainActivity.java   Source Code and License Vote up 6 votes
public 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.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return "0.0.0.0";
}
 
Example 9
Project: Ec2m   File: NetUtil.java   Source Code and License Vote up 6 votes
/**
 * getLocalIpAddress
 * @return
 */
public static String getLocalIpAddress() {
    String ret = "";
    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()) {
                    ret = inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return ret;
}
 
Example 10
Project: dubbo-transaction   File: NetworkUtils.java   Source Code and License Vote up 5 votes
private static boolean isValidAddress(InetAddress address) {
    if(address != null && !address.isLoopbackAddress()) {
        String name = address.getHostAddress();
        return name != null &&!LOCAL_IP_PATTERN.matcher(name).matches()&&!name.equalsIgnoreCase("localhost")&& !"0.0.0.0".equals(name) && !"192.168.122.1".equals(name)&& !"127.0.0.1".equals(name) && IP_PATTERN.matcher(name).matches();
    } else {
        return false;
    }
}
 
Example 11
Project: aidos-node   File: Peers.java   Source Code and License Vote up 5 votes
public boolean isLocal() {
	InetAddress a = address.getAddress();
	if (a.isLinkLocalAddress() || a.isLoopbackAddress() || a.isSiteLocalAddress()) {
		log.error("Loopback address: {}", a);
		return true;
	}
	return false;
}
 
Example 12
Project: elasticsearch_my   File: IfConfig.java   Source Code and License Vote up 5 votes
/** format internet address: java's default doesn't include everything useful */
private static String formatAddress(InterfaceAddress interfaceAddress) throws IOException {
    StringBuilder sb = new StringBuilder();

    InetAddress address = interfaceAddress.getAddress();
    if (address instanceof Inet6Address) {
        sb.append("inet6 ");
        sb.append(NetworkAddress.format(address));
        sb.append(" prefixlen:");
        sb.append(interfaceAddress.getNetworkPrefixLength());
    } else {
        sb.append("inet ");
        sb.append(NetworkAddress.format(address));
        int netmask = 0xFFFFFFFF << (32 - interfaceAddress.getNetworkPrefixLength());
        sb.append(" netmask:" + NetworkAddress.format(InetAddress.getByAddress(new byte[] {
                (byte)(netmask >>> 24),
                (byte)(netmask >>> 16 & 0xFF),
                (byte)(netmask >>> 8 & 0xFF),
                (byte)(netmask & 0xFF)
        })));
        InetAddress broadcast = interfaceAddress.getBroadcast();
        if (broadcast != null) {
            sb.append(" broadcast:" + NetworkAddress.format(broadcast));
        }
    }
    if (address.isLoopbackAddress()) {
        sb.append(" scope:host");
    } else if (address.isLinkLocalAddress()) {
        sb.append(" scope:link");
    } else if (address.isSiteLocalAddress()) {
        sb.append(" scope:site");
    }
    return sb.toString();
}
 
Example 13
Project: EatDubbo   File: NetUtils.java   Source Code and License Vote up 5 votes
private static boolean isValidAddress(InetAddress address) {
    if (address == null || address.isLoopbackAddress())
        return false;
    String name = address.getHostAddress();
    return (name != null 
            && ! ANYHOST.equals(name)
            && ! LOCALHOST.equals(name) 
            && IP_PATTERN.matcher(name).matches());
}
 
Example 14
Project: Utils   File: NetworkUtil.java   Source Code and License Vote up 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 15
Project: ForeverLibrary   File: NetworkUtils.java   Source Code and License Vote up 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 16
Project: aos-FileCoreLibrary   File: SambaDiscovery.java   Source Code and License Vote up 4 votes
private boolean isUsableIpforSMB(InetAddress inetAddress) {
    return !inetAddress.isLoopbackAddress()
            && !inetAddress.isAnyLocalAddress()
            && (inetAddress instanceof Inet4Address);
}
 
Example 17
Project: BiglyBT   File: Result.java   Source Code and License Vote up 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 18
Project: uavstack   File: HostNewworkInfo.java   Source Code and License Vote up 4 votes
private List<InetAddress> readInfo(String name) {

        List<InetAddress> ipList = new ArrayList<InetAddress>();

        InetAddress ip = null;
        Enumeration<NetworkInterface> netInterfaces;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();

            while (netInterfaces.hasMoreElements()) {

                NetworkInterface ni = netInterfaces.nextElement();

                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = ips.nextElement();

                    if (!ni.isVirtual() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {

                        if (name != null) {

                            if (ni.getName().equalsIgnoreCase(name)) {
                                ipList.add(ip);
                            }
                        }
                        else {

                            ipList.add(ip);
                        }

                    }
                }

            }
        }
        catch (SocketException e) {
            // ignore
        }

        return ipList;
    }
 
Example 19
Project: open-rmbt   File: Helperfunctions.java   Source Code and License Vote up 4 votes
public static boolean isIPLocal(final InetAddress adr)
{
    return adr.isLinkLocalAddress() || adr.isLoopbackAddress() || adr.isSiteLocalAddress();
}
 
Example 20
Project: asura   File: InetAddresses.java   Source Code and License Vote up 2 votes
/**
 * 验证是不是有效的ip地址
 *
 * @param address
 *         待验证IP字符串
 *
 * @return
 */
private static boolean isValidAddress(InetAddress address) {
    if (address == null || address.isLoopbackAddress()) return false;
    String name = address.getHostAddress();
    return (name != null && !ANYHOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name).matches());
}