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:eu.stratosphere.nephele.taskmanager.TaskManager.java

/**
 * Find out the TaskManager's own IP address.
 *//*from w ww .  j a  va  2s  . c  om*/
private InetAddress getTaskManagerAddress(InetSocketAddress jobManagerAddress) throws IOException {
    AddressDetectionState strategy = AddressDetectionState.ADDRESS;

    while (true) {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface n = e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = ee.nextElement();
                switch (strategy) {
                case ADDRESS:
                    if (hasCommonPrefix(jobManagerAddress.getAddress().getAddress(), i.getAddress())) {
                        if (tryToConnect(i, jobManagerAddress, strategy.getTimeout())) {
                            LOG.info("Determined " + i + " as the TaskTracker's own IP address");
                            return i;
                        }
                    }
                    break;
                case FAST_CONNECT:
                case SLOW_CONNECT:
                    boolean correct = tryToConnect(i, jobManagerAddress, strategy.getTimeout());
                    if (correct) {
                        LOG.info("Determined " + i + " as the TaskTracker's own IP address");
                        return i;
                    }
                    break;
                default:
                    throw new RuntimeException("Unkown address detection strategy: " + strategy);
                }
            }
        }
        // state control
        switch (strategy) {
        case ADDRESS:
            strategy = AddressDetectionState.FAST_CONNECT;
            break;
        case FAST_CONNECT:
            strategy = AddressDetectionState.SLOW_CONNECT;
            break;
        case SLOW_CONNECT:
            throw new RuntimeException("The TaskManager failed to detect its own IP address");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Defaulting to detection strategy " + strategy);
        }
    }
}

From source file:edu.stanford.epad.epadws.processing.pipeline.task.EpadStatisticsTask.java

public static String getIPAddress() {
    String ip = "";
    String ipi = "";
    Enumeration e;//from w  ww  .  jav  a 2 s  .c om
    try {
        e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                ipi = i.getHostAddress();
                if (!ipi.startsWith("127") && !ipi.startsWith("192") && !ipi.startsWith("172")
                        && !ipi.startsWith("10.") && !ipi.startsWith("0:"))
                    ip = ipi;
            }
        }
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
    if (ip.length() == 0)
        return ipi;
    else
        return ip;
}

From source file:cn.apputest.ctria.section2.Lucha2Activity.java

public String getLocalIpAddressGPRS() {
    try {/*from  w w w  .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()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreferenceIpAddress", "ex.toString()");
    }
    return null;
}

From source file:org.votingsystem.util.HttpHelper.java

public static String getLocalIP() throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            if (inetAddress.isSiteLocalAddress()) {
                String inetAddressStr = inetAddress.toString();
                while (inetAddressStr.startsWith("/"))
                    inetAddressStr = inetAddressStr.substring(1);
                return inetAddressStr;
            }//ww w  . jav a  2 s .c o  m

        }
    }
    return null;
}

From source file:com.material.katha.wifidirectmp3.DeviceDetailFragment.java

public byte[] getLocalIPAddress() {
    try {//from   w w w . j  av  a2s . 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()) {
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    } catch (NullPointerException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    }
    return null;
}

From source file:org.apache.axis2.util.Utils.java

/**
* Returns the ip address to be used for the replyto epr
* CAUTION://  w  w  w  .j a v  a 2 s .  c  om
* This will go through all the available network interfaces and will try to return an ip address.
* First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found
* then this will return this will return 127.0.0.1.
* This will <b>not<b> consider IPv6 addresses.
* <p/>
* TODO:
* - Improve this logic to genaralize it a bit more
* - Obtain the ip to be used here from the Call API
*
* @return Returns String.
* @throws java.net.SocketException
 */
public static String getIpAddress() throws SocketException {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    String address = "127.0.0.1";

    while (e.hasMoreElements()) {
        NetworkInterface netface = (NetworkInterface) e.nextElement();
        Enumeration addresses = netface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress ip = (InetAddress) addresses.nextElement();
            if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
                return ip.getHostAddress();
            }
        }
    }

    return address;
}

From source file:com.googlecode.networklog.NetworkLog.java

public static void getLocalIpAddresses() {
    MyLog.d("getLocalIpAddresses");
    localIpAddrs = new ArrayList<String>();

    try {// ww w .j  ava2  s  .c o  m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            MyLog.d("Network interface found: " + intf.toString());

            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                MyLog.d("InetAddress: " + inetAddress.toString());

                if (!inetAddress.isLoopbackAddress()) {
                    MyLog.d("Adding local IP address: [" + inetAddress.getHostAddress().toString() + "]");
                    localIpAddrs.add(inetAddress.getHostAddress().toString());
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("NetworkLog", ex.toString());
    }
}

From source file:com.android.development.Connectivity.java

private void onBoundSocketRequest() {
    NetworkInterface networkInterface = null;
    try {/* w  w w  .  j av  a  2  s . c  om*/
        networkInterface = NetworkInterface.getByName("rmnet0");
    } catch (Exception e) {
        Log.e(TAG, "exception getByName: " + e);
        return;
    }
    if (networkInterface == null) {
        try {
            Log.d(TAG, "getting any networkInterface");
            networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
        } catch (Exception e) {
            Log.e(TAG, "exception getting any networkInterface: " + e);
            return;
        }
    }
    if (networkInterface == null) {
        Log.e(TAG, "couldn't find a local interface");
        return;
    }
    Enumeration inetAddressess = networkInterface.getInetAddresses();
    while (inetAddressess.hasMoreElements()) {
        Log.d(TAG, " addr:" + ((InetAddress) inetAddressess.nextElement()));
    }
    InetAddress local = null;
    InetAddress remote = null;
    try {
        local = networkInterface.getInetAddresses().nextElement();
    } catch (Exception e) {
        Log.e(TAG, "exception getting local InetAddress: " + e);
        return;
    }
    try {
        remote = InetAddress.getByName("www.flickr.com");
    } catch (Exception e) {
        Log.e(TAG, "exception getting remote InetAddress: " + e);
        return;
    }
    Log.d(TAG, "remote addr =" + remote);
    Log.d(TAG, "local addr =" + local);
    Socket socket = null;
    try {
        socket = new Socket(remote, 80, local, 6000);
    } catch (Exception e) {
        Log.e(TAG, "Exception creating socket: " + e);
        return;
    }
    try {
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println("Hi flickr");
    } catch (Exception e) {
        Log.e(TAG, "Exception writing to socket: " + e);
        return;
    }
}

From source file:com.entertailion.java.fling.FlingFrame.java

private InterfaceAddress getPreferredInetAddress(String prefix) {
    InterfaceAddress selectedInterfaceAddress = null;
    try {// www  .ja  v a 2s  .  co m
        Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces();

        while (list.hasMoreElements()) {
            NetworkInterface iface = list.nextElement();
            if (iface == null)
                continue;
            Log.d(LOG_TAG, "interface=" + iface.getName());
            Iterator<InterfaceAddress> it = iface.getInterfaceAddresses().iterator();
            while (it.hasNext()) {
                InterfaceAddress interfaceAddress = it.next();
                if (interfaceAddress == null)
                    continue;
                InetAddress address = interfaceAddress.getAddress();
                Log.d(LOG_TAG, "address=" + address);
                if (address instanceof Inet4Address) {
                    // Only pick an interface that is likely to be on the
                    // same subnet as the selected ChromeCast device
                    if (address.getHostAddress().toString().startsWith(prefix)) {
                        return interfaceAddress;
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    return selectedInterfaceAddress;
}