Java IP Address Get getAllIp()

Here you can find the source of getAllIp()

Description

get All Ip

License

Open Source License

Exception

Parameter Description
SocketException I/O error occurs

Return

list of all available ip address

Declaration

public static List<String> getAllIp() throws SocketException 

Method Source Code


//package com.java2s;
/*/*ww  w.ja va 2 s.c om*/
 License:
    
 blueprint-sdk is licensed under the terms of Eclipse Public License(EPL) v1.0
 (http://www.eclipse.org/legal/epl-v10.html)
    
    
 Distribution:
    
 Repository - https://github.com/lempel/blueprint-sdk.git
 Blog - http://lempel.egloos.com
 */

import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    /**
     * @return list of all available ip address
     * @throws SocketException I/O error occurs
     */
    public static List<String> getAllIp() throws SocketException {
        List<String> result = new ArrayList<>();

        Enumeration<NetworkInterface> infs = NetworkInterface.getNetworkInterfaces();
        while (infs.hasMoreElements()) {
            NetworkInterface inf = infs.nextElement();
            if (!inf.isUp() || inf.isVirtual()) {
                continue;
            }

            Enumeration<InetAddress> addrs = inf.getInetAddresses();
            while (addrs.hasMoreElements()) {
                InetAddress addr = addrs.nextElement();
                String ip = addr.getHostAddress();
                if ("0.0.0.0".equals(ip) || "::0".equals(ip)) {
                    continue;
                }
                result.add(ip);
            }
        }

        return result;
    }
}

Related

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