Java InetAddress Create getAllInetAddress(final String interfaceName)

Here you can find the source of getAllInetAddress(final String interfaceName)

Description

get All Inet Address

License

Apache License

Declaration

public static List<InetAddress> getAllInetAddress(final String interfaceName) throws SocketException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class Main {
    public static List<InetAddress> getAllInetAddress() throws SocketException {
        return getAllInetAddress(null);
    }//from w  w  w. j  a  v a  2s  .co  m

    public static List<InetAddress> getAllInetAddress(final String interfaceName) throws SocketException {
        final List<InetAddress> ret = new ArrayList<>();
        final Enumeration<NetworkInterface> netInterfaces = getInterfaces(interfaceName);
        while (netInterfaces.hasMoreElements()) {
            final NetworkInterface networkInterface = netInterfaces.nextElement();
            for (final Enumeration<InetAddress> loopInetAddress = networkInterface
                    .getInetAddresses(); loopInetAddress.hasMoreElements();) {
                final InetAddress tempInetAddress = loopInetAddress.nextElement();
                ret.add(tempInetAddress);
            }
        }
        return ret;
    }

    static Enumeration<NetworkInterface> getInterfaces(final String interfaceName) throws SocketException {
        return interfaceName != null
                ? new Vector<NetworkInterface>(Arrays.asList(NetworkInterface.getByName(interfaceName))).elements()
                : NetworkInterface.getNetworkInterfaces();

    }
}

Related

  1. getActiveInterfaceInetAddresses()
  2. getAddressInfoFindAll(Function convertValue, Predicate doBreak)
  3. getAddressPriority(InetAddress addr)
  4. getAddrInfo(InetAddress pAddr)
  5. getAllIPv4InetAddresses()
  6. getAllLocalIPv4InetAddresses()
  7. getBroadcast(InetAddress address)
  8. getBytes(InetAddress addr, int port)