Example usage for java.net NetworkInterface isLoopback

List of usage examples for java.net NetworkInterface isLoopback

Introduction

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

Prototype


public boolean isLoopback() throws SocketException 

Source Link

Document

Returns whether a network interface is a loopback interface.

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public String getHostOrIP() {
    try {//  w ww. j  a  v a 2  s  .  c  o m
        InetAddress i = java.net.InetAddress.getLocalHost();
        if (i.isLoopbackAddress()) {
            Enumeration<NetworkInterface> nie = NetworkInterface.getNetworkInterfaces();
            while (nie.hasMoreElements()) {
                NetworkInterface current = nie.nextElement();
                if (!current.isLoopback()) {
                    Enumeration<InetAddress> iae = current.getInetAddresses();
                    while (iae.hasMoreElements()) {
                        InetAddress currentI = iae.nextElement();
                        if (!currentI.isLoopbackAddress()) {
                            return currentI.getHostAddress();
                        }
                    }
                }
            }
        }
        return i.getHostAddress();
    } catch (Throwable t) {
        com.codename1.io.Log.e(t);
        return null;
    }
}