1 package org.apache.commons.net.ntp; 2 17 18 import java.io.IOException ; 19 import java.net.DatagramPacket ; 20 import java.net.InetAddress ; 21 import org.apache.commons.net.DatagramSocketClient; 22 23 38 39 public final class NTPUDPClient extends DatagramSocketClient 40 { 41 42 public static final int DEFAULT_PORT = 123; 43 44 private int _version = NtpV3Packet.VERSION_3; 45 46 58 public TimeInfo getTime(InetAddress host, int port) throws IOException 59 { 60 if (!isOpen()) 62 { 63 open(); 64 } 65 66 NtpV3Packet message = new NtpV3Impl(); 67 message.setMode(NtpV3Packet.MODE_CLIENT); 68 message.setVersion(_version); 69 DatagramPacket sendPacket = message.getDatagramPacket(); 70 sendPacket.setAddress(host); 71 sendPacket.setPort(port); 72 73 NtpV3Packet recMessage = new NtpV3Impl(); 74 DatagramPacket receivePacket = recMessage.getDatagramPacket(); 75 76 82 TimeStamp now = TimeStamp.getCurrentTime(); 83 84 message.setTransmitTime(now); 87 88 _socket_.send(sendPacket); 89 _socket_.receive(receivePacket); 90 91 long returnTime = System.currentTimeMillis(); 92 TimeInfo info = new TimeInfo(recMessage, returnTime, false); 94 95 return info; 96 } 97 98 109 public TimeInfo getTime(InetAddress host) throws IOException 110 { 111 return getTime(host, NtpV3Packet.NTP_PORT); 112 } 113 114 121 public int getVersion() 122 { 123 return _version; 124 } 125 126 132 public void setVersion(int version) 133 { 134 _version = version; 135 } 136 137 } 138 | Popular Tags |