Example usage for org.apache.commons.net.time TimeTCPClient connect

List of usage examples for org.apache.commons.net.time TimeTCPClient connect

Introduction

In this page you can find the example usage for org.apache.commons.net.time TimeTCPClient connect.

Prototype

public void connect(InetAddress host) throws SocketException, IOException 

Source Link

Document

Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port.

Usage

From source file:examples.unix.rdate.java

public static final void timeTCP(String host) throws IOException {
    TimeTCPClient client = new TimeTCPClient();

    // We want to timeout if a response takes longer than 60 seconds
    client.setDefaultTimeout(60000);/*from  w ww  .  j  ava 2s  .c om*/
    client.connect(host);
    System.out.println(client.getDate().toString());
    client.disconnect();
}

From source file:beans.TimeClient.java

public static final void timeTCP(String host) throws IOException {
    TimeTCPClient client = new TimeTCPClient();
    try {//from   w  w  w . j  a  va 2s . c  om
        // We want to timeout if a response takes longer than 60 seconds
        client.setDefaultTimeout(60000);
        client.connect(host);
        System.out.println(client.getDate());
    } finally {
        client.disconnect();
    }
}

From source file:it.jnrpe.plugin.CheckTime.java

private Date getTimeTCP(String host, int timeout, int port) throws IOException {
    TimeTCPClient client = new TimeTCPClient();
    client.setDefaultPort(port);/*from  w  w  w  .  j av  a  2 s  .  c o  m*/
    client.setDefaultTimeout((int) TimeUnit.SECOND.convert(timeout));
    client.connect(host);
    Date date = client.getDate();
    client.disconnect();
    return date;

}

From source file:org.techytax.util.DateHelper.java

public static Date getNTPDate() {
    List<String> hosts = Collections.singletonList("nl.pool.ntp.org");

    for (String host : hosts) {
        TimeTCPClient client = new TimeTCPClient();
        client.setDefaultTimeout(5000);/*from  w w  w .ja  v  a  2 s  . com*/
        try {
            client.connect(host);
            Date ntpDate = client.getDate();
            client.disconnect();
            return ntpDate;
        } catch (Exception exp) {
            System.out.println("NTP connection error");
        }
    }
    return null;
}