Java Host Name Get getCanonicalHostName()

Here you can find the source of getCanonicalHostName()

Description

Returns the fully-qualified domain name of the local host in all lower case.

License

Open Source License

Exception

Parameter Description
RuntimeException to wrap UnknownHostException if the local host could not beresolved into an address

Declaration

public static String getCanonicalHostName() 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;

import java.net.SocketException;
import java.net.UnknownHostException;

import java.util.Enumeration;

public class Main {
    /**/*from ww  w.jav a2  s  . co m*/
     * Returns the fully-qualified domain name of the local host in all lower case.
     *
     * @throws RuntimeException to wrap {@link UnknownHostException} if the local host could not be
     *     resolved into an address
     */
    public static String getCanonicalHostName() {
        try {
            return getExternalAddressOfLocalSystem().getCanonicalHostName().toLowerCase();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns the externally-facing IPv4 network address of the local host.
     *
     * <p>This function implements a workaround for an
     * <a href="http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4665037">issue</a> in
     * {@link InetAddress#getLocalHost}.
     *
     * <p><b>Note:</b> This code was pilfered from {@link "com.google.net.base.LocalHost"} which was
     * never made open source.
     *
     * @throws UnknownHostException if the local host could not be resolved into an address
     */
    private static InetAddress getExternalAddressOfLocalSystem() throws UnknownHostException {
        InetAddress localhost = InetAddress.getLocalHost();
        // If we have a loopback address, look for an address using the network cards.
        if (localhost.isLoopbackAddress()) {
            try {
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                if (interfaces == null) {
                    return localhost;
                }
                while (interfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = interfaces.nextElement();
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        if (!(address.isLoopbackAddress() || address.isLinkLocalAddress()
                                || address instanceof Inet6Address)) {
                            return address;
                        }
                    }
                }
            } catch (SocketException e) {
                // Fall-through.
            }
        }
        return localhost;
    }
}

Related

  1. getAllMyHostAdresses()
  2. getByName(String host)
  3. getCanonicalHostName()
  4. getCanonicalHostName()
  5. getCanonicalHostName()
  6. getCanonicalHostname()