List of usage examples for java.net InetAddress isLoopbackAddress
public boolean isLoopbackAddress()
From source file:massbank.svn.SVNUtils.java
/** * /* w w w . j av a 2s . c o m*/ */ public static String getLocalIPAddress() { String address = ""; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface network = interfaces.nextElement(); Enumeration<InetAddress> addresses = network.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress inet = addresses.nextElement(); if (!inet.isLoopbackAddress() && inet instanceof Inet4Address) { return inet.getHostAddress(); } } } address = InetAddress.getLocalHost().getHostAddress(); } catch (Exception e) { } return address; }
From source file:Main.java
protected static void obtainHostInfo() { HOST_IPADDRESS = "127.0.0.1"; HOST_NAME = ""; try {//from w ww. j a va 2 s.co m InetAddress primera = InetAddress.getLocalHost(); String hostname = InetAddress.getLocalHost().getHostName(); if (!primera.isLoopbackAddress() && !primera.getHostName().equalsIgnoreCase("localhost") && primera.getHostAddress().indexOf(':') == -1) { // Get it without delay!! //System.out.println ("we have it!"); HOST_IPADDRESS = primera.getHostAddress(); HOST_NAME = hostname; //System.out.println (hostname + " IP " + HOST_IPADDRESS); return; } // Get it by slow way ... InetAddress[] familia = InetAddress.getAllByName(hostname); int netto = 1; for (int aa = 0; aa < familia.length; aa++) { // System.out.println ("Networko " + netto++); InetAddress laAdd = familia[aa]; String ipstring = laAdd.getHostAddress(); hostname = laAdd.getHostName(); // don't know if it can change, probably not // System.out.println ("checking : " + hostname + " IP " + ipstring); if (laAdd.isLoopbackAddress()) continue; if (hostname.equalsIgnoreCase("localhost")) continue; if (ipstring.indexOf(':') >= 0) continue; // System.out.println ("this pass!"); HOST_IPADDRESS = ipstring; HOST_NAME = hostname; break; } } catch (Exception e) { } }
From source file:com.seadee.library.receiver.NetworkStateReceiver.java
public static String getLocalIpAddress(Context context) { final String IPTAG = "getLocalIpAddress"; String nullAddress = "0.0.0.0"; try {// w w w . j a v a 2s. com for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); String ip_address = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address)) return ip_address; } } } catch (SocketException ex) { Log.e(IPTAG, ex.toString()); } return nullAddress; }
From source file:org.sonar.application.config.ClusterSettings.java
@VisibleForTesting protected static void ensureNotLoopback(Props props, String key) { String ipList = props.value(key); if (ipList == null) { return;//from w w w . j ava 2 s. c o m } stream(ipList.split(",")).filter(StringUtils::isNotBlank).map(StringUtils::trim).forEach(ip -> { InetAddress inetAddress = convertToInetAddress(ip, key); if (inetAddress.isLoopbackAddress()) { throw new MessageException( format("The interface address [%s] of [%s] must not be a loopback address", ip, key)); } }); }
From source file:com.DPFaragir.DPFUtils.java
public static String getIPAddress(boolean useIPv4) { try {//from ww w.j a v a 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(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 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 ""; }
From source file:com.vinexs.tool.NetworkManager.java
public static String getIPAddress(boolean useIPv4) { try {/*from ww w. ja va 2s .c om*/ 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 = InetAddressUtilsHC4.isIPv4Address(sAddr); 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 e) { e.printStackTrace(); } return ""; }
From source file:com.vaadin.tests.tb3.PrivateTB3Configuration.java
/** * Tries to automatically determine the IP address of the machine the test * is running on./*from w w w . j a v a2s .c om*/ * * @return An IP address of one of the network interfaces in the machine. * @throws RuntimeException * if there was an error or no IP was found */ private static String findAutoHostname() { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface nwInterface = interfaces.nextElement(); if (!nwInterface.isUp() || nwInterface.isLoopback() || nwInterface.isVirtual()) { continue; } Enumeration<InetAddress> addresses = nwInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address.isLoopbackAddress()) { continue; } if (address.isSiteLocalAddress()) { return address.getHostAddress(); } } } } catch (SocketException e) { throw new RuntimeException("Could not enumerate "); } throw new RuntimeException("No compatible (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) ip address found."); }
From source file:org.apache.smscserver.test.TestUtil.java
public static InetAddress findNonLocalhostIp() throws Exception { Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); while (nifs.hasMoreElements()) { NetworkInterface nif = nifs.nextElement(); Enumeration<InetAddress> ips = nif.getInetAddresses(); while (ips.hasMoreElements()) { InetAddress ip = ips.nextElement(); if ((ip instanceof java.net.Inet4Address) && !ip.isLoopbackAddress()) { return ip; } else { // IPv6 not tested }//from w w w. j a v a2 s . c o m } } return null; }
From source file:org.encuestame.core.util.InternetUtils.java
/** * /*from w ww.j a v a 2s. c o m*/ * @param addr * @return */ public static boolean isThisMyIpAddress(InetAddress addr) { // Check if the address is a valid special local or loop back if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) return true; // Check if the address is defined on any interface try { return NetworkInterface.getByInetAddress(addr) != null; } catch (SocketException e) { return false; } }
From source file:com.vinexs.tool.NetworkManager.java
public static InetAddress getInetAddress(Context context) { if (haveNetwork(context)) { return null; }//from ww w . j av a 2 s .com if (isWifiNetwork(context)) { int ipAddress = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo() .getIpAddress(); if (ipAddress == 0) { return null; } return intToInet(ipAddress); } try { Enumeration<NetworkInterface> netinterfaces = NetworkInterface.getNetworkInterfaces(); while (netinterfaces.hasMoreElements()) { NetworkInterface netinterface = netinterfaces.nextElement(); Enumeration<InetAddress> adresses = netinterface.getInetAddresses(); while (adresses.hasMoreElements()) { InetAddress address = adresses.nextElement(); if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) { return address; } } } } catch (Exception e) { e.printStackTrace(); } return null; }