View Javadoc

    1   /*
    2    * Copyright 2001-2005 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net;
    17
    18  import java.io.IOException;
    19  import java.net.DatagramPacket;
    20  import java.net.InetAddress;
    21
    22  /****
    23   * The DaytimeUDPClient class is a UDP implementation of a client for the
    24   * Daytime protocol described in RFC 867.  To use the class, merely
    25   * open a local datagram socket with
    26   * {@link org.apache.commons.net.DatagramSocketClient#open  open }
    27   * and call {@link #getTime  getTime } to retrieve the daytime
    28   * string, then
    29   * call {@link org.apache.commons.net.DatagramSocketClient#close  close }
    30   * to close the connection properly.  Unlike
    31   * {@link org.apache.commons.net.DaytimeTCPClient},
    32   * successive calls to {@link #getTime  getTime } are permitted
    33   * without re-establishing a connection.  That is because UDP is a
    34   * connectionless protocol and the Daytime protocol is stateless.
    35   * <p>
    36   * <p>
    37   * @author Daniel F. Savarese
    38   * @see DaytimeTCPClient
    39   ***/
    40
    41  public final class DaytimeUDPClient extends DatagramSocketClient
    42  {
    43      /**** The default daytime port.  It is set to 13 according to RFC 867. ***/
    44      public static final int DEFAULT_PORT = 13;
    45
    46      private byte[] __dummyData = new byte[1];
    47      // Received dates should be less than 256 bytes
    48      private byte[] __timeData = new byte[256];
    49
    50      /****
    51       * Retrieves the time string from the specified server and port and
    52       * returns it.
    53       * <p>
    54       * @param host The address of the server.
    55       * @param port The port of the service.
    56       * @return The time string.
    57       * @exception IOException If an error occurs while retrieving the time.
    58       ***/
    59      public String getTime(InetAddress host, int port) throws IOException
    60      {
    61          DatagramPacket sendPacket, receivePacket;
    62
    63          sendPacket =
    64              new DatagramPacket(__dummyData, __dummyData.length, host, port);
    65          receivePacket = new DatagramPacket(__timeData, __timeData.length);
    66
    67          _socket_.send(sendPacket);
    68          _socket_.receive(receivePacket);
    69
    70          return new String(receivePacket.getData(), 0, receivePacket.getLength());
    71      }
    72
    73      /**** Same as <code>getTime(host, DaytimeUDPClient.DEFAULT_PORT);</code> ***/
    74      public String getTime(InetAddress host) throws IOException
    75      {
    76          return getTime(host, DEFAULT_PORT);
    77      }
    78
    79  }
    80