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  
18  package examples.unix;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  
23  import org.apache.commons.net.daytime.DaytimeTCPClient;
24  import org.apache.commons.net.daytime.DaytimeUDPClient;
25  
26  /***
27   * This is an example program demonstrating how to use the DaytimeTCP
28   * and DaytimeUDP classes.
29   * This program connects to the default daytime service port of a
30   * specified server, retrieves the daytime, and prints it to standard output.
31   * The default is to use the TCP port.  Use the -udp flag to use the UDP
32   * port.
33   * <p>
34   * Usage: daytime [-udp] <hostname>
35   ***/
36  public final class daytime
37  {
38  
39      public static final void daytimeTCP(String host) throws IOException
40      {
41          DaytimeTCPClient client = new DaytimeTCPClient();
42  
43          // We want to timeout if a response takes longer than 60 seconds
44          client.setDefaultTimeout(60000);
45          client.connect(host);
46          System.out.println(client.getTime().trim());
47          client.disconnect();
48      }
49  
50      public static final void daytimeUDP(String host) throws IOException
51      {
52          DaytimeUDPClient client = new DaytimeUDPClient();
53  
54          // We want to timeout if a response takes longer than 60 seconds
55          client.setDefaultTimeout(60000);
56          client.open();
57          System.out.println(client.getTime(
58                                            InetAddress.getByName(host)).trim());
59          client.close();
60      }
61  
62  
63      public static void main(String[] args)
64      {
65  
66          if (args.length == 1)
67          {
68              try
69              {
70                  daytimeTCP(args[0]);
71              }
72              catch (IOException e)
73              {
74                  e.printStackTrace();
75                  System.exit(1);
76              }
77          }
78          else if (args.length == 2 && args[0].equals("-udp"))
79          {
80              try
81              {
82                  daytimeUDP(args[1]);
83              }
84              catch (IOException e)
85              {
86                  e.printStackTrace();
87                  System.exit(1);
88              }
89          }
90          else
91          {
92              System.err.println("Usage: daytime [-udp] <hostname>");
93              System.exit(1);
94          }
95  
96      }
97  
98  }
99