Java Network Interface Get getLoopbackNIF()

Here you can find the source of getLoopbackNIF()

Description

Get a loopback NIF.

License

Open Source License

Return

a loopback NIF, null if not found.

Declaration


public static NetworkInterface getLoopbackNIF() 

Method Source Code

//package com.java2s;
/*/* w  w  w.  j  av a 2 s  .c o m*/
 * Copyright (c) 2004 by Cosylab
 *
 * The full license specifying the redistribution, modification, usage and other
 * rights and obligations is included with the distribution of this project in
 * the file "LICENSE-CAJ". If the license is not included visit Cosylab web site,
 * <http://www.cosylab.com>.
 *
 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
 * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
 * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
 * OR REDISTRIBUTION OF THIS SOFTWARE.
 */

import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    /**
     * Get a loopback NIF.
     * @return a loopback NIF, <code>null</code> if not found.
     */
    // TODO support case with multiple loopback NIFs
    public static NetworkInterface getLoopbackNIF() {

        Enumeration<NetworkInterface> nets;
        try {
            nets = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException se) {
            return null;
        }

        while (nets.hasMoreElements()) {
            NetworkInterface net = nets.nextElement();
            try {
                if (net.isUp() && net.isLoopback())
                    return net;
            } catch (Throwable th) {
                // some methods throw exceptions, some return null (and they shouldn't)
                // noop, skip that interface
            }
        }

        return null;
    }
}

Related

  1. getLocalInterfaces()
  2. getLocalMac()
  3. getLocalNetworkInterface()
  4. getLoopbackInterface()
  5. getLoopbackInterfaceName()
  6. getMac()
  7. getMacBytes()
  8. getNetworkErrorMessage(Throwable e)
  9. getNetworkInterface()