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:net.pms.network.UPNPHelper.java

/**
 * Gets the new multicast socket./*from   w  w  w.  j a  v a 2  s  .  c o m*/
 *
 * @return the new multicast socket
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static MulticastSocket getNewMulticastSocket() throws IOException {
    NetworkInterface networkInterface = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName();

    if (networkInterface == null) {
        networkInterface = PMS.get().getServer().getNetworkInterface();
    }

    if (networkInterface == null) {
        throw new IOException("No usable network interface found for UPnP multicast");
    }

    List<InetAddress> usableAddresses = new ArrayList<InetAddress>();
    List<InetAddress> networkInterfaceAddresses = Collections.list(networkInterface.getInetAddresses());

    for (InetAddress inetAddress : networkInterfaceAddresses) {
        if (inetAddress != null && inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
            usableAddresses.add(inetAddress);
        }
    }

    if (usableAddresses.isEmpty()) {
        throw new IOException("No usable addresses found for UPnP multicast");
    }

    InetSocketAddress localAddress = new InetSocketAddress(usableAddresses.get(0), 0);
    MulticastSocket ssdpSocket = new MulticastSocket(localAddress);
    ssdpSocket.setReuseAddress(true);

    logger.trace(
            "Sending message from multicast socket on network interface: " + ssdpSocket.getNetworkInterface());
    logger.trace("Multicast socket is on interface: " + ssdpSocket.getInterface());
    ssdpSocket.setTimeToLive(32);
    logger.trace("Socket Timeout: " + ssdpSocket.getSoTimeout());
    logger.trace("Socket TTL: " + ssdpSocket.getTimeToLive());
    return ssdpSocket;
}

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

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

    try {//from  www .  ja v  a  2s  . co m
        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.adito.core.InterfacesMultiSelectListDataSource.java

public Collection<LabelValueBean> getValues(SessionInfo session) {
    ArrayList l = new ArrayList();
    try {//from  w  w  w  .  j  ava2s  . c o m
        // TODO make this localised
        l.add(new LabelValueBean("All Interfaces", "0.0.0.0"));
        for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
            NetworkInterface ni = (NetworkInterface) e.nextElement();
            for (Enumeration e2 = ni.getInetAddresses(); e2.hasMoreElements();) {
                InetAddress addr = (InetAddress) e2.nextElement();
                l.add(new LabelValueBean(addr.getHostAddress(), addr.getHostAddress()));
            }
        }
    } catch (Throwable t) {
        log.error("Failed to list network interfaces.", t);
    }
    return l;
}

From source file:com.ery.ertc.estorm.util.DNS.java

/**
 * Returns all the IPs associated with the provided interface, if any, in textual form.
 * /*from   w ww .  ja v  a2s  .c o m*/
 * @param strInterface
 *            The name of the network interface or subinterface to query (eg eth0 or eth0:0) or the string "default"
 * @param returnSubinterfaces
 *            Whether to return IPs associated with subinterfaces of the given interface
 * @return A string vector of all the IPs associated with the provided interface
 * @throws UnknownHostException
 *             If an UnknownHostException is encountered in querying the default interface or the given interface can not be found
 * 
 */
public static String[] getIPs(String strInterface, boolean returnSubinterfaces) throws UnknownHostException {
    if ("default".equals(strInterface)) {
        return new String[] { InetAddress.getLocalHost().getHostAddress() };
    }
    NetworkInterface netIf;
    try {
        netIf = NetworkInterface.getByName(strInterface);
        if (netIf == null) {
            netIf = getSubinterface(strInterface);
            if (netIf == null) {
                throw new UnknownHostException("Unknown interface " + strInterface);
            }
        }
    } catch (SocketException e) {
        LOG.warn("Unable to get IP for interface " + strInterface, e);
        return new String[] { InetAddress.getLocalHost().getHostAddress() };
    }

    // NB: Using a LinkedHashSet to preserve the order for callers
    // that depend on a particular element being 1st in the array.
    // For example, getDefaultIP always returns the first element.
    LinkedHashSet<InetAddress> allAddrs = new LinkedHashSet<InetAddress>();
    allAddrs.addAll(Collections.list(netIf.getInetAddresses()));
    if (!returnSubinterfaces) {
        allAddrs.removeAll(getSubinterfaceInetAddrs(netIf));
    }

    String ips[] = new String[allAddrs.size()];
    int i = 0;
    for (InetAddress addr : allAddrs) {
        ips[i++] = addr.getHostAddress();
    }
    return ips;
}

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

