Java Local Host Get isLocalhost(String host)

Here you can find the source of isLocalhost(String host)

Description

is Localhost

License

Open Source License

Declaration

public static boolean isLocalhost(String host) 

Method Source Code

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

import java.net.*;

public class Main {
    public static boolean isLocalhost(String host) {
        InetAddress address;/*  w ww  .  j av  a 2s . c  o  m*/

        // convert the host name or ip string to an InetAddress object
        // If the address is unknown it is not localhost
        try {
            address = InetAddress.getByName(host);
        } catch (UnknownHostException e) {
            return false;
        }
        // Check if the address is a valid special local or loop back
        if (address.isAnyLocalAddress() || address.isLoopbackAddress())
            return true;

        // Check if the address is defined on any interface
        try {
            return NetworkInterface.getByInetAddress(address) != null;
        } catch (SocketException e) {
            return false;
        }
    }
}

Related

  1. getLocalHostName(boolean useHostname)
  2. getLocalHostNames()
  3. getLocalHosts()
  4. isLocalHost(String address)
  5. isLocalHost(String h1)
  6. isLocalhost(String hostname)
  7. isLocalhost(String someHost)
  8. isLocalHostNameInList(String[] hostList)