get Network Interface List - Java Network

Java examples for Network:Network Interface

Description

get Network Interface List

Demo Code


//package com.java2s;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getNetworkInterfaceList());
    }//w  w  w. java  2s  .  c  om

    public static List<NetworkInterface> getNetworkInterfaceList() {
        List<NetworkInterface> networkInterfaceList = new ArrayList<NetworkInterface>();
        try {
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface
                    .getNetworkInterfaces(); interfaces.hasMoreElements();) {
                NetworkInterface networkInterface = interfaces
                        .nextElement();
                if ((networkInterface.isLoopback())
                        || (networkInterface.isVirtual())
                        || (!networkInterface.isUp())
                        || (networkInterface.isPointToPoint()))
                    continue;
                networkInterfaceList.add(networkInterface);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return networkInterfaceList;
    }
}

Related Tutorials