简体   繁体   中英

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. Previously all interfaces were listed in a combobox and the user had to pick one. Now it should be possible to not only pick the interface, but also one specific ip address that comes with this interface. 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. Is that even possible? I know, I can't create a new 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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