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     *      http://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    
018    package org.apache.commons.net.daytime;
019    
020    import java.io.IOException;
021    import java.net.DatagramPacket;
022    import java.net.InetAddress;
023    
024    import org.apache.commons.net.DatagramSocketClient;
025    
026    /***
027     * The DaytimeUDPClient class is a UDP implementation of a client for the
028     * Daytime protocol described in RFC 867.  To use the class, merely
029     * open a local datagram socket with
030     * {@link org.apache.commons.net.DatagramSocketClient#open  open }
031     * and call {@link #getTime  getTime } to retrieve the daytime
032     * string, then
033     * call {@link org.apache.commons.net.DatagramSocketClient#close  close }
034     * to close the connection properly.  Unlike
035     * {@link org.apache.commons.net.daytime.DaytimeTCPClient},
036     * successive calls to {@link #getTime  getTime } are permitted
037     * without re-establishing a connection.  That is because UDP is a
038     * connectionless protocol and the Daytime protocol is stateless.
039     * <p>
040     * <p>
041     * @author Daniel F. Savarese
042     * @see DaytimeTCPClient
043     ***/
044    
045    public final class DaytimeUDPClient extends DatagramSocketClient
046    {
047        /*** The default daytime port.  It is set to 13 according to RFC 867. ***/
048        public static final int DEFAULT_PORT = 13;
049    
050        private final byte[] __dummyData = new byte[1];
051        // Received dates should be less than 256 bytes
052        private final byte[] __timeData = new byte[256];
053    
054        /***
055         * Retrieves the time string from the specified server and port and
056         * returns it.
057         * <p>
058         * @param host The address of the server.
059         * @param port The port of the service.
060         * @return The time string.
061         * @exception IOException If an error occurs while retrieving the time.
062         ***/
063        public String getTime(InetAddress host, int port) throws IOException
064        {
065            DatagramPacket sendPacket, receivePacket;
066    
067            sendPacket =
068                new DatagramPacket(__dummyData, __dummyData.length, host, port);
069            receivePacket = new DatagramPacket(__timeData, __timeData.length);
070    
071            _socket_.send(sendPacket);
072            _socket_.receive(receivePacket);
073    
074            return new String(receivePacket.getData(), 0, receivePacket.getLength());
075        }
076    
077        /*** Same as <code>getTime(host, DaytimeUDPClient.DEFAULT_PORT);</code> ***/
078        public String getTime(InetAddress host) throws IOException
079        {
080            return getTime(host, DEFAULT_PORT);
081        }
082    
083    }
084