List of usage examples for java.net InetAddress isLoopbackAddress
public boolean isLoopbackAddress()
From source file:net.pocketmine.server.HomeActivity.java
public static String getIPAddress(boolean useIPv4) { try {//from w ww . j ava 2 s .c o m List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(Locale.US); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } return ha.getResources().getString(R.string.unknown); }
From source file:de.madvertise.android.sdk.MadUtil.java
/** * Fetch the address of the enabled interface * //ww w . j a v a 2s. co m * @return ip address as string */ protected 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()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { if (PRINT_LOG) Log.d(MadUtil.LOG, ex.toString()); } return ""; }
From source file:com.sckftr.android.utils.net.Network.java
/** * Get IP address from first non-localhost interface * @return address or empty string/*from w ww .j av a 2 s .com*/ */ 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()) { String ip = Formatter.formatIpAddress(inetAddress.hashCode()); Log.i("NET", "***** IP=" + ip); return ip; } } } } catch (SocketException ex) { Log.e("NET", ex.toString()); } return null; }
From source file:Main.java
public static InetAddress getLocalInetAddress() { InetAddress ip = null; try {//from w w w . j a va 2 s .c om 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:org.apache.hadoop.yarn.webapp.util.WebAppUtils.java
private static String getResolvedAddress(InetSocketAddress address) { address = NetUtils.getConnectAddress(address); StringBuilder sb = new StringBuilder(); InetAddress resolved = address.getAddress(); if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) { String lh = address.getHostName(); try {/*from w ww . java 2s .c o m*/ 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(); }
From source file:org.apache.axis2.transport.nhttp.ServerWorker.java
/** * Copied from transport.http of Axis2/*from www .ja v a 2 s.co m*/ * * Returns the ip address to be used for the replyto epr * CAUTION: * This will go through all the available network interfaces and will try to return an ip address. * First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found * then this will return this will return 127.0.0.1. * This will <b>not<b> consider IPv6 addresses. * <p/> * TODO: * - Improve this logic to genaralize it a bit more * - Obtain the ip to be used here from the Call API * * @return Returns String. * @throws java.net.SocketException */ private static String getIpAddress() throws SocketException { Enumeration e = NetworkInterface.getNetworkInterfaces(); String address = "127.0.0.1"; while (e.hasMoreElements()) { NetworkInterface netface = (NetworkInterface) e.nextElement(); Enumeration addresses = netface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress ip = (InetAddress) addresses.nextElement(); if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) { return ip.getHostAddress(); } } } return address; }
From source file:com.adamkruger.myipaddressinfo.NetworkInfoFragment.java
@TargetApi(Build.VERSION_CODES.GINGERBREAD) private static List<NetworkInterfaceInfo> getNetworkInterfaceInfos() { List<NetworkInterfaceInfo> networkInterfaceInfos = new ArrayList<NetworkInterfaceInfo>(); try {/*from w w w. j a v a 2 s . com*/ List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface networkInterface : interfaces) { NetworkInterfaceInfo networkInterfaceInfo = new NetworkInterfaceInfo(); networkInterfaceInfo.name = networkInterface.getDisplayName(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { byte[] MAC = networkInterface.getHardwareAddress(); if (MAC != null) { StringBuilder stringBuilder = new StringBuilder(18); for (byte b : MAC) { if (stringBuilder.length() > 0) { stringBuilder.append(':'); } stringBuilder.append(String.format("%02x", b)); } networkInterfaceInfo.MAC = stringBuilder.toString(); } networkInterfaceInfo.MTU = networkInterface.getMTU(); } List<InetAddress> addresses = Collections.list(networkInterface.getInetAddresses()); for (InetAddress address : addresses) { if (!address.isLoopbackAddress()) { networkInterfaceInfo.ipAddresses.add(InetAddressToString(address)); } } if (networkInterfaceInfo.ipAddresses.size() > 0) { networkInterfaceInfos.add(networkInterfaceInfo); } } } catch (SocketException e) { } return networkInterfaceInfos; }
From source file:org.apache.synapse.transport.passthru.api.PassThroughNHttpGetProcessor.java
/** * Whatever this method returns as the IP is ignored by the actual http/s listener when * its getServiceEPR is invoked. This was originally copied from axis2 * * @return Returns String./*from www. j av a 2 s .c o m*/ * @throws java.net.SocketException if the socket can not be accessed */ protected static String getIpAddress() throws SocketException { Enumeration e = NetworkInterface.getNetworkInterfaces(); String address = "127.0.0.1"; while (e.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) e.nextElement(); Enumeration addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress ip = (InetAddress) addresses.nextElement(); if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) { return ip.getHostAddress(); } } } return address; }
From source file:util.Control.java
private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException { Enumeration en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface i = (NetworkInterface) en.nextElement(); for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr = (InetAddress) en2.nextElement(); if (!addr.isLoopbackAddress()) { if (addr instanceof Inet4Address) { if (preferIPv6) { continue; }// w w w.j a v a 2s . c om return addr; } if (addr instanceof Inet6Address) { if (preferIpv4) { continue; } return addr; } } } } return null; }
From source file:com.cloud.utils.UriUtils.java
public static Pair<String, Integer> validateUrl(String format, String url) throws IllegalArgumentException { try {/*from ww w . j a v a 2 s . c o m*/ URI uri = new URI(url); if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) { throw new IllegalArgumentException("Unsupported scheme for url: " + url); } int port = uri.getPort(); if (!(port == 80 || port == 8080 || port == 443 || port == -1)) { throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed"); } if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) { port = 443; } else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) { port = 80; } String host = uri.getHost(); try { InetAddress hostAddr = InetAddress.getByName(host); if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) { throw new IllegalArgumentException("Illegal host specified in url"); } if (hostAddr instanceof Inet6Address) { throw new IllegalArgumentException( "IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); } } catch (UnknownHostException uhe) { throw new IllegalArgumentException("Unable to resolve " + host); } // verify format if (format != null) { String uripath = uri.getPath(); checkFormat(format, uripath); } return new Pair<String, Integer>(host, port); } catch (URISyntaxException use) { throw new IllegalArgumentException("Invalid URL: " + url); } }