Java Network Interface Get getAllLocalInterfaces()

Here you can find the source of getAllLocalInterfaces()

Description

Get all the interfaces which are not loopback ones

License

Open Source License

Declaration

public static Set<NetworkInterface> getAllLocalInterfaces() 

Method Source Code

//package com.java2s;
/**/*  w  w w  .j  a  v a  2 s .  c  o  m*/
 * PETALS - PETALS Services Platform. Copyright (c) 2008 EBM Websourcing,
 * http://www.ebmwebsourcing.com/
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version. This library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * -------------------------------------------------------------------------
 * $Id$
 * -------------------------------------------------------------------------
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**
     * Get all the interfaces which are not loopback ones
     * 
     * @return
     */
    public static Set<NetworkInterface> getAllLocalInterfaces() {
        Set<NetworkInterface> result = new HashSet<NetworkInterface>();
        try {
            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                NetworkInterface ni = e.nextElement();

                Enumeration<InetAddress> set = ni.getInetAddresses();
                // assume that all the inet addresses of the interface are all
                // loopback or not all loopback...
                if (set.hasMoreElements()) {
                    InetAddress address = set.nextElement();
                    if (!address.isLoopbackAddress()) {
                        result.add(ni);
                    }
                }
            }
        } catch (SocketException e) {
        }
        return result;
    }

    /**
     * Test if the given address is a loopback one ie localhost or 127.0.0.1
     * 
     * @param addr
     * @return
     */
    public static boolean isLoopbackAddress(InetAddress addr) {
        return addr.isLoopbackAddress();
        // JAVA 6
        // boolean result = false;
        // Set<NetworkInterface> itfs = getAllInterfaces();
        // for (NetworkInterface networkInterface : itfs) {
        // try {
        // if (networkInterface.isLoopback()) {
        // Enumeration<InetAddress> e = networkInterface.getInetAddresses();
        // while (e.hasMoreElements()) {
        // InetAddress elem = e.nextElement();
        // if (elem.equals(addr)) {
        // return true;
        // }
        // }
        // }
        // } catch (SocketException e) {
        // }
        // }
        // return result;
    }
}

Related

  1. getAllAvailableInterfaces()
  2. getAllAvailableInterfaces()
  3. getAllAvailableInterfaces()
  4. getAllInterfaces()
  5. getAllInterfaces()
  6. getAllLocalUsingNetworkInterface()
  7. getAvailableNetworkInterface()
  8. getBroadcast()
  9. getCustomMACFormat(NetworkInterface inte)