Example usage for java.net NetworkInterface getInetAddresses

List of usage examples for java.net NetworkInterface getInetAddresses

Introduction

In this page you can find the example usage for java.net NetworkInterface getInetAddresses.

Prototype

public Enumeration<InetAddress> getInetAddresses() 

Source Link

Document

Get an Enumeration with all or a subset of the InetAddresses bound to this network interface.

Usage

From source file:org.apache.geode.internal.net.SocketCreator.java

/**
 * Returns true if host matches the LOCALHOST.
 *//*  w  w w.j  a v a2  s.  c  o m*/
public static boolean isLocalHost(Object host) {
    if (host instanceof InetAddress) {
        if (InetAddressUtil.LOCALHOST.equals(host)) {
            return true;
        } else if (((InetAddress) host).isLoopbackAddress()) {
            return true;
        } else {
            try {
                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 (host.equals(addr)) {
                            return true;
                        }
                    }
                }
                return false;
            } catch (SocketException e) {
                throw new IllegalArgumentException(
                        LocalizedStrings.InetAddressUtil_UNABLE_TO_QUERY_NETWORK_INTERFACE.toLocalizedString(),
                        e);
            }
        }
    } else {
        return isLocalHost(toInetAddress(host.toString()));
    }
}

From source file:com.taobao.adfs.util.Utilities.java

public static List<InetAddress> getInetAddressList() throws IOException {
    List<InetAddress> addressList = new ArrayList<InetAddress>();
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
        NetworkInterface netInterface = netInterfaces.nextElement();
        Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
            InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress != null && inetAddress instanceof Inet4Address)
                addressList.add(inetAddress);
        }//from  w  w w  . j  a  v a2  s  . com
    }

    List<InetAddress> newAddressList = getWanInetAddressList(addressList);
    newAddressList.addAll(getLanInetAddressList(addressList));
    newAddressList.addAll(getLoopbackInetAddressList(addressList));
    newAddressList.addAll(getLinklocalInetAddressList(addressList));

    return newAddressList;
}

From source file:org.peercast.core.PeerCastServiceController.java

private String getIpAddress() {
    Enumeration<NetworkInterface> netIFs;
    try {/*from  ww  w .j  av a  2 s  .co  m*/
        netIFs = NetworkInterface.getNetworkInterfaces();
        while (netIFs.hasMoreElements()) {
            NetworkInterface netIF = netIFs.nextElement();
            Enumeration<InetAddress> ipAddrs = netIF.getInetAddresses();
            while (ipAddrs.hasMoreElements()) {
                InetAddress ip = ipAddrs.nextElement();
                if (!ip.isLoopbackAddress() && !ip.isLinkLocalAddress() && ip.isSiteLocalAddress()) {
                    String ipStr = ip.getHostAddress().toString();
                    Log.d(TAG, "IP: " + ipStr);
                    return ipStr;
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.powertac.tournament.services.TournamentProperties.java

private String getTourneyUrl() {
    if (!properties.getProperty("tourney.location", "").isEmpty()) {
        return properties.getProperty("tourney.location");
    }// ww w. jav  a 2 s .c  o m

    String tourneyUrl = "http://%s:8080/TournamentScheduler/";
    String address = "127.0.0.1";
    try {
        Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
        while (n.hasMoreElements()) {
            NetworkInterface e = n.nextElement();
            if (e.getName().startsWith("lo")) {
                continue;
            }

            Enumeration<InetAddress> a = e.getInetAddresses();
            while (a.hasMoreElements()) {
                InetAddress addr = a.nextElement();
                if (addr.getClass().getName().equals("java.net.Inet4Address")) {
                    address = addr.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        messages.add("Error getting Tournament Location!");
    }

    return String.format(tourneyUrl, address);
}

From source file:weinre.server.ServerSettings.java

public String[] getBoundHosts() {
    if (getBoundHostValue() != null) {
        return new String[] { getBoundHost() };
    }//w  w  w .  j  a va 2  s.  c  om

    ArrayList<String> hosts = new ArrayList<String>();
    List<NetworkInterface> networkInterfaces;
    try {
        networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
    } catch (SocketException e) {
        return new String[] { "localhost" };
    }

    for (NetworkInterface networkInterface : networkInterfaces) {
        List<InetAddress> inetAddresses = Collections.list(networkInterface.getInetAddresses());

        for (InetAddress inetAddress : inetAddresses) {
            hosts.add(inetAddress.getHostName());
        }
    }

    return hosts.toArray(new String[] {});
}

From source file:com.bluetooth.activities.WiFiControl.java

/**
 * This method gets an IPv4 address for the user to enter in the browser.
 * /*from   w  w w. j  a  v  a2 s .c o  m*/
 * @return The IP
 */
private 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()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.sf.janos.control.SonosController.java

/**
 * Broadcasts a search packet on all network interfaces. This method should
 * really be a part of the Discovery class, but that's not my code...
 * /*from w ww.ja va  2  s.c o m*/
 * @param searchTarget
 * @throws IOException 
 */
public void sendSearchPacket(String searchTarget) throws IOException {
    for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
        NetworkInterface intf = (NetworkInterface) e.nextElement();
        for (Enumeration<InetAddress> adrs = intf.getInetAddresses(); adrs.hasMoreElements();) {
            InetAddress adr = (InetAddress) adrs.nextElement();
            if (adr instanceof Inet4Address && !adr.isLoopbackAddress()) {
                Discovery.sendSearchMessage(adr, Discovery.DEFAULT_TTL, Discovery.DEFAULT_MX, searchTarget);
            }
        }
    }

}

From source file:org.mobisocial.corral.ContentCorral.java

public static String getLocalIpAddress() {
    try {//w  w  w . java  2s .  co 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()) {
                    // not ready for IPv6, apparently.
                    if (!inetAddress.getHostAddress().contains(":")) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
    } catch (SocketException ex) {

    }
    return null;
}

From source file:org.apache.hadoop.ipc.MiniRPCBenchmark.java

private void configureSuperUserIPAddresses(Configuration conf, String superUserShortName) throws IOException {
    ArrayList<String> ipList = new ArrayList<String>();
    Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces();
    while (netInterfaceList.hasMoreElements()) {
        NetworkInterface inf = netInterfaceList.nextElement();
        Enumeration<InetAddress> addrList = inf.getInetAddresses();
        while (addrList.hasMoreElements()) {
            InetAddress addr = addrList.nextElement();
            ipList.add(addr.getHostAddress());
        }// w w w .j  a  v a  2 s.  c  om
    }
    StringBuilder builder = new StringBuilder();
    for (String ip : ipList) {
        builder.append(ip);
        builder.append(',');
    }
    builder.append("127.0.1.1,");
    builder.append(InetAddress.getLocalHost().getCanonicalHostName());
    conf.setStrings(
            DefaultImpersonationProvider.getTestProvider().getProxySuperuserIpConfKey(superUserShortName),
            builder.toString());
}

From source file:com.momathink.common.tools.ToolMonitor.java

/**
 * ?ip// w  w w  .  ja v  a 2s .  co  m
 * @return  : "111.111.111.111"
 */
private String getRealIp() {
    String netip = null;// IP
    try {
        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        boolean finded = false;// ?IP
        while (netInterfaces.hasMoreElements() && !finded) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                ip = address.nextElement();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {// IP
                    netip = ip.getHostAddress();
                    finded = true;
                    break;
                }
            }
        }
    } catch (Exception e) {
    }
    return netip;
}