DaytimeUDPClient.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.daytime;

  18. import java.io.IOException;
  19. import java.net.DatagramPacket;
  20. import java.net.InetAddress;

  21. import org.apache.commons.net.DatagramSocketClient;

  22. /**
  23.  * 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
  24.  * socket with {@link org.apache.commons.net.DatagramSocketClient#open open } and call {@link #getTime getTime } to retrieve the daytime string, then call
  25.  * {@link org.apache.commons.net.DatagramSocketClient#close close } to close the connection properly. Unlike
  26.  * {@link org.apache.commons.net.daytime.DaytimeTCPClient}, successive calls to {@link #getTime getTime } are permitted without re-establishing a connection.
  27.  * That is because UDP is a connectionless protocol and the Daytime protocol is stateless.
  28.  *
  29.  * @see DaytimeTCPClient
  30.  */
  31. public final class DaytimeUDPClient extends DatagramSocketClient {

  32.     /**
  33.      * The default daytime port. It is set to 13 according to RFC 867.
  34.      */
  35.     public static final int DEFAULT_PORT = 13;

  36.     private final byte[] dummyData = new byte[1];

  37.     /**
  38.      * Received dates should be less than 256 bytes.
  39.      */
  40.     private final byte[] timeData = new byte[256];

  41.     /**
  42.      * Shorthand for <code>getTime(host, DaytimeUDPClient.DEFAULT_PORT);</code>
  43.      *
  44.      * @param host the host
  45.      * @return the time
  46.      * @throws IOException on error
  47.      */
  48.     public String getTime(final InetAddress host) throws IOException {
  49.         return getTime(host, DEFAULT_PORT);
  50.     }

  51.     /**
  52.      * Gets the time string from the specified server and port and returns it.
  53.      *
  54.      * @param host The address of the server.
  55.      * @param port The port of the service.
  56.      * @return The time string.
  57.      * @throws IOException If an error occurs while retrieving the time.
  58.      */
  59.     public String getTime(final InetAddress host, final int port) throws IOException {
  60.         final DatagramPacket sendPacket = new DatagramPacket(dummyData, dummyData.length, host, port);
  61.         final DatagramPacket receivePacket = new DatagramPacket(timeData, timeData.length);
  62.         checkOpen().send(sendPacket);
  63.         checkOpen().receive(receivePacket);
  64.         return new String(receivePacket.getData(), 0, receivePacket.getLength(), getCharset());
  65.     }

  66. }