Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

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

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:com.google.appinventor.components.runtime.BlockyTalky.java

private static String getIPAddress(boolean useIPv4) {
    try {/* w  w w . ja 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:io.joynr.util.JoynrUtil.java

public static void checkConnectivity(String host) throws IOException {
    InetAddress headUnitAddr = InetAddress.getByName(host);
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    NetworkInterface iface;//from   w  w w  .  j  a  v  a 2 s.c o m

    while (interfaces.hasMoreElements()) {
        iface = interfaces.nextElement();
        if (headUnitAddr.isReachable(iface, 0, 5000)) {
            return;
        }
    }
    throw new UnknownHostException("Connectivity check to the host \"" + host + "\" has failed");
}

From source file:com.hypersocket.netty.NettyServer.java

protected void bindInterface(Integer port, Set<Channel> channels) throws IOException {

    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();

    Set<String> interfacesToBind = new HashSet<String>(
            Arrays.asList(configurationService.getValues("listening.interfaces")));

    if (interfacesToBind.isEmpty()) {

        if (log.isInfoEnabled()) {
            log.info("Binding server to all interfaces on port " + port);
        }// w w  w .  ja  v  a 2  s.co  m
        Channel ch = serverBootstrap.bind(new InetSocketAddress(port));
        channels.add(ch);

        if (log.isInfoEnabled()) {
            log.info("Bound to port " + ((InetSocketAddress) ch.getLocalAddress()).getPort());
        }
    } else {
        while (e.hasMoreElements()) {

            NetworkInterface i = e.nextElement();

            Enumeration<InetAddress> inetAddresses = i.getInetAddresses();

            for (InetAddress inetAddress : Collections.list(inetAddresses)) {

                if (interfacesToBind.contains(inetAddress.getHostAddress())) {
                    try {
                        if (log.isInfoEnabled()) {
                            log.info("Binding server to interface " + i.getDisplayName() + " "
                                    + inetAddress.getHostAddress() + ":" + port);
                        }

                        Channel ch = serverBootstrap.bind(new InetSocketAddress(inetAddress, port));
                        channels.add(ch);

                        if (log.isInfoEnabled()) {
                            log.info("Bound to " + inetAddress.getHostAddress() + ":"
                                    + ((InetSocketAddress) ch.getLocalAddress()).getPort());
                        }

                    } catch (ChannelException ex) {
                        log.error("Failed to bind port", ex);
                    }
                }
            }
        }
    }
}

From source file:com.offbynull.portmapper.common.NetworkUtils.java

/**
 * Set a {@link MulticastChannel} to listen on all IPv6 interfaces.
 * @param channel multicast channel to listen on
 * @throws IOException if there's an error
 * @throws NullPointerException if any argument is {@code null}
 *///w w w.j av  a2  s .c o m
public static void multicastListenOnAllIpv6InterfaceAddresses(MulticastChannel channel) throws IOException {
    Validate.notNull(channel);

    final InetAddress ipv6Group = InetAddress.getByName("ff02::1"); // NOPMD

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
        while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface
            InetAddress addr = addrs.nextElement();

            try {
                if (addr instanceof Inet6Address) {
                    channel.join(ipv6Group, networkInterface);
                }
            } catch (IOException ioe) { // NOPMD
                // occurs with certain interfaces, do nothing
            }
        }
    }
}

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...
 * //w ww. j  av  a2  s .c om
 * @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.apache.sentry.provider.db.log.util.CommandUtil.java

@VisibleForTesting
public static boolean assertIPInAuditLog(String ipInAuditLog) throws Exception {
    if (ipInAuditLog == null) {
        return false;
    }/*w ww .  j a  va2s .  c o  m*/
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = netInterfaces.nextElement();
        Enumeration<InetAddress> ips = ni.getInetAddresses();
        while (ips.hasMoreElements()) {
            if (ipInAuditLog.indexOf(ips.nextElement().getHostAddress()) != -1) {
                return true;
            }
        }
    }
    return false;
}

From source file:es.auth.plugin.AbstractUnitTest.java

public static String getNonLocalhostAddress() {
    try {/*from   w w w  . j  av  a  2s  .  com*/
        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:com.seleuco.mame4droid.NetPlay.java

public String getIPAddress() {
    try {//from   w  w w .  j a  v a2s  . co m
        Enumeration<NetworkInterface> ifaceList;
        NetworkInterface selectedIface = null;

        // First look for a WLAN interface
        ifaceList = NetworkInterface.getNetworkInterfaces();
        while (selectedIface == null && ifaceList.hasMoreElements()) {
            NetworkInterface intf = ifaceList.nextElement();
            if (intf.getName().startsWith("wlan")) {
                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)
                            return sAddr;
                    }
                }
            }
        }

        // If we didn't find that, look for an Ethernet interface
        ifaceList = NetworkInterface.getNetworkInterfaces();
        while (selectedIface == null && ifaceList.hasMoreElements()) {
            NetworkInterface intf = ifaceList.nextElement();
            if (intf.getName().startsWith("eth")) {
                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)
                            return sAddr;
                    }
                }
            }
        }

        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)
                        return sAddr;
                }
            }
        }
    } catch (Exception ex) {
    }
    return null;
}

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

/**
 * ?ip//from   www.j a v  a  2  s .c  om
 * @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;
}