Java Host Check isLocal(final String hostName)

Here you can find the source of isLocal(final String hostName)

Description

Checks if given host name is local.

License

Apache License

Parameter

Parameter Description
hostName host name

Return

true if host name pointing to local address

Declaration

public static boolean isLocal(final String hostName) 

Method Source Code

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

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;

import java.net.UnknownHostException;

public class Main {
    /**//from   w ww.j  ava  2  s.  c o m
     * Checks if given host name is local.
     *
     * @param hostName host name
     * @return {@code true} if host name pointing to local address
     */
    public static boolean isLocal(final String hostName) {
        try {
            return isLocal(InetAddress.getByName(hostName));
        } catch (final UnknownHostException e) {
            return false;
        }
    }

    /**
     * Check if given address is local.
     *
     * @param address address
     * @return {@code true} if address is local
     */
    public static boolean isLocal(final InetAddress address) {
        // 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 (final SocketException e) {
            return false;
        }
    }
}

Related

  1. checkHost(String hostname)
  2. checkHostIsAvailable(String hostName)
  3. checkHostName(String hostName)
  4. isHostReachable(String host, int timeout)
  5. isInternalHostname(String host)
  6. isLocal(final String hostname)
  7. isThisMe(String hostname)
  8. normalizeHost(String host)
  9. normalizeHostName(String name)