private void setDeviceIp() {
    try {//  w  w w  . j a  va  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:org.lnicholls.galleon.util.Tools.java

public static String getLocalIpAddress() {
    try {//from w w  w  .  j  av a  2s .c om
        for (Enumeration interfaceEnum = NetworkInterface.getNetworkInterfaces(); interfaceEnum
                .hasMoreElements();) {
            NetworkInterface ni = (NetworkInterface) interfaceEnum.nextElement();
            Enumeration inetAddresses = ni.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
                if (inetAddress.getHostAddress().startsWith("192"))
                    return inetAddress.getHostAddress();
            }
        }
        return InetAddress.getLocalHost().getHostAddress();
    } catch (Exception ex) {
        Tools.logException(Tools.class, ex);
    }
    return "127.0.0.1";
}

From source file:com.buaa.cfs.net.DNS.java

/**
 * Returns all the IPs associated with the provided interface, if any, in textual form.
 *
 * @param strInterface        The name of the network interface or sub-interface to query (eg eth0 or eth0:0) or the
 *                            string "default"
 * @param returnSubinterfaces Whether to return IPs associated with subinterfaces of the given interface
 *
 * @return A string vector of all the IPs associated with the provided interface. The local host IP is returned if
 * the interface name "default" is specified or there is an I/O error looking for the given interface.
 *
 * @throws UnknownHostException If the given interface is invalid
 *//*  w  w  w.  j av a2  s  . c om*/
public static String[] getIPs(String strInterface, boolean returnSubinterfaces) throws UnknownHostException {
    if ("default".equals(strInterface)) {
        return new String[] { cachedHostAddress };
    }
    NetworkInterface netIf;
    try {
        netIf = NetworkInterface.getByName(strInterface);
        if (netIf == null) {
            netIf = getSubinterface(strInterface);
        }
    } catch (SocketException e) {
        LOG.warn("I/O error finding interface " + strInterface + ": " + e.getMessage());
        return new String[] { cachedHostAddress };
    }
    if (netIf == null) {
        throw new UnknownHostException("No such interface " + strInterface);
    }

    // NB: Using a LinkedHashSet to preserve the order for callers
    // that depend on a particular element being 1st in the array.
    // For example, getDefaultIP always returns the first element.
    LinkedHashSet<InetAddress> allAddrs = new LinkedHashSet<InetAddress>();
    allAddrs.addAll(Collections.list(netIf.getInetAddresses()));
    if (!returnSubinterfaces) {
        allAddrs.removeAll(getSubinterfaceInetAddrs(netIf));
    }

    String ips[] = new String[allAddrs.size()];
    int i = 0;
    for (InetAddress addr : allAddrs) {
        ips[i++] = addr.getHostAddress();
    }
    return ips;
}

From source file:org.lnicholls.galleon.util.Tools.java

public static boolean isLocalAddress(String address) {
    if (address != null) {
        try {//from  w w w . j a v a  2s.  com
            for (Enumeration interfaceEnum = NetworkInterface.getNetworkInterfaces(); interfaceEnum
                    .hasMoreElements();) {
                NetworkInterface ni = (NetworkInterface) interfaceEnum.nextElement();
                Enumeration inetAddresses = ni.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
                    if (inetAddress.getHostAddress().equals(address))
                        return true;
                }
            }
        } catch (Exception ex) {
            Tools.logException(Tools.class, ex);
        }
    }
    return false;
}

From source file:org.lnicholls.galleon.util.Tools.java

public static boolean isLocal(String address) {
    if (address != null) {
        try {//from www .j av a  2  s .c o m
            if (address.equals("127.0.0.1"))
                return true;
            String prefix = null;
            StringTokenizer tokenizer = new StringTokenizer(address, ".");
            if (tokenizer.hasMoreTokens())
                prefix = tokenizer.nextToken();
            if (prefix != null) {
                for (Enumeration interfaceEnum = NetworkInterface.getNetworkInterfaces(); interfaceEnum
                        .hasMoreElements();) {
                    NetworkInterface ni = (NetworkInterface) interfaceEnum.nextElement();
                    Enumeration inetAddresses = ni.getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
                        if (inetAddress.getHostAddress().startsWith(prefix))
                            return true;
                    }
                }
            }
        } catch (Exception ex) {
            Tools.logException(Tools.class, ex);
        }
    }
    return false;
}

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 {/* w  w  w . ja  v  a  2s  .  c  o  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)");
}