Java InetAddress Create getAllIPv4InetAddresses()

Here you can find the source of getAllIPv4InetAddresses()

Description

Get all the local IPV4 addresses without the loopback one

License

Open Source License

Declaration

public static Set<Inet4Address> getAllIPv4InetAddresses() 

Method Source Code

//package com.java2s;
/**/* w w  w. java2  s .co  m*/
 * PETALS - PETALS Services Platform. Copyright (c) 2008 EBM Websourcing,
 * http://www.ebmwebsourcing.com/
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version. This library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * -------------------------------------------------------------------------
 * $Id$
 * -------------------------------------------------------------------------
 */

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**
     * Get all the local IPV4 addresses without the loopback one
     * 
     * @return
     */
    public static Set<Inet4Address> getAllIPv4InetAddresses() {
        Set<Inet4Address> result = new HashSet<Inet4Address>();
        Set<NetworkInterface> iset = getAllInterfaces();
        for (NetworkInterface networkInterface : iset) {
            Enumeration<InetAddress> e = networkInterface.getInetAddresses();
            while (e.hasMoreElements()) {
                InetAddress elem = e.nextElement();
                if (elem instanceof Inet4Address) {
                    result.add((Inet4Address) elem);
                }
            }
        }
        return result;
    }

    /**
     * Get all the host interfaces including the loopback one
     * 
     * @return
     */
    public static Set<NetworkInterface> getAllInterfaces() {
        Set<NetworkInterface> result = new HashSet<NetworkInterface>();
        try {
            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                result.add(e.nextElement());
            }
        } catch (SocketException e) {
        }
        return result;
    }
}

Related

  1. getActiveInterfaceInetAddresses()
  2. getAddressInfoFindAll(Function convertValue, Predicate doBreak)
  3. getAddressPriority(InetAddress addr)
  4. getAddrInfo(InetAddress pAddr)
  5. getAllInetAddress(final String interfaceName)
  6. getAllLocalIPv4InetAddresses()
  7. getBroadcast(InetAddress address)
  8. getBytes(InetAddress addr, int port)
  9. getClassPart(InetAddress ip, int partNumber)