List of usage examples for java.net InetAddress isLoopbackAddress
public boolean isLoopbackAddress()
From source file:ca.frozen.curlingtv.classes.Utils.java
public static String getLocalIpAddress() { try {/*w w w . j a v a2 s.c o 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() && inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress(); } } } } catch (SocketException ex) { ex.printStackTrace(); } return null; }
From source file:cycronix.CTandroid.CTAserver.java
/** * Get IP address from first non-localhost interface * @param ipv4 true=return ipv4, false=return ipv6 * @return address or empty string//ww w.ja v a 2 s.c om */ public static String getIPAddress(boolean useIPv4) { try { 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(); // org.apache.http.conn.util is no longer supported under Android API 23 // http://stackoverflow.com/questions/32141785/android-api-23-inetaddressutils-replacement // https://developer.android.com/sdk/api_diff/23/changes.html // boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); InetAddress tempInetAddress = InetAddress.getByName(sAddr); boolean isIPv4 = false; if (tempInetAddress instanceof Inet4Address) { isIPv4 = true; } if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return "Unknown"; }
From source file:es.auth.plugin.AbstractUnitTest.java
public static String getNonLocalhostAddress() { try {/*from ww w. j a v a 2 s. co m*/ for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { final NetworkInterface intf = en.nextElement(); if (intf.isLoopback() || !intf.isUp()) { continue; } for (final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { final InetAddress ia = enumIpAddr.nextElement(); if (ia.isLoopbackAddress() || ia instanceof Inet6Address) { continue; } return ia.getHostAddress(); } } } catch (final SocketException e) { throw new RuntimeException(e); } System.out.println("ERROR: No non-localhost address available, will use localhost"); return "localhost"; }
From source file:org.ngrinder.recorder.util.NetworkUtil.java
private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException { Enumeration<?> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface i = (NetworkInterface) en.nextElement(); if (!i.isUp()) { continue; }/*from w w w. j a va2s.c o m*/ if (StringUtils.containsIgnoreCase(i.getDisplayName(), "Host-Only")) { continue; } for (Enumeration<?> en2 = i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr = (InetAddress) en2.nextElement(); if (!addr.isLoopbackAddress()) { if (addr instanceof Inet4Address) { if (preferIPv6) { continue; } return addr; } if (addr instanceof Inet6Address) { if (preferIpv4) { continue; } return addr; } } } } return null; }
From source file:net.sbbi.upnp.Discovery.java
private static UPNPRootDevice[] discoverDevices(int timeOut, int ttl, int mx, String searchTarget, NetworkInterface ni) throws IOException { if (searchTarget == null || searchTarget.trim().length() == 0) { throw new IllegalArgumentException("Illegal searchTarget"); }//from w ww . j ava 2 s.c o m final Map devices = new HashMap(); DiscoveryResultsHandler handler = new DiscoveryResultsHandler() { public void discoveredDevice(String usn, String udn, String nt, String maxAge, URL location, String firmware) { synchronized (devices) { if (!devices.containsKey(usn)) { try { UPNPRootDevice device = new UPNPRootDevice(location, maxAge, firmware, usn, udn); devices.put(usn, device); } catch (Exception ex) { log.error("Error occured during upnp root device object creation from location " + location, ex); } } } } }; DiscoveryListener.getInstance().registerResultsHandler(handler, searchTarget); if (ni == null) { for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) { NetworkInterface intf = (NetworkInterface) e.nextElement(); for (Enumeration adrs = intf.getInetAddresses(); adrs.hasMoreElements();) { InetAddress adr = (InetAddress) adrs.nextElement(); if (adr instanceof Inet4Address && !adr.isLoopbackAddress()) { sendSearchMessage(adr, ttl, mx, searchTarget); } } } } else { for (Enumeration adrs = ni.getInetAddresses(); adrs.hasMoreElements();) { InetAddress adr = (InetAddress) adrs.nextElement(); if (adr instanceof Inet4Address && !adr.isLoopbackAddress()) { sendSearchMessage(adr, ttl, mx, searchTarget); } } } try { Thread.sleep(timeOut); } catch (InterruptedException ex) { // don't care } DiscoveryListener.getInstance().unRegisterResultsHandler(handler, searchTarget); if (devices.size() == 0) { return null; } int j = 0; UPNPRootDevice[] rootDevices = new UPNPRootDevice[devices.size()]; for (Iterator i = devices.values().iterator(); i.hasNext();) { rootDevices[j++] = (UPNPRootDevice) i.next(); } return rootDevices; }
From source file:com.triggertrap.ZeroConf.java
/** * Returns the first found IP4 address.//from ww w. j a v a 2 s .com * * @return the first found IP4 address */ public static InetAddress getIPAddress(int wifi_ipaddr) { try { String ipString = String.format("%d.%d.%d.%d", (wifi_ipaddr & 0xff), (wifi_ipaddr >> 8 & 0xff), (wifi_ipaddr >> 16 & 0xff), (wifi_ipaddr >> 24 & 0xff)); 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() && (ipString.equals(addr.getHostAddress()))) { //String sAddr = addr.getHostAddress().toUpperCase(); if (addr instanceof java.net.Inet4Address) { //Log.d("found IP address to listen: " , sAddr); return addr; } } } } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.wso2.carbon.integration.test.ha.HATestCase.java
public static String findAddress(String hostname) throws SocketException { if (hostname.trim().equals("localhost") || hostname.trim().equals("127.0.0.1") || hostname.trim().equals("::1")) { Enumeration<NetworkInterface> 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()) { return addr.getHostAddress(); }//from www . ja v a 2s .co m } } return "127.0.0.1"; } else { return hostname; } }
From source file:de.jaetzold.networking.SimpleServiceDiscovery.java
/** * Comma separated List of IP adresses of available network interfaces * @param useIPv4//from www. j av a 2 s . c om * @return */ public static String getIPAddress(boolean useIPv4) { String addresses = ""; try { 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(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) addresses += sAddr + ", "; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix if (delim < 0) addresses += sAddr + ", "; else addresses += sAddr.substring(0, delim) + ", "; } } } } } } catch (Exception ex) { } // for now eat exceptions if (addresses == null || addresses.length() <= 3) return ""; return addresses.subSequence(0, addresses.length() - 2).toString(); }
From source file:com.starit.diamond.client.processor.LocalConfigInfoProcessor.java
static String getHostAddress() { String address = "127.0.0.1"; try {//from ww w . j av a 2 s . c om 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) { throw new RuntimeException("??", e); } return address; }
From source file:com.dmsl.anyplace.utils.NetworkUtils.java
public static String getLocalIP(boolean useIPv4) { String ip = "No Local IP assigned"; try {// w w w. jav a2 s. c om List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni : interfaces) { List<InetAddress> addresses = Collections.list(ni.getInetAddresses()); for (InetAddress ia : addresses) { if (ia != null && !ia.isLoopbackAddress()) { String sAddr = ia.getHostAddress().toUpperCase(Locale.ENGLISH); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { ip = sAddr; } } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); ip = delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { ip = "Unknown Error, Report it!"; } return ip; }