Java IP Address Get getLocalIPs()

Here you can find the source of getLocalIPs()

Description

get Local I Ps

License

Open Source License

Declaration

public static List<InetAddress> getLocalIPs() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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<InetAddress> getLocalIPs() {

        Enumeration<NetworkInterface> interfaces;

        try {/* ww  w  . j  a  v  a 2  s.c om*/
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return null;
        }

        List<InetAddress> ips = new ArrayList<InetAddress>();

        while (interfaces.hasMoreElements()) {
            NetworkInterface current = interfaces.nextElement();

            if (current != null) {
                Enumeration<InetAddress> addresses = current.getInetAddresses();

                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    if (addr != null) {
                        ips.add(addr);
                    }
                }
            }
        }

        return ips;

    }
}

Related

  1. getLocalIpByInterfaceName(String interfaceName)
  2. getLocalIPByNetworkInterface()
  3. getLocalIPCollection()
  4. getLocalIPList()
  5. getLocalIps()
  6. getLocalIPs()
  7. getLocalIPs(boolean refresh)
  8. getLocalIpv4()
  9. getLocalIPv6Address()