Save This Page
Home » juddi-tomcat-3.0.1 » examples » ntp » [javadoc | source]
    1   package examples.ntp;
    2   
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *      http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20    
   21   import java.io.IOException;
   22   import java.net.InetAddress;
   23   
   24   import org.apache.commons.net.time.TimeTCPClient;
   25   import org.apache.commons.net.time.TimeUDPClient;
   26   
   27   /***
   28    * This is an example program demonstrating how to use the TimeTCPClient
   29    * and TimeUDPClient classes.
   30    * This program connects to the default time service port of a
   31    * specified server, retrieves the time, and prints it to standard output.
   32    * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A>
   33    * for details.  The default is to use the TCP port.  Use the -udp flag to
   34    * use the UDP port.
   35    * <p>
   36    * Usage: TimeClient [-udp] <hostname>
   37    * <p>
   38    ***/
   39   public final class TimeClient
   40   {
   41   
   42       public static final void timeTCP(String host) throws IOException
   43       {
   44           TimeTCPClient client = new TimeTCPClient();
   45       try {
   46             // We want to timeout if a response takes longer than 60 seconds
   47             client.setDefaultTimeout(60000);
   48         client.connect(host);
   49             System.out.println(client.getDate());
   50       } finally {
   51             client.disconnect();
   52       }
   53       }
   54   
   55       public static final void timeUDP(String host) throws IOException
   56       {
   57           TimeUDPClient client = new TimeUDPClient();
   58   
   59           // We want to timeout if a response takes longer than 60 seconds
   60           client.setDefaultTimeout(60000);
   61           client.open();
   62           System.out.println(client.getDate(InetAddress.getByName(host)));
   63           client.close();
   64       }
   65   
   66       public static final void main(String[] args)
   67       {
   68   
   69           if (args.length == 1)
   70           {
   71               try
   72               {
   73                   timeTCP(args[0]);
   74               }
   75               catch (IOException e)
   76               {
   77                   e.printStackTrace();
   78                   System.exit(1);
   79               }
   80           }
   81           else if (args.length == 2 && args[0].equals("-udp"))
   82           {
   83               try
   84               {
   85                   timeUDP(args[1]);
   86               }
   87               catch (IOException e)
   88               {
   89                   e.printStackTrace();
   90                   System.exit(1);
   91               }
   92           }
   93           else
   94           {
   95               System.err.println("Usage: TimeClient [-udp] <hostname>");
   96               System.exit(1);
   97           }
   98   
   99       }
  100   
  101   }
  102   

Save This Page
Home » juddi-tomcat-3.0.1 » examples » ntp » [javadoc | source]