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