Java Local Host Get getLocalHost()

Here you can find the source of getLocalHost()

Description

Use this instead of InetAddress#getLocalHost() to prevent multiple lookups.

License

Mozilla Public License

Declaration

public synchronized static InetAddress getLocalHost() 

Method Source Code

//package com.java2s;
/*/*from   w w w  . ja  v a2 s  .  c o m*/
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is Ziptie Client Framework.
 * 
 * The Initial Developer of the Original Code is AlterPoint.
 * Portions created by AlterPoint are Copyright (C) 2007,
 * AlterPoint, Inc. All Rights Reserved.
 * 
 * Contributor(s):
 */

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    /**
     * The cached localhost address. 
     * @see #getLocalHost()
     */
    private static InetAddress localhost;

    /**
     * Use this instead of {@link InetAddress#getLocalHost()} to prevent multiple lookups.
     */
    public synchronized static InetAddress getLocalHost() {
        if (localhost == null) {
            try {
                localhost = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                throw new RuntimeException(e);
            }
        }
        return localhost;
    }
}

Related

  1. getLocalHost()
  2. getLocalHost()
  3. getLocalHost()
  4. getLocalHost()
  5. getLocalHost()
  6. getLocalhost()
  7. getLocalHost()
  8. getLocalHost()
  9. getLocalHost()