    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.daytime;
    019
    020import java.io.IOException;
    021import java.net.DatagramPacket;
    022import java.net.InetAddress;
    023
    024import org.apache.commons.net.DatagramSocketClient;
    025
    026/**
    027 * The DaytimeUDPClient class is a UDP implementation of a client for the Daytime protocol described in RFC 867. To use the class, merely open a local datagram
    028 * socket with {@link org.apache.commons.net.DatagramSocketClient#open open} and call {@link #getTime getTime} to retrieve the daytime string, then call
    029 * {@link org.apache.commons.net.DatagramSocketClient#close close} to close the connection properly. Unlike
    030 * {@link org.apache.commons.net.daytime.DaytimeTCPClient}, successive calls to {@link #getTime getTime} are permitted without re-establishing a connection.
    031 * That is because UDP is a connectionless protocol and the Daytime protocol is stateless.
    032 *
    033 * @see DaytimeTCPClient
    034 */
    035public final class DaytimeUDPClient extends DatagramSocketClient {
    036
    037    /**
    038     * The default daytime port. It is set to 13 according to RFC 867.
    039     */
    040    public static final int DEFAULT_PORT = 13;
    041
    042    private final byte[] dummyData = new byte[1];
    043
    044    /**
    045     * Received dates should be less than 256 bytes.
    046     */
    047    private final byte[] timeData = new byte[256];
    048
    049    /**
    050     * Constructs a new instance.
    051     */
    052    public DaytimeUDPClient() {
    053        // empty
    054    }
    055
    056    /**
    057     * Gets the time string from the specified server and port and returns it.
    058     * <p>
    059     * Shorthand for {@code getTime(host, DaytimeUDPClient.DEFAULT_PORT);}
    060     * </p>
    061     *
    062     * @param host the host
    063     * @return the time
    064     * @throws IOException on error
    065     */
    066    public String getTime(final InetAddress host) throws IOException {
    067        return getTime(host, DEFAULT_PORT);
    068    }
    069
    070    /**
    071     * Gets the time string from the specified server and port and returns it.
    072     *
    073     * @param host The address of the server.
    074     * @param port The port of the service.
    075     * @return The time string.
    076     * @throws IOException If an error occurs while retrieving the time.
    077     */
    078    public String getTime(final InetAddress host, final int port) throws IOException {
    079        final DatagramPacket sendPacket = new DatagramPacket(dummyData, dummyData.length, host, port);
    080        final DatagramPacket receivePacket = new DatagramPacket(timeData, timeData.length);
    081        checkOpen().send(sendPacket);
    082        checkOpen().receive(receivePacket);
    083        return new String(receivePacket.getData(), 0, receivePacket.getLength(), getCharset());
    084    }
    085
    086}