Example usage for java.net InetAddress getHostName

List of usage examples for java.net InetAddress getHostName

Introduction

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

Prototype

public String getHostName() 

Source Link

Document

Gets the host name for this IP address.

Usage

From source file:org.openconcerto.sql.PropsConfiguration.java

public static String getHostname() {
    final InetAddress addr;
    try {//from   w ww  . j  a  v a  2 s . c  om
        addr = InetAddress.getLocalHost();
    } catch (final UnknownHostException e) {
        e.printStackTrace();
        return "local";
    }
    return addr.getHostName();
}

From source file:com.clustercontrol.ping.util.ReachAddress.java

/**
 * ????????????/*from  w ww  .  j av a  2  s . c  o m*/
 * 
 * @param addressText
 * @return PING
 */
private boolean isReachable(String addressText) {

    m_message = null;
    m_messageOrg = null;
    m_lost = 0;
    m_average = 0;

    try {
        long max = 0;
        long min = 0;
        long sum = 0;
        int num = 0;
        long start = 0;
        long end = 0;

        // 
        StringBuffer buffer = new StringBuffer();

        InetAddress address = InetAddress.getByName(addressText);
        buffer.append("Pinging " + address.getHostName() + " [" + address.getHostAddress() + "].\n\n");

        int i = 0;
        for (; i < m_sentCount; i++) {

            // Reachability ?? ICMP ??
            boolean isReachable;
            // isReachable?????????
            synchronized (m_syncObj) {
                start = HinemosTime.currentTimeMillis();
                isReachable = address.isReachable(m_timeout);
                end = HinemosTime.currentTimeMillis();
            }

            long time = end - start;

            if (isReachable) {
                buffer.append("Reply from " + address.getHostAddress() + ": ");

                sum += time;

                if (i == 0) {
                    max = time;
                    min = time;
                } else {
                    if (time > max) {
                        max = time;
                    } else if (time < min) {
                        min = time;
                    }
                }
                num++;

                if (time > 0) {
                    buffer.append("time=" + time + "ms\n");
                } else {
                    buffer.append("time<1ms\n");
                }
            } else {
                if (time >= m_timeout) {
                    buffer.append("Request timed out.\n");
                } else {
                    buffer.append(
                            "Reply from " + address.getHostAddress() + ": Destination net unreachable.\n");
                    //                        num++;
                }
            }

            if (i < m_sentCount - 1) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        buffer.append("\nPing statistics for " + address.getHostAddress() + ":\n");
        //
        if (num == 0) {
            m_lost = 100;
        } else {
            m_lost = (i - num) * 100 / i;
        }

        //
        m_message = "Packets: Sent = " + i + ", Received = " + num + ", Lost = " + (i - num) + " (" + m_lost
                + "% loss),";

        buffer.append("\t" + m_message + "\n");

        buffer.append("Approximate round trip times in milli-seconds:\n");

        // ?
        if (num != 0) {
            m_average = sum / num;
        } else {
            m_average = 0;
        }

        buffer.append("\tMinimum = " + min + "ms, Maximum = " + max + "ms, Average = " + m_average + "ms\n");

        m_messageOrg = buffer.toString();
        return true;

    } catch (UnknownHostException e) {
        m_log.warn("isReachable() " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage()
                + e.getClass().getSimpleName() + ", " + e.getMessage(), e);

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage() + " (" + e.getMessage() + ")";
    } catch (IOException e) {
        m_log.warn("isReachable() " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage()
                + e.getClass().getSimpleName() + ", " + e.getMessage(), e);

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage() + " (" + e.getMessage() + ")";
    }
    return false;
}

From source file:org.tango.orb.IORDump.java

/**
 * Make the IOR analyse//from  w  w  w .  j  a  v a  2s. c om
 */
private void iorAnalysis() throws DevFailed {
    if (!iorString.startsWith("IOR:")) {
        DevFailedUtils.throwDevFailed("CORBA_ERROR", iorString + " not an IOR");
    }
    final ORB orb = ORBManager.getOrb();
    final ParsedIOR pior = new ParsedIOR((org.jacorb.orb.ORB) orb, iorString);
    final org.omg.IOP.IOR ior = pior.getIOR();
    typeId = ior.type_id;
    final List<Profile> profiles = pior.getProfiles();
    for (final Profile profile : profiles) {
        if (profile instanceof IIOPProfile) {
            final IIOPProfile iiopProfile = ((IIOPProfile) profile).to_GIOP_1_0();
            iiopVersion = (int) iiopProfile.version().major + "." + (int) iiopProfile.version().minor;
            final String name = ((IIOPAddress) iiopProfile.getAddress()).getOriginalHost();
            java.net.InetAddress iadd = null;
            try {
                iadd = java.net.InetAddress.getByName(name);
            } catch (final UnknownHostException e) {
                DevFailedUtils.throwDevFailed(e);
            }
            hostName = iadd.getHostName();

            port = ((IIOPAddress) iiopProfile.getAddress()).getPort();
            if (port < 0) {
                port += 65536;
            }

        } else {
            DevFailedUtils.throwDevFailed("CORBA_ERROR", iorString + " not an IOR");
        }
    }
    // code for old jacorb 2.3
    // final List<IIOPProfile> profiles = pior.getProfiles();
    // for (IIOPProfile profile : profiles) {
    // iiopVersion = (int) profile.version().major + "." + (int)
    // p.version().minor;
    // final String name = ((IIOPAddress)
    // profile.getAddress()).getOriginalHost();
    // java.net.InetAddress iadd = null;
    // try {
    // iadd = java.net.InetAddress.getByName(name);
    // } catch (final UnknownHostException e) {
    // DevFailedUtils.throwDevFailed(e);
    // }
    // hostName = iadd.getHostName();
    //
    // port = ((IIOPAddress) profile.getAddress()).getPort();
    // if (port < 0) {
    // port += 65536;
    // }
    // }

}

From source file:org.psikeds.common.idgen.impl.RequestIdGenerator.java

public void initHostName() {
    try {//from w  ww.  j ava  2 s  . c o  m
        if (this.includeHostname && StringUtils.isEmpty(this.hostName)) {
            final InetAddress localMachine = InetAddress.getLocalHost();
            // InetAddress.getHostName() performs a Nameservice-Lookup.
            // This can be a very expensive Operation, worst case blocking
            // for several Minutes until some Timeout occurs!
            this.hostName = (this.resolveHostname ? localMachine.getHostName() : localMachine.getHostAddress());
        }
    } catch (final Exception ex) {
        this.hostName = null;
        this.includeHostname = false;
        this.resolveHostname = false;
    }
}

From source file:com.cscao.apps.ntplib.NTPClient.java

private TimeInfo requestTime(String server, int timeout) {
    TimeInfo responseInfo = null;/*from   w  w w . j a v a  2s .  co  m*/
    NTPUDPClient client = new NTPUDPClient();
    client.setDefaultTimeout(timeout);
    try {
        client.open();
        try {
            InetAddress hostAddr = InetAddress.getByName(server);
            //                System.out.println("> " + hostAddr.getHostName() + "/"
            //                        + hostAddr.getHostAddress());
            details += "NTP server: " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress();

            responseInfo = client.getTime(hostAddr);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    } catch (SocketException e) {
        e.printStackTrace();
    }

    client.close();
    return responseInfo;

}

From source file:org.lockss.exporter.Exporter.java

protected String getHostName() {
    String res = ConfigManager.getPlatformHostname();
    if (res == null) {
        try {/*from  w w w.j  av  a2  s .  co  m*/
            InetAddress inet = InetAddress.getLocalHost();
            return inet.getHostName();
        } catch (UnknownHostException e) {
            log.warning("Can't get hostname", e);
            return "unknown";
        }
    }
    return res;
}

From source file:org.mule.test.firewall.FirewallTestCase.java

@Test
public void testLocalHost() throws Exception {
    InetAddress aLocalAddress = InetAddress.getLocalHost();
    logger.info("Java returns " + addressToString(aLocalAddress) + " as the 'local' address");
    assertNotSame("No external address", LOCALADDR, aLocalAddress.getHostAddress());
    consistentAddress(aLocalAddress.getHostName(), false);
    assertEquals("Inconsistent hostname", aLocalAddress.getHostName(), new HostNameFactory().create(null));
}

From source file:org.springframework.integration.ip.tcp.connection.AbstractTcpConnection.java

public AbstractTcpConnection(Socket socket, boolean server, boolean lookupHost) {
    this.server = server;
    InetAddress inetAddress = socket.getInetAddress();
    if (inetAddress != null) {
        this.hostAddress = inetAddress.getHostAddress();
        if (lookupHost) {
            this.hostName = inetAddress.getHostName();
        } else {//w ww .  ja  v a2  s . com
            this.hostName = this.hostAddress;
        }
    }
    int port = socket.getPort();
    this.connectionId = this.hostName + ":" + port + ":" + UUID.randomUUID().toString();
    try {
        this.soLinger = socket.getSoLinger();
    } catch (SocketException e) {
    }
}

From source file:org.jajuk.util.UtilSystem.java

/**
 * Gets the host name.//from   w ww .  jav  a  2s  . c  o m
 * 
 * @return This box hostname
 */
public static String getHostName() {
    String sHostname = null;
    // Try to get hostname using the standard way
    try {
        sHostname = InetAddress.getLocalHost().getHostName();
    } catch (final Exception e) {
        Log.debug("Cannot get Hostname using the standard way");
    }
    if (sHostname == null) {
        // Try using IP now
        try {
            final java.net.InetAddress inetAdd = java.net.InetAddress.getByName(LOCAL_IP);
            sHostname = inetAdd.getHostName();
        } catch (final Exception e) {
            Log.debug("Cannot get Hostname by IP");
        }
    }
    // If still no hostname, return a default value
    if (sHostname == null) {
        sHostname = Const.DEFAULT_HOSTNAME;
    }
    return sHostname;
}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.security.GridSecurity.java

public String getHostname() throws GT4SecurityException {
    String hostname = null;/*from w w  w  .ja v  a  2s  . c om*/
    try {
        InetAddress addr = InetAddress.getLocalHost();
        hostname = addr.getHostName();
    } catch (UnknownHostException uhe) {
        log.error("Error in getting hostname", uhe);
        throw new GT4SecurityException("Error in getting hostname", uhe);
    }
    return hostname;
}