Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

In this page you can find the example usage for java.net NetworkInterface getNetworkInterfaces.

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:Main.java

static public void main(String args[]) throws Exception {
    Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) interfaces.nextElement();
        System.out.println(ni);//from   w  w  w.jav a 2  s  . co m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface nif = e.nextElement();
        System.out.println("Name: " + nif.getName() + ",  Supports Multicast: " + nif.supportsMulticast()
                + ", isUp(): " + nif.isUp());
    }/* w w w.  j  a va  2  s  .c o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface ni = en.nextElement();
        printParameter(ni);// w w  w  .  jav a2s  . co  m
    }
}

From source file:NetworkParameterDemo.java

public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface ni = en.nextElement();
        printParameter(ni);/*w  ww  .j  a va  2 s .co m*/

    }
}

From source file:Main.java

public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
        displayInterfaceInformation(netint);
    }//from  w ww .j  av  a 2 s .  co m
}

From source file:com.netsteadfast.greenstep.util.HostUtils.java

public static void main(String args[]) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
    for (; nics.hasMoreElements();) {
        System.out.println("--------------------------------------------------------------------------");
        NetworkInterface interfece = nics.nextElement();
        System.out.println(interfece.getName());
        Enumeration<InetAddress> addrs = interfece.getInetAddresses();
        for (; addrs.hasMoreElements();) {
            InetAddress addr = addrs.nextElement();
            System.out.println(addr.getHostAddress());
        }/* www  .j a  v a2s .  com*/
    }
}

From source file:uk.ac.horizon.ubihelper.j2se.Server.java

/**
 * @param args//ww w.  j ava 2 s  . c o  m
 */
public static void main(String[] args) {
    InetAddress bestAddress = null;
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            if (!ni.isUp() || ni.isVirtual() || ni.isLoopback())
                continue;
            logger.info("Has interface " + ni.getName() + ": " + ni.getDisplayName());
            Enumeration<InetAddress> as = ni.getInetAddresses();
            while (as.hasMoreElements()) {
                InetAddress a = as.nextElement();
                if (a instanceof Inet4Address) {
                    Inet4Address ip = (Inet4Address) a;
                    logger.info("- IPv4 address " + ip.getHostAddress());
                    if (ip.isSiteLocalAddress()) {
                        logger.info("-- site local!");
                        if (bestAddress == null)
                            bestAddress = ip;
                    } else
                        bestAddress = ip;
                }
            }
        }
    } catch (Exception e) {
        logger.severe("Could not list NetworkInterfaces: " + e);
        System.exit(-1);
    }
    if (bestAddress == null) {
        logger.severe("Could not find an IP address to bind to - using localhost");
        try {
            bestAddress = InetAddress.getLocalHost();
        } catch (Exception e) {
            logger.severe("Could not get localhost address: " + e);
            System.exit(-2);
        }
    }
    int port = 0;
    if (args.length == 1) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException nfe) {
            System.err.println("Usage: <server-port>");
            System.exit(-3);
        }
    }
    Server server = new Server();
    server.init(bestAddress, port);
}

From source file:Main.java

public static String localIPAddress() {
    try {/* w ww  . java  2  s  .c  o  m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    String ipAddress = inetAddress.getHostAddress().toString();
                    int p = ipAddress.indexOf("%");
                    if (p > 0)
                        ipAddress = ipAddress.substring(0, p);
                    //if (MagicBooleans.trace_mode) System.out.println("--> localIPAddress = "+ipAddress);
                    return ipAddress;
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return "127.0.0.1";
}

From source file:Main.java

public static String getHostIp() {
    try {/*from w ww  . jav a  2s  .  c o m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements();) {
                InetAddress inetAddress = ipAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

public static String getIpAddress() {
    try {/*from  ww  w . j a v  a  2  s.  c om*/
        Enumeration en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {
            NetworkInterface ex = (NetworkInterface) en.nextElement();
            Enumeration enumIpAddr = ex.getInetAddresses();

            while (enumIpAddr.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }

        return null;
    } catch (SocketException var4) {
        var4.printStackTrace();
        return null;
    }
}