1 package examples.ntp; 2 3 18 19 import java.io.IOException ; 20 import java.net.InetAddress ; 21 import org.apache.commons.net.TimeTCPClient; 22 import org.apache.commons.net.TimeUDPClient; 23 24 36 public final class TimeClient 37 { 38 39 public static final void timeTCP(String host) throws IOException 40 { 41 TimeTCPClient client = new TimeTCPClient(); 42 try { 43 client.setDefaultTimeout(60000); 45 client.connect(host); 46 System.out.println(client.getDate()); 47 } finally { 48 client.disconnect(); 49 } 50 } 51 52 public static final void timeUDP(String host) throws IOException 53 { 54 TimeUDPClient client = new TimeUDPClient(); 55 56 client.setDefaultTimeout(60000); 58 client.open(); 59 System.out.println(client.getDate(InetAddress.getByName(host))); 60 client.close(); 61 } 62 63 public static final void main(String [] args) 64 { 65 66 if (args.length == 1) 67 { 68 try 69 { 70 timeTCP(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 timeUDP(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: TimeClient [-udp] <hostname>"); 93 System.exit(1); 94 } 95 96 } 97 98 } 99 100 | Popular Tags |