Java IP Address Get getHostIP()

Here you can find the source of getHostIP()

Description

Find the IP address of localhost.

License

Open Source License

Return

the IP address of localhost.

Declaration

public static String getHostIP() 

Method Source Code

//package com.java2s;
/* $Revision: 361 $ $Date: 2010-10-01 16:11:11 +0200 (Fr, 01 Okt 2010) $ $Author: bkiers $
 *
 * Copyright (C) 2007-2009  National Library of the Netherlands,
 *                          Nationaal Archief of the Netherlands,
 *                          Planets//  w w w.ja va 2  s  .co m
 *                          KEEP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * For more information about this project, visit
 * http://dioscuri.sourceforge.net/
 * or contact us via email:
 *   jrvanderhoeven at users.sourceforge.net
 *   blohman at users.sourceforge.net
 *   bkiers at users.sourceforge.net
 *
 * Developed by:
 *   Nationaal Archief               <www.nationaalarchief.nl>
 *   Koninklijke Bibliotheek         <www.kb.nl>
 *   Tessella Support Services plc   <www.tessella.com>
 *   Planets                         <www.planets-project.eu>
 *   KEEP                            <www.keep-project.eu>
 *
 * Project Title: DIOSCURI
 */

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**
     * Find the IP address of localhost.
     *
     * @return the IP address of localhost.
     */
    public static String getHostIP() {
        try {
            byte[] ipAddr = InetAddress.getLocalHost().getAddress();
            StringBuilder b = new StringBuilder();
            if (ipAddr != null && ipAddr.length > 0) {
                b.append(ipAddr[0]);
                for (int i = 1; i < ipAddr.length; i++) {
                    b.append('.').append(ipAddr[i]);
                }
                return b.toString();
            }
        } catch (UnknownHostException e) {
            // swallow it
        }
        return null;
    }
}

Related

  1. getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6)
  2. getFirstNonLoopBackAddress(boolean preferIpv4, boolean preferIPv6)
  3. getHostIp()
  4. getHostIP()
  5. getHostIp()
  6. getHostIP()
  7. getHostIpAddress()
  8. getHostIPByHostName(String host)
  9. getInetIps()