Java Network Interface Get getPublicInterface()

Here you can find the source of getPublicInterface()

Description

get Public Interface

License

Apache License

Declaration

public static NetworkInterface getPublicInterface() 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    public static NetworkInterface getPublicInterface() {
        Enumeration<NetworkInterface> nets;
        try {//from   w w w . j  av  a2s .  c  o m
            nets = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return null;
        }
        NetworkInterface netinf;
        while (nets.hasMoreElements()) {
            netinf = nets.nextElement();

            Enumeration<InetAddress> addresses = netinf.getInetAddresses();

            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (!address.isAnyLocalAddress() && !address.isMulticastAddress()
                        && !(address instanceof Inet6Address)) {
                    return netinf;
                }
            }
        }
        return null;
    }
}

Related

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