Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Returns all available IP addresses.
- * <p>
- * To get the first/main local ip only you could use also
- * {@link #getLocalIP() }.
- * <p>
- * In error case or if no network connection is established, we return
- * an empty list here.
- * <p>
- * Loopback addresses are excluded - so 127.0.0.1 will not be never
- * returned.
- * <p>
- * The "primary" IP might not be the first one in the returned list.
- *
- * @return Returns all IP addresses (can be an empty list in error case
- * or if network connection is missing).
- * @since 0.1.0
- */
- public static Collection<InetAddress> getAllLocalIPs()
- {
- LinkedList<InetAddress> listAdr = new LinkedList<InetAddress>();
- try
- {
- Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
- if (nifs == null) return listAdr;
- while (nifs.hasMoreElements())
- {
- NetworkInterface nif = nifs.nextElement();
- // We ignore subinterfaces - as not yet needed.
- Enumeration<InetAddress> adrs = nif.getInetAddresses();
- while (adrs.hasMoreElements())
- {
- InetAddress adr = adrs.nextElement();
- if (adr != null && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()))
- {
- listAdr.add(adr);
- }
- }
- }
- return listAdr;
- }
- catch (SocketException ex)
- {
- Logger.getLogger(Net.class.getName()).log(Level.WARNING, "No IP address available", ex);
- return listAdr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement