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:ch.cyberduck.core.proxy.ProxySocketFactory.java

@Override
public Socket createSocket(final InetAddress inetAddress, final int port) throws IOException {
    try {//from   w ww .java2 s  .c o  m
        final Socket socket = this.factory(inetAddress.getHostName()).createSocket(inetAddress, port);
        configurator.configure(socket);
        return socket;
    } catch (IllegalArgumentException e) {
        throw this.failure(inetAddress.getHostName(), e);
    }
}

From source file:de.burlov.ultracipher.core.mail.AuthenticatingSMTPClient.java

/**
 * Login to the ESMTP server by sending the EHLO command with the client
 * hostname as an argument. Before performing any mail commands, you must
 * first login./*from  w w w .  j a v a 2 s . c  om*/
 * <p/>
 *
 * @return True if successfully completed, false if not.
 * @throws SMTPConnectionClosedException If the SMTP server prematurely closes the connection as a
 *                                       result of the client being idle or some other reason
 *                                       causing the server to send SMTP reply code 421. This
 *                                       exception may be caught either as an IOException or
 *                                       independently as itself.
 * @throws java.io.IOException           If an I/O error occurs while either sending a command to
 *                                       the server or receiving a reply from the server.
 *                                       *
 */
public boolean elogin() throws IOException {
    String name;
    InetAddress host;

    host = getLocalAddress();
    name = host.getHostName();

    if (name == null) {
        return false;
    }

    return SMTPReply.isPositiveCompletion(ehlo(name));
}

From source file:org.apereo.portal.PortalInfoProviderImpl.java

protected String getDefaultNetworkInterfaceName() {
    this.logger// w  ww  .  j a  v  a2  s  . c o m
            .info("Attempting to resolve serverName by iterating over NetworkInterface.getNetworkInterfaces()");

    //Fail back to our best attempt at resolution
    final Enumeration<NetworkInterface> networkInterfaceEnum;
    try {
        networkInterfaceEnum = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.warn("Failed to get list of available NetworkInterfaces.", e);
        return null;
    }

    //Use a local variable here to try and return the first hostName found that doesn't start with localhost
    String name = null;
    while (networkInterfaceEnum.hasMoreElements()) {
        final NetworkInterface networkInterface = networkInterfaceEnum.nextElement();

        for (Enumeration<InetAddress> inetAddressEnum = networkInterface.getInetAddresses(); inetAddressEnum
                .hasMoreElements();) {
            final InetAddress inetAddress = inetAddressEnum.nextElement();
            name = inetAddress.getHostName();
            if (!name.startsWith("localhost")) {
                return name;
            }
        }
    }

    return name;
}

From source file:org.manalith.ircbot.plugin.ping.PingPlugin.java

@BotCommand("")
public String ping(@Option(name = "?", help = "?  ?? ? IP ") String uri) {
    InetAddress addr;

    try {/*  ww  w  .j a v a 2s. c  om*/
        addr = InetAddress.getByName(uri);
    } catch (UnknownHostException e) {
        return "[Ping]   .";
    }

    try {
        return String.format("[Ping] %s(%s) is %s: ", addr.getHostName(), addr.getHostAddress(),
                addr.isReachable(3000) ? "reachable" : "not reachable");
    } catch (IOException e) {
        return String.format("[Ping] ?  ?.(%s)", e.getMessage());
    }
}

From source file:SignatureInfos.java

/**
* @return hostname of the current server
*//*ww w . j a  v a2 s  . co  m*/
private String getHostname() {
    try {
        final InetAddress addr = InetAddress.getLocalHost();
        return addr.getHostName();
    } catch (final UnknownHostException e) {
        return "localhost";
    }

}

From source file:com.microsoft.alm.plugin.idea.common.services.TelemetryContextInitializer.java

private String getComputerName() {
    String hostname = TfsTelemetryHelper.UNKNOWN;

    try {//from w w w. j  a v a2  s  .  c om
        final InetAddress address = InetAddress.getLocalHost();
        hostname = address.getHostName();
    } catch (UnknownHostException ex) {
        // This case is covered by the initial value of hostname above
    }

    return hostname;
}

From source file:org.apache.synapse.JmxAdapter.java

/**
 * Initialized the JMX configuration./*  w w  w .ja v  a 2 s .c  om*/
 *
 * @throws  SynapseException  if the port or host configuration is erroneous
 */
