Java Local Host Get getLocalHostName()

Here you can find the source of getLocalHostName()

Description

Retrieves the localhost's hostname, or if unavailable, the ip address

License

Open Source License

Declaration

public static String getLocalHostName() throws SocketException, UnknownHostException 

Method Source Code


//package com.java2s;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Main {
    /**//from   ww  w . java2  s.c o m
     * Retrieves the localhost's hostname, or if unavailable, the ip address
     */
    public static String getLocalHostName() throws SocketException, UnknownHostException {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            NetworkInterface networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
            if (networkInterface == null)
                throw e;
            InetAddress ipAddress = networkInterface.getInetAddresses().nextElement();
            if (ipAddress == null)
                throw e;
            return ipAddress.getHostAddress();
        }
    }
}

Related

  1. getLocalHostName()
  2. getLocalHostName()
  3. getLocalHostName()
  4. getLocalHostName()
  5. getLocalHostName()
  6. GetLocalHostName()
  7. getLocalHostName()
  8. getLocalHostName()
  9. getLocalHostName()