我试图实现一个NTP客户端,以便在带有armbian(Debian 9.6 Stretch)操作系统的cubietruck板(ARM®Cortex™-A7双核)上计算偏移和延迟。我使用了Poco库(版本1.7.8p2)和类似于poco提供的示例的代码。以下是main.cpp:
#include <sstream>
#include <iostream>
#include "NTPClientTest.h"
using namespace std;
using namespace NTP;
void NtpServerSynchronization::ntpTimeSync()
{
_ntpClient.request("pool.ntp.org");
}
void NtpServerSynchronization::onResponse(const void* pSender, NTPEventArgs& args)
{
auto t1 = args.packet().originateTime().epochMicroseconds();
auto t2 = args.packet().receiveTime().epochMicroseconds();
auto t3 = args.packet().transmitTime().epochMicroseconds();
}
int main()
{
try {
NtpServerSynchronization ntp;
ntp.ntpTimeSync();
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
}
}
标题NTPClientTest包含以下内容:
#include <Poco/Net/NTPClient.h>
#include <Poco/Net/NTPEventArgs.h>
#include <Poco/Delegate.h>
using namespace Poco;
using namespace Poco::Net;
using namespace std;
namespace NTP {
class NtpServerSynchronization
/// Class for time synchronization with ntp server
{
public:
NtpServerSynchronization() :
_ntpClient(IPAddress::IPv4)
{
_ntpClient.response += Delegate<NtpServerSynchronization, NTPEventArgs>(this, &NtpServerSynchronization::onResponse);
}
~NtpServerSynchronization()
{
_ntpClient.response -= Delegate<NtpServerSynchronization, NTPEventArgs>(this, &NtpServerSynchronization::onResponse);
}
void ntpTimeSync();
void onResponse(const void* pSender, NTPEventArgs& args);
private:
NTPClient _ntpClient;
SocketAddress _sa;
};
}
但是我得到的值是:
t1 = 2085978496000000
t2 = 1549296374000000
t3 = 1549296374000000
从上面可以看到,t1与有效的纪元时间戳不对应,并且t2 == t3意味着ReceiveTime和sendTime相同,无论使用哪个ntp服务器,我都得到相似的结果。 关于t1,我在做什么错?t2,t3的相等性是否合乎逻辑?