Java IP Address Get getAllIPAddresses()

Here you can find the source of getAllIPAddresses()

Description

Retrieves all the IP addresses of the local computer.

License

Open Source License

Declaration

public static Collection<InterfaceAddress> getAllIPAddresses() throws SocketException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *   //from  www.  j  a va2  s. co m
 *   Copyright 2008 Mytech Ingenieria Aplicada <http://www.mytechia.com>, Gervasio Varela, Alejandro Paz
 *   Victor Sonora
 * 
 *   This file is part of Mytechia Commons.
 *
 *   Mytechia Commons 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 3 of the License, or
 *   (at your option) any later version.
 *
 *   Mytechia Commons 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 Mytechia Commons.  If not, see <http://www.gnu.org/licenses/>.
 * 
 ******************************************************************************/

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

public class Main {
    /**
     * Retrieves all the IP addresses of the local computer.
     * The loopback address (127.0.0.1) will not be included.
     */
    public static Collection<InterfaceAddress> getAllIPAddresses() throws SocketException {
        ArrayList<InterfaceAddress> direcciones = new ArrayList();
        InterfaceAddress ipLoopback = null;

        try {
            Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
            while (ifaces.hasMoreElements()) {
                NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
                for (InterfaceAddress ips : iface.getInterfaceAddresses()) {
                    InetAddress ia = ips.getAddress();
                    if (!ia.getHostAddress().contains(":")) {
                        if (ia.isLoopbackAddress()) {
                            ipLoopback = ips;
                        } else {
                            direcciones.add(ips);
                        }
                    }
                }
            }

            if ((direcciones.isEmpty()) && (ipLoopback != null)) {
                direcciones.add(ipLoopback);
            }
        } catch (SocketException e) {
            throw e;
        }

        return direcciones;
    }
}

Related

  1. getAllHostAddresses(boolean ignoreLoopback, boolean ignoreIP6)
  2. getAllHostIPs(String hostName)
  3. getAllInternalHostIPs(String hostName)
  4. getAllIp()
  5. getAllIPsAndAssignedBroadcastAddresses()
  6. getALLLocalHostIP()
  7. getAllLocalIP()
  8. getAllLocalIP()