Java Local Host Get getLocalHost()

Here you can find the source of getLocalHost()

Description

get Local Host

License

Apache License

Declaration

static String getLocalHost() throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

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

import java.util.Enumeration;

public class Main {
    static String getLocalHost() throws IOException {
        try {/*www.j  a  v a2  s  .c  om*/
            return InetAddress.getLocalHost().getCanonicalHostName();
        } catch (final UnknownHostException uhe) {
            final Enumeration<NetworkInterface> interfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                final NetworkInterface networkInterface = interfaces
                        .nextElement();
                if (null != networkInterface) {
                    final Enumeration<InetAddress> addresses = networkInterface
                            .getInetAddresses();
                    if (addresses.hasMoreElements()) {
                        return addresses.nextElement().getHostAddress();
                    }
                }
            }
            throw uhe;
        }
    }
}

Related

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