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 examples;
019    
020    import java.io.IOException;
021    import java.net.InetAddress;
022    
023    import org.apache.commons.net.daytime.DaytimeTCPClient;
024    import org.apache.commons.net.daytime.DaytimeUDPClient;
025    
026    /***
027     * This is an example program demonstrating how to use the DaytimeTCP
028     * and DaytimeUDP classes.
029     * This program connects to the default daytime service port of a
030     * specified server, retrieves the daytime, and prints it to standard output.
031     * The default is to use the TCP port.  Use the -udp flag to use the UDP
032     * port.
033     * <p>
034     * Usage: daytime [-udp] <hostname>
035     * <p>
036     ***/
037    public final class daytime
038    {
039    
040        public static final void daytimeTCP(String host) throws IOException
041        {
042            DaytimeTCPClient client = new DaytimeTCPClient();
043    
044            // We want to timeout if a response takes longer than 60 seconds
045            client.setDefaultTimeout(60000);
046            client.connect(host);
047            System.out.println(client.getTime().trim());
048            client.disconnect();
049        }
050    
051        public static final void daytimeUDP(String host) throws IOException
052        {
053            DaytimeUDPClient client = new DaytimeUDPClient();
054    
055            // We want to timeout if a response takes longer than 60 seconds
056            client.setDefaultTimeout(60000);
057            client.open();
058            System.out.println(client.getTime(
059                                              InetAddress.getByName(host)).trim());
060            client.close();
061        }
062    
063    
064        public static final void main(String[] args)
065        {
066    
067            if (args.length == 1)
068            {
069                try
070                {
071                    daytimeTCP(args[0]);
072                }
073                catch (IOException e)
074                {
075                    e.printStackTrace();
076                    System.exit(1);
077                }
078            }
079            else if (args.length == 2 && args[0].equals("-udp"))
080            {
081                try
082                {
083                    daytimeUDP(args[1]);
084                }
085                catch (IOException e)
086                {
087                    e.printStackTrace();
088                    System.exit(1);
089                }
090            }
091            else
092            {
093                System.err.println("Usage: daytime [-udp] <hostname>");
094                System.exit(1);
095            }
096    
097        }
098    
099    }
100