Example usage for java.net NetworkInterface getInetAddresses

List of usage examples for java.net NetworkInterface getInetAddresses

Introduction

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

Prototype

public Enumeration<InetAddress> getInetAddresses() 

Source Link

Document

Get an Enumeration with all or a subset of the InetAddresses bound to this network interface.

Usage

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    try {//from  www  . ja va2 s  . co m
        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:com.seadee.degree.service.NetworkStateReceiver.java

public static String getLocalIpAddress(Context context) {
    final String IPTAG = "getLocalIpAddress";
    String nullAddress = "0.0.0.0";
    try {/*from ww  w  .ja  va2 s .c  om*/
        if (SettingVarible.networkstate != SettingVarible.NETWORKSTATE.NONETWORK) {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    String ip_address = inetAddress.getHostAddress();
                    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address))
                        return ip_address;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(IPTAG, ex.toString());
    }
    return nullAddress;
}

From source file:org.wso2.bam.integration.tests.agents.KPIAgent.java

private static InetAddress getLocalHostAddress() throws SocketException {
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress addr = addresses.nextElement();
            if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                return addr;
            }//from   ww w  .j a v a2s.c o  m
        }
    }

    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);
    }/*from  www  . j  av a2  s  .co m*/

    System.out.printf("Parent: %s%n", netint.getParent());
    System.out.printf("Up? %s%n", netint.isUp());
    System.out.printf("Loopback? %s%n", netint.isLoopback());
    System.out.printf("PointToPoint? %s%n", netint.isPointToPoint());
    System.out.printf("Supports multicast? %s%n", netint.isVirtual());
    System.out.printf("Virtual? %s%n", netint.isVirtual());
    System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress()));
    System.out.printf("MTU: %s%n", netint.getMTU());

    List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses();
    for (InterfaceAddress addr : interfaceAddresses) {
        System.out.printf("InterfaceAddress: %s%n", addr.getAddress());
    }
    System.out.printf("%n");
    Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces();
    for (NetworkInterface networkInterface : Collections.list(subInterfaces)) {
        System.out.printf("%nSubInterface%n");
        displayInterfaceInformation(networkInterface);
    }
    System.out.printf("%n");
}

From source file:org.sonar.application.cluster.ClusterProperties.java

private static List<String> findAllLocalIPs() throws SocketException {
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    List<String> localInterfaces = new ArrayList<>();

    while (netInterfaces.hasMoreElements()) {
        NetworkInterface networkInterface = netInterfaces.nextElement();
        Enumeration<InetAddress> ips = networkInterface.getInetAddresses();
        while (ips.hasMoreElements()) {
            InetAddress ip = ips.nextElement();
            localInterfaces.add(ip.getHostAddress());
        }/*  w  ww .j a  v a2 s.com*/
    }
    return localInterfaces;
}

From source file:com.jean.farCam.SettingsActivity.java

public static String getIPAddress() {
    try {/*ww  w . j ava  2  s .  c o m*/
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(sAddr)) {
                        return sAddr;
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    return "";
}

From source file:com.frame.network.utils.NetworkUtil.java

public static String getLocalIpv4Address() {
    try {//from   w  ww  . j a v  a  2s.  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()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*from   ww w .  ja  va  2  s .co  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:net.sf.jasperreports.phantomjs.InetUtil.java

public static Inet4Address getIPv4Loopback() {
    InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
    //the phantomjs web server module only works with IPv4
    if (loopbackAddress instanceof Inet4Address) {
        return (Inet4Address) loopbackAddress;
    }/*from  w ww. j av  a2 s.  c o m*/

    try {
        InetAddress[] addresses = InetAddress.getAllByName(loopbackAddress.getHostName());
        for (InetAddress inetAddress : addresses) {
            if (inetAddress instanceof Inet4Address) {
                return (Inet4Address) inetAddress;
            }
        }
    } catch (UnknownHostException e) {
        log.warn("Error while determining loopback addresses for " + loopbackAddress.getHostName(), e);
    }

    try {
        //keep looking for a IPv4 loopback address
        for (Enumeration<NetworkInterface> itfs = NetworkInterface.getNetworkInterfaces(); itfs
                .hasMoreElements();) {
            NetworkInterface itf = itfs.nextElement();
            if (itf.isLoopback()) {
                for (Enumeration<InetAddress> addresses = itf.getInetAddresses(); addresses
                        .hasMoreElements();) {
                    InetAddress address = addresses.nextElement();
                    if (address instanceof Inet4Address) {
                        return (Inet4Address) address;
                    }
                }
            }
        }
    } catch (SocketException e) {
        log.warn("Error while listing network interfaces", e);
    }

    return null;
}

From source file:net.sourceforge.subsonic.util.Util.java

/**
 * Returns the local IP address.//from w ww.  ja v a 2s .  co  m
 * @return The local IP, or the loopback address (127.0.0.1) if not found.
 */
public static String getLocalIpAddress() {
    try {

        // Try the simple way first.
        InetAddress address = InetAddress.getLocalHost();
        if (!address.isLoopbackAddress()) {
            return address.getHostAddress();
        }

        // Iterate through all network interfaces, looking for a suitable IP.
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                    return addr.getHostAddress();
                }
            }
        }

    } catch (Throwable x) {
        LOG.warn("Failed to resolve local IP address.", x);
    }

    return "127.0.0.1";
}