Java Network Interface Get getNetworkInterfaces()

Here you can find the source of getNetworkInterfaces()

Description

get Network Interfaces

License

Apache License

Declaration

private static List<NetworkInterface> getNetworkInterfaces() throws SocketException 

Method Source Code

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

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;

public class Main {
    private static List<NetworkInterface> getNetworkInterfaces() throws SocketException {
        List<NetworkInterface> networkInterfaces = new ArrayList<>();
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            networkInterfaces.add(networkInterface);
            Enumeration<NetworkInterface> subInterfaces = networkInterface.getSubInterfaces();
            if (subInterfaces.hasMoreElements()) {
                while (subInterfaces.hasMoreElements()) {
                    networkInterfaces.add(subInterfaces.nextElement());
                }/*  w ww  .  j  av a  2s  .co m*/
            }
        }
        sortInterfaces(networkInterfaces);
        return networkInterfaces;
    }

    private static void sortInterfaces(List<NetworkInterface> interfaces) {
        Collections.sort(interfaces, new Comparator<NetworkInterface>() {
            @Override
            public int compare(NetworkInterface o1, NetworkInterface o2) {
                return Integer.compare(o1.getIndex(), o2.getIndex());
            }
        });
    }
}

Related

  1. getNetworkInterfaces()
  2. getNetworkInterfaces()
  3. getNetworkInterfaces()
  4. getNetworkInterfaces()
  5. getNetworkInterfaces()
  6. getNIOMulticastMethod()
  7. getNonLoopbackLocalAdresses()
  8. getNonLoopbackNetworkInterface()
  9. getPublicInterface()