当前位置: 首页>>代码示例>>Java>>正文


Java NetworkInterface.getInetAddresses方法代码示例

本文整理汇总了Java中java.net.NetworkInterface.getInetAddresses方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getInetAddresses方法的具体用法?Java NetworkInterface.getInetAddresses怎么用?Java NetworkInterface.getInetAddresses使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.NetworkInterface的用法示例。


在下文中一共展示了NetworkInterface.getInetAddresses方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getLocalIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Get local IP address
 *
 * @return ip address
 */
static String getLocalIpAddress() {
    String ip = "127.0.0.1";
    try {
        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()
                        && !inetAddress.isLinkLocalAddress()) {
                    ip = inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return ip;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:26,代码来源:PrefStore.java

示例2: getIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * 获取本机IP地址
 *
 * @return null:没有网络连接
 */
public static String getIpAddress() {
    try {
        NetworkInterface nerworkInterface;
        InetAddress inetAddress;
        for (Enumeration<NetworkInterface> en
             = NetworkInterface.getNetworkInterfaces();
             en.hasMoreElements(); ) {
            nerworkInterface = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr
                 = nerworkInterface.getInetAddresses();
                 enumIpAddr.hasMoreElements(); ) {
                inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
        return null;
    } catch (SocketException ex) {
        ex.printStackTrace();
        return null;
    }
}
 
开发者ID:liying2008,项目名称:Simpler,代码行数:29,代码来源:NetWorkUtils.java

示例3: getInterfaceIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private static Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return OspfUtil.DEFAULTIP;
    }
    return ipAddress;
}
 
开发者ID:shlee89,项目名称:athena,代码行数:25,代码来源:OspfConfigUtil.java

示例4: getLocalMacAddressFromIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getLocalMacAddressFromIp() {
    String mac_s = "";
    try {
        byte[] mac;
        for (Enumeration<NetworkInterface> enNetI = NetworkInterface.getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
            NetworkInterface netI = enNetI.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = netI.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                    mac = netI.getHardwareAddress();
                    mac_s = byte2hex(mac);
                    return mac_s;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return mac_s;
}
 
开发者ID:jiajieshen,项目名称:AndroidDevSamples,代码行数:22,代码来源:NetworkUtil.java

示例5: getLocalIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * 根据网卡取本机配置的IP 如果是双网卡的,则取出外网IP
 * 
 * @return
 */
public static String getLocalIp() {
	String localip = null;// 返回它
	try {
		Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
		InetAddress ip = null;
		boolean finded = false;// 是否找到外网IP
		while (netInterfaces.hasMoreElements() && !finded) {
			NetworkInterface ni = netInterfaces.nextElement();
			Enumeration<InetAddress> address = ni.getInetAddresses();
			while (address.hasMoreElements()) {
				ip = address.nextElement();
				if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP
					localip = ip.getHostAddress();
				}
			}
		}
	} catch (SocketException e) {
		e.printStackTrace();
	}
	return localip;
}
 
开发者ID:langxianwei,项目名称:iot-plat,代码行数:27,代码来源:CommUtils.java

示例6: findLocalIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
private IpAddress findLocalIp(Collection<ControllerNode> controllerNodes) throws SocketException {
    Enumeration<NetworkInterface> interfaces =
            NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface iface = interfaces.nextElement();
        Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
            IpAddress ip = IpAddress.valueOf(inetAddresses.nextElement());
            if (controllerNodes.stream()
                    .map(ControllerNode::ip)
                    .anyMatch(nodeIp -> ip.equals(nodeIp))) {
                return ip;
            }
        }
    }
    throw new IllegalStateException("Unable to determine local ip");
}
 
开发者ID:shlee89,项目名称:athena,代码行数:18,代码来源:ClusterMetadataManager.java

示例7: GetIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
public String GetIpAddress() {
    StringBuilder ip = new StringBuilder();
    try {
        Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();

        while (enumNetworkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
            Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();

            while (enumInetAddress.hasMoreElements()) {
                InetAddress inetAddress = enumInetAddress.nextElement();

                if (inetAddress.isSiteLocalAddress()) {
                    ip.append("SiteLocalAddress: ").append(inetAddress.getHostAddress()).append("\n");
                }
            }
        }
    } catch (SocketException e) {
        Logger.getInstance().Error(TAG, e.toString());
        ip.append("Something Wrong! ").append(e.toString()).append("\n");
    }

    return ip.toString();
}
 
开发者ID:GuepardoApps,项目名称:LucaHome-AndroidApplication,代码行数:25,代码来源:NetworkController.java

示例8: getNetworkInterfaceAdresses

import java.net.NetworkInterface; //导入方法依赖的package包/类
public List<String> getNetworkInterfaceAdresses() {
    Enumeration<NetworkInterface> networkInterfaceEnumeration;
    List<String> networkInterfaceAddressesList = new ArrayList<>();
    try {
        networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
            while (inetAddressEnumeration.hasMoreElements()) {
                InetAddress inetAddress = inetAddressEnumeration.nextElement();
                if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                    networkInterfaceAddressesList.add(
                            String.format("%5s %s",
                                    networkInterface.getDisplayName(),
                                    inetAddress.getHostAddress()));
                }
            }
        }
    } catch (SocketException e) {
        Timber.e(e);
    }
    return networkInterfaceAddressesList;
}
 
