Java Local Host Get isLocalHost(String address)

Here you can find the source of isLocalHost(String address)

Description

Indica si la direccion IP/host que se pasa como parametro pertenece a la maquina local o no

License

Open Source License

Return

true si es de la maquina local.

Declaration


public static boolean isLocalHost(String address) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  .ja  v  a 2  s .c  om*/
 * ISABEL: A group collaboration tool for the Internet
 * Copyright (C) 2009 Agora System S.A.
 * 
 * This file is part of Isabel.
 * 
 * Isabel is free software: you can redistribute it and/or modify
 * it under the terms of the Affero GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Isabel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Affero GNU General Public License for more details.
 * 
 * You should have received a copy of the Affero GNU General Public License
 * along with Isabel.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.*;

public class Main {
    /**

     * Indica si la direccion IP/host que se pasa como parametro pertenece a la maquina local o no
     * @return true si es de la maquina local.
     */

    public static boolean isLocalHost(String address) {

        if ((address == null) || (address.equals(""))) {
            return false;
        }

        try {
            InetAddress ia = InetAddress.getByName(address);

            if (ia.isLoopbackAddress()) {
                return true;
            }

            NetworkInterface ni = NetworkInterface.getByInetAddress(ia);

            return ni != null;
        } catch (SocketException se) {
            return false;
        } catch (UnknownHostException uhe) {
            return false;
        }
    }
}

Related

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