Java Local Host Get getLocalHosts()

Here you can find the source of getLocalHosts()

Description

Get host IP address

License

Apache License

Return

IP Address

Declaration

public static List<String> getLocalHosts() 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**//from  w  w  w.  jav a 2  s. c o m
     * Get host IP address
     * 
     * @return IP Address
     */
    public static List<String> getLocalHosts() {
        List<String> list = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
            while (nets.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) nets.nextElement();
                Enumeration<InetAddress> itor = ni.getInetAddresses();
                while (itor.hasMoreElements()) {
                    InetAddress inetAddress = itor.nextElement();
                    String host = inetAddress.getHostName();
                    list.add(host);
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        return list;
    }
}

Related

  1. getLocalHostName()
  2. getLocalHostName()
  3. getLocalHostName(boolean includeDomain)
  4. getLocalHostName(boolean useHostname)
  5. getLocalHostNames()
  6. isLocalHost(String address)
  7. isLocalHost(String h1)
  8. isLocalhost(String host)
  9. isLocalhost(String hostname)