Java IP Address Get getAllMyHostIPV4Adresses()

Here you can find the source of getAllMyHostIPV4Adresses()

Description

get All My Host IPV Adresses

License

Open Source License

Declaration

public static Set<InetAddress> getAllMyHostIPV4Adresses() throws Exception 

Method Source Code

//package com.java2s;
/*//from   w  w w  .ja va2s.  c  om
 * (C) Copyright IBM Corp. 2008
 *
 * LICENSE: Eclipse Public License v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.util.Enumeration;
import java.util.HashSet;

import java.util.Set;

public class Main {
    public static Set<InetAddress> getAllMyHostIPV4Adresses() throws Exception {
        Set<InetAddress> addresses = new HashSet<InetAddress>();

        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            Enumeration<InetAddress> ias = en.nextElement().getInetAddresses();
            while (ias.hasMoreElements()) {
                InetAddress address = ias.nextElement();
                if (isIPv4(stripToSlash(address.toString())))
                    addresses.add(address);
            }
        }

        return addresses;
    }

    public static boolean isIPv4(String ip) {
        return ip.matches("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");
    }

    public static String stripToSlash(String s) {
        int idx = s.indexOf('/');
        return -1 == idx ? s : s.substring(idx + 1);
    }
}

Related

  1. getALLLocalHostIP()
  2. getAllLocalIP()
  3. getAllLocalIP()
  4. getAllLocalIPs()
  5. getAllLocalIpv4Addresses()
  6. getAllUsableIPAddresses()
  7. getExternalIP()
  8. getExternalIp()
  9. getExternalIp()