Java IP Address Get getIpByNetworkInterfaceName(String name)

Here you can find the source of getIpByNetworkInterfaceName(String name)

Description

get Ip By Network Interface Name

License

Open Source License

Declaration

public static List<String> getIpByNetworkInterfaceName(String name) 

Method Source Code

//package com.java2s;
/**/*from   ww w.  j  a  va2s .  co  m*/
 * 
 * @Title NetworkUtils.java
 * @Description ?AppStatus???????????????
 * Copyright: Copyright (c) 2013, Opzoon and/or its affiliates. All rights reserved.
 * OPZOON PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @author NY
 * @date 2013-11-30 ????11:02:07
 * 
 */

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {

    public static List<String> getIpByNetworkInterfaceName(String name) {
        List<String> resultList = null;
        NetworkInterface networkInterface = null;
        for (int i = 0; i < getUsualNetworkInterfaces().size(); i++) {
            networkInterface = getUsualNetworkInterfaces().get(i);
            if (name.equals(networkInterface.getName())) {
                resultList = getInetAddressList(networkInterface);
            }
        }
        return resultList;
    }

    public static List<NetworkInterface> getUsualNetworkInterfaces() {
        List<NetworkInterface> resultList = new ArrayList<NetworkInterface>();
        Enumeration<NetworkInterface> allNetInterfaces;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }
                resultList.add(netInterface);
            }
        } catch (SocketException e) {
            System.out
                    .println("Some error occured during execute method getServerUsualInterface, see details:" + e);
        }
        return resultList;
    }

    public static List<String> getInetAddressList(NetworkInterface netInterface) {
        List<String> inetAddressList = new ArrayList<String>();
        Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
        InetAddress ip = null;
        while (addresses.hasMoreElements()) {
            ip = (InetAddress) addresses.nextElement();
            if (ip != null && ip instanceof Inet4Address) {
                inetAddressList.add(ip.getHostAddress());
            }
        }
        return inetAddressList;
    }
}

Related

  1. getIpByHost(String hostName)
  2. getIpByHost(String hostName)
  3. getIpByHost(String hostName)
  4. getIPByInterfaceName(String name)
  5. getIpByName(String name)
  6. getIPFromHash(final long ipHash)
  7. getIPFromInterface(String ni)
  8. getIPFromNetworkInterface()
  9. getIpHostnameLocal()