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