简体   繁体   English

Java,将NetworkInterface拆分为ip个地址

[英]Java, split NetworkInterface by ip addresses

my first post, so please be gentle;) I have to modify some existing software that is focused on java.net.NetworkInterface.我的第一篇文章,所以请温和;)我必须修改一些专注于 java.net.NetworkInterface 的现有软件。 Previously all interfaces were listed in a combobox and the user had to pick one.以前所有接口都列在 combobox 中,用户必须选择一个。 Now it should be possible to not only pick the interface, but also one specific ip address that comes with this interface.现在应该不仅可以选择接口,还可以选择该接口附带的一个特定的 ip 地址。 As a lot of the code is based on the NetworkInterface class, I was thinking if it is possible to "create" some new NetworkInterface by splitting an existing one by the ip addresses.由于很多代码都是基于 NetworkInterface class,我在想是否可以通过将现有的 NetworkInterface 拆分为 ip 地址来“创建”一些新的 NetworkInterface 。 Is that even possible?这可能吗? I know, I can't create a new NetworkInterface.我知道,我无法创建新的 NetworkInterface。 But maybe there is another option, that I just can not see right now.但也许还有另一种选择,我现在看不到。

This is, what I got so far:这是我到目前为止得到的:

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;
}

I think, you would build a Map<.netAddress, NetworkInterface>, to provide the values of .netAddress (with or without name of corresponded NetworkInterface) in your combobox and to be able to handle a pair of values (.netAddress, NetworkInterface) when user will made his selection.我认为,您将构建一个 Map<.netAddress, NetworkInterface>,以在您的 combobox 中提供 .netAddress 的值(有或没有对应的 NetworkInterface 的名称)并能够处理一对值(.netAddress, NetworkInterface)用户何时做出选择。

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

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