Here you can find the source of getAllHostAddresses(boolean ignoreLoopback, boolean ignoreIP6)
Parameter | Description |
---|---|
ignoreLoopback | ignore loopback addresses |
ignoreIP6 | ignore IPv6 addresses |
private static InetAddress[] getAllHostAddresses(boolean ignoreLoopback, boolean ignoreIP6)
//package com.java2s; //License from project: Open Source License import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class Main { /**//from w ww. j a va2s .c om * Returns all IP addresses that could be found on this machine. If desired, * loopback and/or IPv6 addresses can be ignored. * * @param ignoreLoopback * ignore loopback addresses * @param ignoreIP6 * ignore IPv6 addresses * * @return all IP addresses found that adhere to the restrictions. */ private static InetAddress[] getAllHostAddresses(boolean ignoreLoopback, boolean ignoreIP6) { ArrayList<InetAddress> list = new ArrayList<InetAddress>(); try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); getAllHostAddresses(networkInterface, list, ignoreLoopback, ignoreIP6); } } catch (SocketException e) { // IGNORE } return list.toArray(new InetAddress[list.size()]); } /** * Adds all IP addresses that are bound to a specific network interface to a * list. If desired, loopback and/or IPv6 addresses can be ignored. * * @param nw * the network interface for which the addresses are determined * @param target * the list used to store the addresses * @param ignoreLoopback * ignore loopback addresses * @param ignoreIP6 * ignore IPv6 addresses */ private static void getAllHostAddresses(NetworkInterface nw, List<InetAddress> target, boolean ignoreLoopback, boolean ignoreIP6) { Enumeration<InetAddress> e2 = nw.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress tmp = e2.nextElement(); boolean t1 = !ignoreLoopback || !tmp.isLoopbackAddress(); boolean t2 = !ignoreIP6 || (tmp instanceof Inet4Address); if (t1 && t2) { target.add(tmp); } } } }