Example usage for java.net NetworkInterface getName

List of usage examples for java.net NetworkInterface getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Get the name of this network interface.

Usage

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 ww  .  j a v a 2s.c om*/
}

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());
        }//  w ww  . ja  v  a2s  . c  o  m
    }
}

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

/**
 * @param args/*w ww .  ja  va 2  s. co  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 void printParameter(NetworkInterface ni) throws SocketException {
    System.out.println(" Name = " + ni.getName());
    System.out.println(" Display Name = " + ni.getDisplayName());
    System.out.println(" Is up = " + ni.isUp());
    System.out.println(" Support multicast = " + ni.supportsMulticast());
    System.out.println(" Is loopback = " + ni.isLoopback());
    System.out.println(" Is virtual = " + ni.isVirtual());
    System.out.println(" Is point to point = " + ni.isPointToPoint());
    System.out.println(" Hardware address = " + ni.getHardwareAddress());
    System.out.println(" MTU = " + ni.getMTU());

    System.out.println("\nList of Interface Addresses:");
    List<InterfaceAddress> list = ni.getInterfaceAddresses();
    Iterator<InterfaceAddress> it = list.iterator();

    while (it.hasNext()) {
        InterfaceAddress ia = it.next();
        System.out.println(" Address = " + ia.getAddress());
        System.out.println(" Broadcast = " + ia.getBroadcast());
        System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
        System.out.println("");
    }//from   w w  w . jav  a2 s .  c  om
}

From source file:Main.java

/** Finds a network interface of sub-interface with the given name */
public static NetworkInterface getByName(String name) throws SocketException {
    if (name == null)
        return null;
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface intf = en.nextElement();
        if (intf.getName().equals(name))
            return intf;
        Enumeration<NetworkInterface> en2 = intf.getSubInterfaces();
        while (en2.hasMoreElements()) {
            NetworkInterface intf2 = en2.nextElement();
            if (intf2.getName().equals(name)) {
                return intf2;
            }//w  w w .ja  v  a 2  s .  c o m
        }
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    try {/*from   ww  w.  j a v  a  2  s . c  om*/
        String ipv4;

        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            if (ni.getName().toLowerCase().contains("usbnet"))
                continue;
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }

        }

    } catch (SocketException ex) {
        Log.d("socket_err", ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/* w ww  . java  2 s . c o m*/
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface intf = en.nextElement();
            String name = intf.getName();
            if (name.compareTo("eth0") == 0 || name.compareTo("wlan0") == 0) {
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress.getClass() == Inet4Address.class) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("MY", ex.toString());
    }
    return null;
}

From source file:Main.java

public static HashMap<String, ArrayList<String>> getLocalIpAddresses() {
    try {//ww  w. ja  v a 2s .c o  m
        HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();

            String name = intf.getName();
            ArrayList<String> addresses = new ArrayList<String>();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    addresses.add(inetAddress.getHostAddress().toString());
                }
            }
            result.put(name, addresses);
        }
        return result;
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    System.out.printf("Display name: %s%n", netint.getDisplayName());
    System.out.printf("Name: %s%n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        System.out.printf("InetAddress: %s%n", inetAddress);
    }/*  ww  w  . j  a v a 2s.  co  m*/
    System.out.printf("%n");
}

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

public static String getHostAddress() {
    String hostAddress = "";
    try {//from  w w w  . jav a  2s  .  co m
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        for (; nics.hasMoreElements() && "".equals(hostAddress);) {
            NetworkInterface interfece = nics.nextElement();
            if (interfece.getName().toLowerCase().startsWith("lo")) {
                continue;
            }
            Enumeration<InetAddress> addrs = interfece.getInetAddresses();
            for (; addrs.hasMoreElements() && "".equals(hostAddress);) {
                InetAddress addr = addrs.nextElement();
                if (addr.getHostAddress().indexOf(":") > -1) {
                    continue;
                }
                hostAddress = addr.getHostAddress();
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    if (StringUtils.isBlank(hostAddress)) {
        hostAddress = "127.0.0.1";
    }
    return hostAddress;
}