Java IP Address Get getLocalAddresses(boolean ipv4only)

Here you can find the source of getLocalAddresses(boolean ipv4only)

Description

get Local Addresses

License

Open Source License

Declaration

private static List<InetAddress> getLocalAddresses(boolean ipv4only) throws SocketException 

Method Source Code


//package com.java2s;
import java.net.Inet4Address;
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 {
    private static List<InetAddress> getLocalAddresses(boolean ipv4only) throws SocketException {
        List<InetAddress> result = new ArrayList<>();
        for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
            NetworkInterface nif = e.nextElement();
            for (Enumeration<InetAddress> en = nif.getInetAddresses(); en.hasMoreElements();) {
                InetAddress addr = en.nextElement();
                if (ipv4only && !(addr instanceof Inet4Address))
                    continue;
                result.add(addr);/*  w w w.  j a v a 2 s.co  m*/
            }
        }
        return result;
    }
}

Related

  1. getIPV6AddrArray(String s)
  2. getIPV6AllAddresses(NetworkInterface ni)
  3. getIpv6List()
  4. getLanIPAddress()
  5. getLinuxIPAddress()
  6. getLocalHostIp()
  7. getLocalHostIp()
  8. getLocalHostIP()
  9. getLocalhostIp()