private void initConfiguration() {
    int jndiPort = jmxInformation.getJndiPort();
    if ((jndiPort < 0) || (65535 < jndiPort)) {
        throw new SynapseException("JNDI Port for Remote Registry not properly configured");
    }

    int rmiPort = jmxInformation.getRmiPort();
    if ((rmiPort < 0) || (65535 < rmiPort)) {
        rmiPort = 0;
        log.info("No or invalid value specified for JMX RMI port - using dynamic port");
    }

    String hostname = jmxInformation.getHostName();
    if ((hostname == null) || (hostname.trim().length() == 0)) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            jmxInformation.setHostName(address.getHostName());
        } catch (UnknownHostException ex) {
            throw new SynapseException("Hostname of loopback could not be determined", ex);
        }
    }
}

From source file:com.clustercontrol.port.protocol.ReachAddressIMAP.java

/**
 * IMAP????????/*w  ww.  j  a  va  2 s.  c o m*/
 * 
 * @param addressText
 * @return IMAP
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 
        boolean retry = true; // ????(true:??false:???)

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the IMAP Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        IMAPClient client = new IMAPClient();

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                client.setDefaultTimeout(m_timeout);

                start = HinemosTime.currentTimeMillis();
                client.connect(address, m_portNo);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;

                result = client.getReplyString();

                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    }
                } else {
                    result = result + ("\n" + "Response Time < 1ms");
                }

                retry = false;
                isReachable = true;

            } catch (SocketException e) {
                result = (e.getMessage() + "[SocketException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (client.isConnected()) {
                    try {
                        client.disconnect();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e);
                    }
                }
            }

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

        m_message = result + "(IMAP/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}

From source file:com.clustercontrol.port.protocol.ReachAddressPOP3.java

/**
 * POP3????????//from   w  w w. j a va  2  s .c  o  m
 * 
 * @param addressText
 * @return POP3
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 
        boolean retry = true; // ????(true:??false:???)

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the POP3 Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        POP3Client client = new POP3Client();

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                client.setDefaultTimeout(m_timeout);

                start = HinemosTime.currentTimeMillis();
                client.connect(address, m_portNo);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;

                result = client.getReplyString();

                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    }
                } else {
                    result = result + ("\n" + "Response Time < 1ms");
                }

                retry = false;
                isReachable = true;

            } catch (SocketException e) {
                result = (e.getMessage() + "[SocketException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (client.isConnected()) {
                    try {
                        client.disconnect();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e);
                    }
                }
            }

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

        m_message = result + "(POP3/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}

From source file:edu.tcu.gaduo.ihe.iti.ct_transaction.service.NTPClient.java

public void getRefAddr() {
    int refId = message.getReferenceId();
    String refAddr = NtpUtils.getHostAddress(refId);
    String refName = null;//from w  w w. ja  va 2s.c o m
    if (refId != 0) {
        int version = message.getVersion();
        if (refAddr.equals("127.127.1.0")) {
            refName = "LOCAL"; // This is the ref address for the Local
            // Clock
        } else if (stratum >= 2) {
            // If reference id has 127.127 prefix then it uses its own
            // reference clock
            // defined in the form 127.127.clock-type.unit-num (e.g.
            // 127.127.8.0 mode 5
            // for GENERIC DCF77 AM; see refclock.htm from the NTP software
            // distribution.
            if (!refAddr.startsWith("127.127")) {
                try {
                    InetAddress addr = InetAddress.getByName(refAddr);
                    String name = addr.getHostName();
                    if (name != null && !name.equals(refAddr))
                        refName = name;
                } catch (UnknownHostException e) {
                    // some stratum-2 servers sync to ref clock device but
                    // fudge stratum level higher... (e.g. 2)
                    // ref not valid host maybe it's a reference clock name?
                    // otherwise just show the ref IP address.
                    refName = NtpUtils.getReferenceClock(message);
                }
            }
        } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
            refName = NtpUtils.getReferenceClock(message);
            // refname usually have at least 3 characters (e.g. GPS, WWV,
            // LCL, etc.)
        }
        // otherwise give up on naming the beast...
    }
    if (refName != null && refName.length() > 1)
        refAddr += " (" + refName + ")";
    logger.info(" Reference Identifier:\t" + refAddr);
}