Example usage for java.net InetAddress isLoopbackAddress

List of usage examples for java.net InetAddress isLoopbackAddress

Introduction

In this page you can find the example usage for java.net InetAddress isLoopbackAddress.

Prototype

public boolean isLoopbackAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a loopback address.

Usage

From source file:com.sentaroh.android.SMBSync2.CommonUtilities.java

public static String getIfIpAddress() {
    String result = "";
    try {//from w w w .  j  a  v  a2 s.c om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                //                  Log.v("","ip="+inetAddress.getHostAddress());
                if (!inetAddress.isLoopbackAddress() && (inetAddress.getHostAddress().startsWith("0")
                        || inetAddress.getHostAddress().startsWith("1")
                        || inetAddress.getHostAddress().startsWith("2"))) {
                    result = inetAddress.getHostAddress();
                    break;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(APPLICATION_TAG, ex.toString());
        result = "192.168.0.1";
    }
    //      Log.v("","getIfIpAddress result="+result);
    if (result.equals(""))
        result = "192.168.0.1";
    return result;
}

From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java

public static List<InetAddress> getFilteredIPv6Addrs() {

    boolean ignoreLoopback = DhcpServerPolicies.globalPolicyAsBoolean(Property.DHCP_IGNORE_LOOPBACK);
    boolean ignoreLinkLocal = DhcpServerPolicies.globalPolicyAsBoolean(Property.DHCP_IGNORE_LINKLOCAL);

    List<InetAddress> myV6Addrs = new ArrayList<InetAddress>();
    List<InetAddress> allV6Addrs = getAllIPv6Addrs();
    if (allV6Addrs != null) {
        for (InetAddress ip : allV6Addrs) {
            if (ignoreLoopback && ip.isLoopbackAddress()) {
                log.debug("Skipping loopback address: " + ip);
                continue;
            }//w ww. jav  a2 s.  co m
            if (ignoreLinkLocal && ip.isLinkLocalAddress()) {
                log.debug("Skipping link local address: " + ip);
                continue;
            }
            myV6Addrs.add(ip);
        }
    }
    return myV6Addrs;
}

From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java

public static List<InetAddress> getFilteredIPv4Addrs() {

    boolean ignoreLoopback = DhcpServerPolicies.globalPolicyAsBoolean(Property.DHCP_IGNORE_LOOPBACK);
    boolean ignoreLinkLocal = DhcpServerPolicies.globalPolicyAsBoolean(Property.DHCP_IGNORE_LINKLOCAL);

    List<InetAddress> myV4Addrs = new ArrayList<InetAddress>();
    List<InetAddress> allV4Addrs = getAllIPv4Addrs();
    if (allV4Addrs != null) {
        for (InetAddress ip : allV4Addrs) {
            if (ignoreLoopback && ip.isLoopbackAddress()) {
                log.debug("Skipping loopback address: " + ip);
                continue;
            }/*  w  ww.  j a  v  a  2s .c o  m*/
            if (ignoreLinkLocal && ip.isLinkLocalAddress()) {
                log.debug("Skipping link local address: " + ip);
                continue;
            }
            myV4Addrs.add(ip);
        }
    }
    return myV4Addrs;
}

From source file:com.example.zoetablet.BasicFragmentActivity.java

protected static String getLocalIpAddress() {
    try {//from w w w.jav  a  2s .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()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("Tablet", ex.toString());
    }
    return null;
}

From source file:com.datatorrent.stram.client.StramClientUtils.java

public static String getSocketConnectString(InetSocketAddress socketAddress) {
    String host;/*from   w w  w. ja v a2s .  c o  m*/
    InetAddress address = socketAddress.getAddress();
    if (address == null) {
        host = socketAddress.getHostString();
    } else if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
        host = address.getCanonicalHostName();
    } else {
        host = address.getHostName();
    }
    return host + ":" + socketAddress.getPort();
}

From source file:com.googlecode.networklog.NetworkLog.java

