繁体   English   中英

Java,将NetworkInterface拆分为ip个地址

[英]Java, split NetworkInterface by ip addresses

我的第一篇文章,所以请温和;)我必须修改一些专注于 java.net.NetworkInterface 的现有软件。 以前所有接口都列在 combobox 中,用户必须选择一个。 现在应该不仅可以选择接口,还可以选择该接口附带的一个特定的 ip 地址。 由于很多代码都是基于 NetworkInterface class,我在想是否可以通过将现有的 NetworkInterface 拆分为 ip 地址来“创建”一些新的 NetworkInterface 。 这可能吗? 我知道,我无法创建新的 NetworkInterface。 但也许还有另一种选择,我现在看不到。

这是我到目前为止得到的:

public static List<NetworkInterface> listNetworkInterfaces() throws SocketException
{
    List<NetworkInterface> interfaces = new ArrayList<>();
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements())
    {
        NetworkInterface intrface = networkInterfaces.nextElement();
        
        //ignore logalhost interface
        if (intrface.isLoopback() || intrface.isVirtual() || !intrface.isUp())
            continue;

        // here should be the point, where I seperate the ip adresses from one interface
        Enumeration<InetAddress> inetAddresses = intrface.getInetAddresses();
        while (inetAddress.hasMoreElements())
            System.out.println("InetAddress: " + inetAddress);
    

        interfaces.add(intrface);
    }
    return interfaces;
}

我认为,您将构建一个 Map<.netAddress, NetworkInterface>,以在您的 combobox 中提供 .netAddress 的值(有或没有对应的 NetworkInterface 的名称)并能够处理一对值(.netAddress, NetworkInterface)用户何时做出选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM