Java IP Address Get getAllLocalIP()

Here you can find the source of getAllLocalIP()

Description

get All Local IP

License

Open Source License

Declaration

public static List<?> getAllLocalIP() 

Method Source Code

//package com.java2s;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {

    public static List<?> getAllLocalIP() {
        List<String> localServers = new ArrayList<String>();
        String regExp = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
        try {// w  ww.  j a v a  2s. com
            Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                Enumeration<?> address = ni.getInetAddresses();
                while (address.hasMoreElements()) {
                    ip = (InetAddress) address.nextElement();
                    if (!ip.isLoopbackAddress() && ip.getHostAddress().matches(regExp)) {
                        localServers.add(ip.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return localServers;
    }
}

Related

  1. getAllIp()
  2. getAllIPAddresses()
  3. getAllIPsAndAssignedBroadcastAddresses()
  4. getALLLocalHostIP()
  5. getAllLocalIP()
  6. getAllLocalIPs()
  7. getAllLocalIpv4Addresses()
  8. getAllMyHostIPV4Adresses()
  9. getAllUsableIPAddresses()