public static void getLocalIpAddresses() {
    MyLog.d("getLocalIpAddresses");
    localIpAddrs = new ArrayList<String>();

    try {/*from   w  w w  .ja v  a  2s.  c om*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            MyLog.d("Network interface found: " + intf.toString());

            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                MyLog.d("InetAddress: " + inetAddress.toString());

                if (!inetAddress.isLoopbackAddress()) {
                    MyLog.d("Adding local IP address: [" + inetAddress.getHostAddress().toString() + "]");
                    localIpAddrs.add(inetAddress.getHostAddress().toString());
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("NetworkLog", ex.toString());
    }
}

From source file:com.aurel.track.prop.ApplicationBean.java

/**
 * Retrieve all non-loopback IP addresses from all network interfaces
 *
 * @return the array of assigned IP numbers
 *///from w  w w  .j a va2  s  .  c o m
public static InetAddress[] getInetAddress() {
    List<InetAddress> allIPs = new ArrayList<InetAddress>();
    InetAddress[] allAds = null;

    try {
        InetAddress candidateAddress = null;
        // Iterate all NICs (network interface cards)...
        for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces
                .hasMoreElements();) {
            NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
            // Iterate all IP addresses assigned to each card...
            for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
                InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
                if (!inetAddr.isLoopbackAddress()) {

                    if (inetAddr.isSiteLocalAddress()) {
                        // Found non-loopback site-local address. Return it
                        // immediately...
                        allIPs.add(inetAddr);
                        continue;
                    } else if (candidateAddress == null) {
                        // Found non-loopback address, but not necessarily
                        // site-local.
                        // Store it as a candidate to be returned if
                        // site-local address is not subsequently found...
                        candidateAddress = inetAddr;
                        // Note that we don't repeatedly assign non-loopback
                        // non-site-local addresses as candidates,
                        // only the first. For subsequent iterations,
                        // candidate will be non-null.
                    }
                }
            }
        }

        // At this point, we did not find a non-loopback address.
        // Fall back to returning whatever InetAddress.getLocalHost()
        // returns...
        if (allIPs.isEmpty()) {
            allIPs.add(InetAddress.getLocalHost());
        }

        allAds = new InetAddress[allIPs.size()];
        allAds = allIPs.toArray(allAds);

    } catch (Exception uhn) {
        LOGGER.error(
                "An exception occurred trying to get " + "all IP addresses for this host: " + uhn.getMessage());
    }
    return allAds;
}

From source file:eu.betaas.betaasandroidapp.configuration.Configuration.java

private void setDeviceIp() {
    try {//from   w  w  w . j av 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 (isIPv4) {
                        deviceIP = sAddr;
                    }
                }
            }
        }
    } catch (Exception ex) {
        deviceIP = null;
    }
}

From source file:com.marcosdiez.server.php.service.Network.java

public Network() {
    names = new LinkedList<String>();
    titles = new LinkedList<String>();
    adresses = new LinkedList<String>();
    try {//from  w  ww  .  j  a  v  a 2  s . co  m
        List<NetworkInterface> list = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface iface : list) {
            List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
            for (InetAddress address : addresses) {
                if (!address.isLoopbackAddress()) {
                    String host = address.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(host)) {
                        names.add(iface.getName());
                        titles.add(host + "(" + iface.getDisplayName() + ")");
                        adresses.add(host);
                    }
                }
            }
        }
        for (NetworkInterface iface : list) {
            List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
            for (InetAddress address : addresses) {
                if (address.isLoopbackAddress()) {
                    String host = address.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(host)) {
                        names.add(iface.getName());
                        titles.add(host + "(" + iface.getDisplayName() + ")");
                        adresses.add(host);
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    names.add("all");
    adresses.add("0.0.0.0");
    titles.add("0.0.0.0 (all)");
}

From source file:org.apache.hadoop.hbase.regionserver.TestRegionServerHostname.java

@Test(timeout = 120000)
public void testRegionServerHostname() throws Exception {
    final int NUM_MASTERS = 1;
    final int NUM_RS = 1;
    Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces();

    while (netInterfaceList.hasMoreElements()) {
        NetworkInterface ni = netInterfaceList.nextElement();
        Enumeration<InetAddress> addrList = ni.getInetAddresses();
        // iterate through host addresses and use each as hostname
        while (addrList.hasMoreElements()) {
            InetAddress addr = addrList.nextElement();
            if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) {
                continue;
            }/*from  ww  w  .j  av  a2 s.  c  o  m*/
            String hostName = addr.getHostName();
            LOG.info("Found " + hostName + " on " + ni);

            TEST_UTIL.getConfiguration().set(HRegionServer.MASTER_HOSTNAME_KEY, hostName);
            TEST_UTIL.getConfiguration().set(HRegionServer.RS_HOSTNAME_KEY, hostName);
            TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
            try {
                ZooKeeperWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
                List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.rsZNode);
                // there would be NUM_RS+1 children - one for the master
                assertTrue(servers.size() == NUM_RS + 1);
                for (String server : servers) {
                    assertTrue("From zookeeper: " + server + " hostname: " + hostName,
                            server.startsWith(hostName.toLowerCase() + ","));
                }
                zkw.close();
            } finally {
                TEST_UTIL.shutdownMiniCluster();
            }
        }
    }
}