【问题标题】:How would I get only IPv4 addresses我如何只获得 IPv4 地址
【发布时间】:2017-01-16 18:16:50
【问题描述】:

我有以下代码,它应该只获取所有活动接口的 IPv4 地址,但它仍然在某些计算机上返回 IPv6 地址。

public static List<List> getIpAddress() {
    List<String> ip = new ArrayList<>();
    List<List> ipRefined = new ArrayList<>();
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            if (iface.isLoopback() || !iface.isUp())
                continue;
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while(addresses.hasMoreElements()) {
                ip.add(addresses.nextElement().getHostAddress());
            }
        }
    } catch (SocketException e) {
        throw new RuntimeException(e);
    }
    for(int x = 0; x < ip.size(); x++){
        if(ip.get(x).contains("%")){
            try {
                if (ip.get(x + 1).contains(".")) {
                    List<String> tempList = new ArrayList<>();
                    tempList.add(ip.get(x).substring(ip.get(x).indexOf("%") + 1));
                    tempList.add(ip.get(x + 1));
                    ipRefined.add(tempList);
                }
            } catch (IndexOutOfBoundsException ae) {
            }
        }
    }
    return ipRefined;
}

我尝试使用System.setProperty("java.net.preferIPv4Stack" , "true"); 指定仅使用IPv4,但这只会导致getIpAddress() 返回一个空列表。我应该如何在不使用字符串操作的情况下获取活动接口的 IPv4?

编辑:

使用System.setProperty("java.net.preferIPv4Stack" , "true"); 总是会导致getIpAddress() 返回一个空列表。

【问题讨论】:

  • 我不确定这一点,但是当您阅读addresses 枚举时,检查每个地址是否instanceof Inet4Address 怎么样?
  • 我可以试试,但我认为问题在于接口只返回其 IPv6。让我快速进行一些调试。
  • 看来我已经过滤了它,所以当我执行if (ip.get(x + 1).contains(".")) 时只能返回 IPv4。我重写了一些代码以使用instanceof 而不是contains(),但它不会改变任何东西。我将更详细地更新问题。
  • IPv6 地址包含“%”,因为 IPv4 没有 scopeid。

标签: java ipv4 network-interface


【解决方案1】:

来自InterfaceAddress

这个类代表一个网络接口地址。简而言之,当地址是 IPv4 时,它是 IP 地址、子网掩码和广播地址。如果是 IPv6 地址,则为 IP 地址和网络前缀长度。

这是我的代码:

Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
  NetworkInterface networkInterface = interfaces.nextElement();
  System.out.println(String.format("networkInterface: %s", networkInterface.toString()));

  if (!networkInterface.isUp()) {
    continue;
  }

  for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
    int npf = interfaceAddress.getNetworkPrefixLength();
    InetAddress address = interfaceAddress.getAddress();
    InetAddress broadcast = interfaceAddress.getBroadcast();
    if (broadcast == null && npf != 8) {
      System.out.println(String.format("IPv6: %s; Network Prefix Length: %s", address, npf));
    } else {
      System.out.println(String.format("IPv4: %s; Subnet Mask: %s; Broadcast: %s", address, npf, broadcast));
    }
  }
}

【讨论】:

  • 这个和子字符串的组合解决了我的问题。
【解决方案2】:

我的 Kotlin 解决方案:

  private suspend fun getAllActiveInterfaces(): Sequence<NetworkInterface> {
        return withContext(Dispatchers.IO) {
            NetworkInterface
                .getNetworkInterfaces()
                .asSequence()
                .filter { !it.isLoopback && it.isUp && !blackListedInterfaces.contains(it.name) }
        }
    }
    suspend fun getAllAddresses(): Result<Collection<InetAddress>> {
        return withContextCatching(Dispatchers.IO) {
            getAllActiveInterfaces()
                .map { networkInterface ->
                    networkInterface
                        .also { Timber.d("Interface: ${it.name}") }
                        .interfaceAddresses
                        .mapNotNull { it.address }
                        .filterIsInstance<Inet4Address>()
                }
                .flatten()
                .toSet()
        }
    }
private val blackListedInterfaces = setOf("dummy0")

【讨论】:

  • 不错! “木材”线让我愣了一秒。一些cmets会很棒。比我五年前的干净多了! :P
猜你喜欢
  • 2016-09-20
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 2012-12-25
  • 1970-01-01
  • 2010-11-06
  • 2012-06-16
相关资源
最近更新 更多