Java Network Interface Check isInterfaceLoopback(NetworkInterface iface)

Here you can find the source of isInterfaceLoopback(NetworkInterface iface)

Description

Determines whether or not the iface interface is a loopback interface.

License

LGPL

Parameter

Parameter Description
iface the inteface that we'd like to determine as loopback or not.

Return

true if iface contains at least one loopback address and false otherwise.

Declaration

public static boolean isInterfaceLoopback(NetworkInterface iface) 

Method Source Code

//package com.java2s;
/*/* w w  w.  j a  v a  2  s  .c o  m*/
 * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
 * Maintained by the SIP Communicator community (http://sip-communicator.org).
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.lang.reflect.*;
import java.net.*;
import java.util.*;

public class Main {
    /**
     * Determines whether or not the <tt>iface</tt> interface is a loopback
     * interface. We use this method as a replacement to the
     * <tt>NetworkInterface.isLoopback()</tt> method that only comes with
     * java 1.6.
     *
     * @param iface the inteface that we'd like to determine as loopback or not.
     *
     * @return true if <tt>iface</tt> contains at least one loopback address
     * and <tt>false</tt> otherwise.
     */
    public static boolean isInterfaceLoopback(NetworkInterface iface) {
        try {
            Method method = iface.getClass().getMethod("isLoopback");

            return ((Boolean) method.invoke(iface, new Object[] {}))
                    .booleanValue();
        } catch (Throwable t) {
            //apparently we are not running in a JVM that supports the
            //is Loopback method. we'll try another approach.
        }
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

        return addresses.hasMoreElements()
                && addresses.nextElement().isLoopbackAddress();
    }
}

Related

  1. describeInterface(NetworkInterface subIf, boolean subinterface)
  2. dumpLocalNetworkInfo()
  3. hasNetworkAccess()
  4. isCIDR(String network)
  5. isHostInNetworkCard(String host)
  6. isInterfaceUp(NetworkInterface iface)
  7. isLoopbackNetworkInterface( NetworkInterface anInterface)
  8. isNetworkAvailable()
  9. isNetworkException(Exception e)