Java Host Name Get getHost()

Here you can find the source of getHost()

Description

get Host

License

Open Source License

Declaration

public static String getHost() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    /***// www  . j av a  2s  .  c om
     * 
     * @return
     */
    public static String getHost() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface current = interfaces.nextElement();
                if (!current.isUp() || current.isLoopback() || current.isVirtual())
                    continue;
                if (current.getName().contains("lxc"))
                    continue; // <-- do not get LXC interfaces
                Enumeration<InetAddress> addresses = current.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress current_addr = addresses.nextElement();
                    if (current_addr instanceof Inet4Address) {
                        return current_addr.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
            return null;
        }
        return null;
    }
}

Related

  1. getFile(String host, String savePath)
  2. getFullHostname()
  3. getFullyQualifiedDomainName(String unqualifiedHostname)
  4. getFullyQualifiedHostName()
  5. getHost()
  6. getHost(String host)
  7. getHost(String link)
  8. getHost(String s)
  9. getHostCore(final String quadGraph)