开发者ID:leinardi,项目名称:androidthings-kuman-sm9,代码行数:24,代码来源:SystemHelper.java

示例9: getPublicIP4

import java.net.NetworkInterface; //导入方法依赖的package包/类
static public String getPublicIP4() {

        Enumeration<NetworkInterface> ifs;
        try {
            ifs = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
            return null;
        }

        while (ifs.hasMoreElements()) {
            NetworkInterface iface = ifs.nextElement();
            Enumeration<InetAddress> iad = iface.getInetAddresses();
            while (iad.hasMoreElements()) {
                InetAddress localIP = iad.nextElement();
                if (!localIP.isSiteLocalAddress() && !localIP.isLoopbackAddress()) {
                    // found an IPv4 address
                    if (localIP instanceof java.net.Inet4Address)
                        return localIP.getHostAddress();
                }
            }
        }
        return null;
    }
 
开发者ID:fast-data-transfer,项目名称:fdt,代码行数:25,代码来源:LocalHost.java

示例10: getDevicesIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * 获取设备IP
 *
 * @return
 */
public String getDevicesIp() {
    try {
        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) {
        ex.printStackTrace();
    }
    return null;
}
 
开发者ID:jopenbox,项目名称:android-lite-utils,代码行数:24,代码来源:DevicesUtils.java

示例11: getIpv4Address

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getIpv4Address() {
    try {
        for (Enumeration<NetworkInterface> enNetI = NetworkInterface.getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
            NetworkInterface netI = enNetI.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = netI.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "";
}
 
开发者ID:jiajieshen,项目名称:AndroidDevSamples,代码行数:17,代码来源:NetworkUtil.java

示例12: main

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
    Scanner s = new Scanner(new File(args[0]));
    try {
        while (s.hasNextLine()) {
            String link = s.nextLine();
            NetworkInterface ni = NetworkInterface.getByName(link);
            if (ni != null) {
                Enumeration<InetAddress> addrs = ni.getInetAddresses();
                while (addrs.hasMoreElements()) {
                    InetAddress addr = addrs.nextElement();
                    System.out.println(addr.getHostAddress());
                }
            }
        }
    } finally {
        s.close();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:ProbeIB.java

示例13: getLocalIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * get local ip address
 *
 * @return
 */
public static String getLocalIpAddress(Context context) {
    String localIp = "";
    try {
        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()) {
                    localIp = inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return localIp;
}
 
开发者ID:wang2016215,项目名称:Bing,代码行数:25,代码来源:NetworkUtil.java

示例14: getIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getIpAddress() {
    final Enumeration<NetworkInterface> nis;
    try {
        nis = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        return StringConstant.EMPTY;
    }

    NetworkInterface ni;
    Enumeration<InetAddress> eia;
    InetAddress ia;
    String ip;

    while (nis.hasMoreElements()) {
        ni = nis.nextElement();
        eia = ni.getInetAddresses();

        while (eia.hasMoreElements()) {
            ia = eia.nextElement();

            if (!ia.isLoopbackAddress()) {
                ip = ia.getHostAddress();

                if (!TextUtils.isEmpty(ip)) {
                    return parseIpAddress(ip);
                }
            }
        }
    }
    return StringConstant.EMPTY;
}
 
开发者ID:Tenor-Inc,项目名称:tenor-android-core,代码行数:32,代码来源:AbstractNetworkUtils.java

示例15: parseAdapterName

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Parse an adapter name string and return the matching address
 * 
 * @param adapter String
 * @return InetAddress
 * @exception InvalidConfigurationException
 */
protected final InetAddress parseAdapterName(String adapter)
  throws InvalidConfigurationException {

  NetworkInterface ni = null;
  
  try {
    ni = NetworkInterface.getByName( adapter);
  }
  catch (SocketException ex) {
    throw new InvalidConfigurationException( "Invalid adapter name, " + adapter);
  }
  
  if ( ni == null)
    throw new InvalidConfigurationException( "Invalid network adapter name, " + adapter);
  
  // Get the IP address for the adapter

  InetAddress adapAddr = null;
  Enumeration<InetAddress> addrEnum = ni.getInetAddresses();
  
  while ( addrEnum.hasMoreElements() && adapAddr == null) {
    
    // Get the current address
    
    InetAddress addr = addrEnum.nextElement();
    if ( IPAddress.isNumericAddress( addr.getHostAddress()))
      adapAddr = addr;
  }
  
  // Check if we found the IP address to bind to
  
  if ( adapAddr == null)
    throw new InvalidConfigurationException( "Adapter " + adapter + " does not have a valid IP address");

  // Return the adapter address
  
  return adapAddr;
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:46,代码来源:AbstractServerConfigurationBean.java


注:本文中的java.net.NetworkInterface.getInetAddresses方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。