Here you can find the source of getLocalIpAddresses()
Parameter | Description |
---|---|
RuntimeException | If there is an error retrieving the InetAddresses |
public static List<InetAddress> getLocalIpAddresses() throws RuntimeException
//package com.java2s; //License from project: Open Source License import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import java.util.List; import java.util.Vector; public class Main { /**/*w ww . j a v a2s . c o m*/ * Returns a list of local InetAddresses for this machine * * @return List[InetAddress] The list of IP addresses * * @throws RuntimeException * If there is an error retrieving the InetAddresses * @since 1.2 */ public static List<InetAddress> getLocalIpAddresses() throws RuntimeException { return getLocalIpAddresses(false, false); } /** * Returns a list of local InetAddresses for this machine * * @param pruneSiteLocal * boolean Set to true if site local (10/8, for example) addresses should be pruned * * @return List[InetAddress] The list of addresses * * @throws RuntimeException * If there is an error retrieving the InetAddresses * @since IpHelper.java 1.2 */ public static List<InetAddress> getLocalIpAddresses(boolean pruneSiteLocal) throws RuntimeException { return getLocalIpAddresses(pruneSiteLocal, false); } /** * Returns a list of local InetAddresses for this machine * * @param pruneSiteLocal * boolean Set to true if site local (10/8, for example) addresses should be pruned * @param pruneDown * boolean Set to true to prune out interfaces whose .isUp() return false * * @return List[InetAddress] The list of addresses * * @throws RuntimeException * If there is an error retrieving the InetAddresses * @since IpHelper.java 2007-11-22 */ public static List<InetAddress> getLocalIpAddresses(boolean pruneSiteLocal, boolean pruneDown) throws RuntimeException { try { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); List<InetAddress> addresses = new Vector<InetAddress>(); while (nics.hasMoreElements()) { NetworkInterface iface = nics.nextElement(); Enumeration<InetAddress> addrs = iface.getInetAddresses(); if (!pruneDown || iface.isUp()) { while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress()) { if (!pruneSiteLocal || (pruneSiteLocal && !addr.isSiteLocalAddress())) { addresses.add(addr); } } } } } return addresses; } catch (SocketException e) { throw new RuntimeException(e.getMessage(), e); } } public static List<InetAddress> getLocalIpAddresses(String ifaceName, boolean pruneSiteLocal) throws RuntimeException { try { NetworkInterface iface = NetworkInterface.getByName(ifaceName); List<InetAddress> addresses = new Vector<InetAddress>(); Enumeration<InetAddress> addrs = iface.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); if (!addr.isLoopbackAddress() || (pruneSiteLocal && !addr.isLinkLocalAddress())) { addresses.add(addr); } } return addresses; } catch (SocketException e) { throw new RuntimeException(e.getMessage(), e); } } }