Returns all network interfaces of this host. - Java Network

Java examples for Network:Host

Description

Returns all network interfaces of this host.

Demo Code


//package com.java2s;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getNetworkInterfaces());
    }/*from   w ww  .  j  a v a 2  s  . c o m*/

    /**
     * Returns all network interfaces of this host.
     * @return all network interfaces of this host
     */
    public static Collection<NetworkInterface> getNetworkInterfaces() {
        // get network interfaces
        ArrayList<NetworkInterface> result = new ArrayList<NetworkInterface>();
        Enumeration<NetworkInterface> ifs = null;
        try {
            ifs = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e1) {
            throw new IllegalStateException(
                    "Could not enumerate network interfaces!");
        }
        while (ifs.hasMoreElements()) {
            result.add(ifs.nextElement());
        }
        return result;
    }
}

Related Tutorials