Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:Main.java

protected static void obtainHostInfo() {
    HOST_IPADDRESS = "127.0.0.1";
    HOST_NAME = "";

    try {/*from  w  w  w .  ja va2  s.c om*/
        InetAddress primera = InetAddress.getLocalHost();
        String hostname = InetAddress.getLocalHost().getHostName();

        if (!primera.isLoopbackAddress() && !primera.getHostName().equalsIgnoreCase("localhost")
                && primera.getHostAddress().indexOf(':') == -1) {
            // Get it without delay!!

            //System.out.println ("we have it!");
            HOST_IPADDRESS = primera.getHostAddress();
            HOST_NAME = hostname;
            //System.out.println (hostname + " IP " + HOST_IPADDRESS);
            return;
        }

        // Get it by slow way ...
        InetAddress[] familia = InetAddress.getAllByName(hostname);

        int netto = 1;
        for (int aa = 0; aa < familia.length; aa++) {
            // System.out.println ("Networko " + netto++);
            InetAddress laAdd = familia[aa];
            String ipstring = laAdd.getHostAddress();
            hostname = laAdd.getHostName(); // don't know if it can change, probably not

            // System.out.println ("checking : " + hostname + " IP " + ipstring);
            if (laAdd.isLoopbackAddress())
                continue;
            if (hostname.equalsIgnoreCase("localhost"))
                continue;
            if (ipstring.indexOf(':') >= 0)
                continue;

            // System.out.println ("this pass!");
            HOST_IPADDRESS = ipstring;
            HOST_NAME = hostname;
            break;
        }
    } catch (Exception e) {
    }
}

From source file:com.singular.utils.FileUtils.java

public static String getCurrentHost() {
    String hostname = "localhost";
    try {//from   w  w  w . j a va 2  s. c o  m
        hostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return hostname;
}

From source file:com.baidu.rigel.biplatform.cache.util.MacAddressUtil.java

/** 
 * getMachineNetworkFlag ?MACIP?MAC/*from   www  .jav  a 2  s .  co m*/
 * @param ia
 * @return
 * @throws SocketException
 * @throws UnknownHostException
 */
public static String getMachineNetworkFlag(InetAddress ia) throws SocketException, UnknownHostException {
    if (ia == null) {
        ia = InetAddress.getLocalHost();
    }
    String machineFlag = getMacAddress(ia);
    if (StringUtils.isBlank(machineFlag)) {
        machineFlag = getIpAddress(ia);
    }
    return machineFlag;
}

From source file:edu.berkeley.sparrow.daemon.util.Network.java

/** Return the hostname of this machine, based on configured value, or system
 * Interrogation. *///w  ww.  j a  va  2s  . c  om
public static String getHostName(Configuration conf) {
    String defaultHostname = null;
    try {
        defaultHostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        defaultHostname = "localhost";
    }
    return conf.getString(SparrowConf.HOSTNAME, defaultHostname);
}

From source file:LocalAddress.java

/**
 * Return an address of a non-loopback interface on the local host
 * /*from  w w w.  j ava  2s . c  o  m*/
 * @return address
 *         the InetAddress of the local host
 */
public static InetAddress getLocalAddress() {
    InetAddress addr = null;
    try {
        addr = InetAddress.getLocalHost();
        // OK - is this the loopback addr ?
        if (!addr.isLoopbackAddress()) {
            return addr;
        }
        // plan B - enumerate the network interfaces
        Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
        while (ifaces.hasMoreElements()) {
            NetworkInterface netIf = (NetworkInterface) ifaces.nextElement();
            Enumeration addrs = netIf.getInetAddresses();
            while (addrs.hasMoreElements()) {
                addr = (InetAddress) addrs.nextElement();
                //System.out.println( "enum: " + addr.getHostAddress() );
                if (addr instanceof Inet6Address) {
                    // probably not what we want - keep looking
                    continue;
                }
                // chose (arbitrarily?) first non-loopback addr
                if (!addr.isLoopbackAddress()) {
                    return addr;
                }
            }
        }
        // nothing so far - last resort
        return getReflectedAddress();
    } catch (UnknownHostException uhE) {
        // deal with this
    } catch (SocketException sockE) {
        // can deal?
    }

    return null;
}

From source file:ch.epfl.eagle.daemon.util.Network.java

/** Return the hostname of this machine, based on configured value, or system
 * Interrogation. *///from w w  w  .j a v  a 2  s.c  o  m
public static String getHostName(Configuration conf) {
    String defaultHostname = null;
    try {
        defaultHostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        defaultHostname = "localhost";
    }
    return conf.getString(EagleConf.HOSTNAME, defaultHostname);
}

From source file:com.alibaba.jstorm.utils.NetWorkUtils.java

public static String hostname() {
    String hostname = null;//w w  w  . ja  v a 2 s.c  o  m
    try {
        hostname = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException e) {
        LOG.error("local_hostname", e);
    }
    return hostname;
}

From source file:com.carl.interesting.StartINGServer.java

/**
 * Starts Jetty HTTP server exposing JAX-RS resources defined in this
 * // w ww  .  j av a  2  s  .c o  m
 * @param port
 * @return [explain parameter]
 * @return Server [explain return type]
 * @exception throws [exception type] [explain exception]
 * @see [class,class#method,class#member]
 */
public static Server startServer(int port) {
    final ResourceConfig rc = new ResourceConfig().packages("com.carl.interesting");
    try {
        String address = InetAddress.getLocalHost().getHostAddress();
        if (NetworkUtil.isPortUsing(address, port)) {
            return null;
        }
        BASE = "http://" + address;
    } catch (UnknownHostException e) {
        LogUtil.logError(LOG, e);
    }
    // create and start a new instance of Jetty http server
    // exposing the Jersey application at base and port
    return JettyHttpContainerFactory.createServer(URI.create(BASE + ":" + Integer.toString(port)), rc);
}

From source file:architecture.ee.web.util.ServletUtils.java

public static InetAddress getLocalHost() {
    try {// w w  w  . jav  a  2s  . c  o  m
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        return null;
    }
}

From source file:ai.grakn.engine.util.EngineID.java

public static EngineID me() {
    String hostName = "";
    try {//  ww w . j  a  v a2s.  c o  m
        hostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        LOG.error("Could not get system hostname: ", e);
    }

    String value = hostName + "-" + UUID.randomUUID().toString();

    return EngineID.of(value);
}