Java Network Interface Get getNonLoopbackNetworkInterface()

Here you can find the source of getNonLoopbackNetworkInterface()

Description

get Non Loopback Network Interface

License

Open Source License

Declaration

private static NetworkInterface getNonLoopbackNetworkInterface() throws SocketException 

Method Source Code

//package com.java2s;

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

public class Main {
    private static NetworkInterface getNonLoopbackNetworkInterface() throws SocketException {
        // We need to iterate over all NIC's machine has because stupid ubuntu does not assign
        // MAC address to default loopback interface...
        Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
        while (b.hasMoreElements()) {
            NetworkInterface networkInterface = b.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress address = inetAddresses.nextElement();
                if (!address.isLoopbackAddress())
                    return networkInterface;
            }//w  ww.j  av a 2s.c o  m
        }

        // Means we haven't found any non loopback interfaces. Bummer, return empty handed.
        return null;
    }
}

Related

  1. getNetworkInterfaces()
  2. getNetworkInterfaces()
  3. getNetworkInterfaces()
  4. getNIOMulticastMethod()
  5. getNonLoopbackLocalAdresses()
  6. getPublicInterface()
  7. getUsualNetworkInterfaces()
  8. isActive(String niName)
  9. isNixMashPC()