为什么 NTP 无法计算延迟时间
Why NTP could not compute delay time
我正在使用公共网络库中的 NTP 来同步我的 Android 应用程序的时间。我尝试使用此代码获得延迟:
public static final String TIME_SERVER = "time-a.nist.gov";
public static long getCurrentNetworkTime() throws IOException {
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); // server time
Log.d("TAG", "delay: " + timeInfo.getDelay() + " time:" + returnTime
+ " local time:" + System.currentTimeMillis());
return returnTime;
}
但是 timeInfo.getDelay()
我得到 null
。根据此方法的文档,它可能不可用:
/**
* Get round-trip network delay. If null then could not compute the delay.
*
* @return Long or null if delay not available.
*/
public Long getDelay()
{
return _delay;
}
为什么不能计算延迟?
我的问题通过覆盖 NTPUDPClient 解决了,复制此 class 的代码并更改 TimeInfo
中的参数以获取详细信息:
TimeInfo info = new TimeInfo(recMessage, returnTime, true);
这是 MyNTPUDPClient
class,用它代替 NTPUDPClient
:
public final class MyNTPUDPClient extends DatagramSocketClient {
public static final int DEFAULT_PORT = 123;
private int _version = NtpV3Packet.VERSION_3;
public TimeInfo getTime(InetAddress host, int port) throws IOException {
// if not connected then open to next available UDP port
if (!isOpen()) {
open();
}
NtpV3Packet message = new NtpV3Impl();
message.setMode(NtpV3Packet.MODE_CLIENT);
message.setVersion(_version);
DatagramPacket sendPacket = message.getDatagramPacket();
sendPacket.setAddress(host);
sendPacket.setPort(port);
NtpV3Packet recMessage = new NtpV3Impl();
DatagramPacket receivePacket = recMessage.getDatagramPacket();
TimeStamp now = TimeStamp.getCurrentTime();
message.setTransmitTime(now);
_socket_.send(sendPacket);
_socket_.receive(receivePacket);
long returnTime = System.currentTimeMillis();
TimeInfo info = new TimeInfo(recMessage, returnTime, true);
return info;
}
public TimeInfo getTime(InetAddress host) throws IOException {
return getTime(host, NtpV3Packet.NTP_PORT);
}
public int getVersion() {
return _version;
}
public void setVersion(int version) {
_version = version;
}
}
我正在使用公共网络库中的 NTP 来同步我的 Android 应用程序的时间。我尝试使用此代码获得延迟:
public static final String TIME_SERVER = "time-a.nist.gov";
public static long getCurrentNetworkTime() throws IOException {
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); // server time
Log.d("TAG", "delay: " + timeInfo.getDelay() + " time:" + returnTime
+ " local time:" + System.currentTimeMillis());
return returnTime;
}
但是 timeInfo.getDelay()
我得到 null
。根据此方法的文档,它可能不可用:
/**
* Get round-trip network delay. If null then could not compute the delay.
*
* @return Long or null if delay not available.
*/
public Long getDelay()
{
return _delay;
}
为什么不能计算延迟?
我的问题通过覆盖 NTPUDPClient 解决了,复制此 class 的代码并更改 TimeInfo
中的参数以获取详细信息:
TimeInfo info = new TimeInfo(recMessage, returnTime, true);
这是 MyNTPUDPClient
class,用它代替 NTPUDPClient
:
public final class MyNTPUDPClient extends DatagramSocketClient {
public static final int DEFAULT_PORT = 123;
private int _version = NtpV3Packet.VERSION_3;
public TimeInfo getTime(InetAddress host, int port) throws IOException {
// if not connected then open to next available UDP port
if (!isOpen()) {
open();
}
NtpV3Packet message = new NtpV3Impl();
message.setMode(NtpV3Packet.MODE_CLIENT);
message.setVersion(_version);
DatagramPacket sendPacket = message.getDatagramPacket();
sendPacket.setAddress(host);
sendPacket.setPort(port);
NtpV3Packet recMessage = new NtpV3Impl();
DatagramPacket receivePacket = recMessage.getDatagramPacket();
TimeStamp now = TimeStamp.getCurrentTime();
message.setTransmitTime(now);
_socket_.send(sendPacket);
_socket_.receive(receivePacket);
long returnTime = System.currentTimeMillis();
TimeInfo info = new TimeInfo(recMessage, returnTime, true);
return info;
}
public TimeInfo getTime(InetAddress host) throws IOException {
return getTime(host, NtpV3Packet.NTP_PORT);
}
public int getVersion() {
return _version;
}
public void setVersion(int version) {
_version = version;
}
}