Java Network Interface Get getNetworkInterfaces()

Here you can find the source of getNetworkInterfaces()

Description

get Network Interfaces

License

Open Source License

Declaration

public static Collection<String> getNetworkInterfaces() 

Method Source Code


//package com.java2s;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Vector;

public class Main {
    public static Collection<String> getNetworkInterfaces() {
        Collection<String> itfs = getNetworkInterfaces(true);
        return itfs;
    }/*  w ww  .java  2 s. c o m*/

    public static Collection<String> getNetworkInterfaces(boolean includeLoopback) {
        Vector<String> itfs = new Vector<String>();
        try {
            Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
            while (nis.hasMoreElements()) {
                NetworkInterface ni = nis.nextElement();
                try {
                    if (ni.isLoopback() && !includeLoopback)
                        continue;
                } catch (Exception e) {
                    continue;
                }

                Enumeration<InetAddress> adrs = ni.getInetAddresses();
                while (adrs.hasMoreElements()) {
                    InetAddress addr = adrs.nextElement();
                    if (addr instanceof Inet4Address)
                        itfs.add(addr.getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return itfs;
    }
}

Related

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