View Javadoc

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;
18  
19  import java.io.IOException;
20  import java.net.DatagramPacket;
21  import java.net.InetAddress;
22  
23  /***
24   * The DaytimeUDPClient class is a UDP implementation of a client for the
25   * Daytime protocol described in RFC 867.  To use the class, merely
26   * open a local datagram socket with
27   * {@link org.apache.commons.net.DatagramSocketClient#open  open }
28   * and call {@link #getTime  getTime } to retrieve the daytime
29   * string, then
30   * call {@link org.apache.commons.net.DatagramSocketClient#close  close }
31   * to close the connection properly.  Unlike
32   * {@link org.apache.commons.net.DaytimeTCPClient},
33   * successive calls to {@link #getTime  getTime } are permitted
34   * without re-establishing a connection.  That is because UDP is a
35   * connectionless protocol and the Daytime protocol is stateless.
36   * <p>
37   * <p>
38   * @author Daniel F. Savarese
39   * @see DaytimeTCPClient
40   ***/
41  
42  public final class DaytimeUDPClient extends DatagramSocketClient
43  {
44      /*** The default daytime port.  It is set to 13 according to RFC 867. ***/
45      public static final int DEFAULT_PORT = 13;
46  
47      private byte[] __dummyData = new byte[1];
48      // Received dates should be less than 256 bytes
49      private byte[] __timeData = new byte[256];
50  
51      /***
52       * Retrieves the time string from the specified server and port and
53       * returns it.
54       * <p>
55       * @param host The address of the server.
56       * @param port The port of the service.
57       * @return The time string.
58       * @exception IOException If an error occurs while retrieving the time.
59       ***/
60      public String getTime(InetAddress host, int port) throws IOException
61      {
62          DatagramPacket sendPacket, receivePacket;
63  
64          sendPacket =
65              new DatagramPacket(__dummyData, __dummyData.length, host, port);
66          receivePacket = new DatagramPacket(__timeData, __timeData.length);
67  
68          _socket_.send(sendPacket);
69          _socket_.receive(receivePacket);
70  
71          return new String(receivePacket.getData(), 0, receivePacket.getLength());
72      }
73  
74      /*** Same as <code>getTime(host, DaytimeUDPClient.DEFAULT_PORT);</code> ***/
75      public String getTime(InetAddress host) throws IOException
76      {
77          return getTime(host, DEFAULT_PORT);
78      }
79  
80  }
81