📄 testsntp.cpp
字号:
#include "stdafx.h"
#include "sntp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
class CSNTPApp : public CWinApp
{
public:
//Constructors / Destructors
CSNTPApp();
//Methods
//{{AFX_VIRTUAL(CSNTPApp)
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
//{{AFX_MSG(CSNTPApp)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
class CMyCommandLineInfo : public CCommandLineInfo
{
public:
//Constructors / Destructors
CMyCommandLineInfo();
//Methods
virtual void ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast);
//Member variables
BOOL m_bSet;
};
BEGIN_MESSAGE_MAP(CSNTPApp, CWinApp)
//{{AFX_MSG_MAP(CSNTPApp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CSNTPApp::CSNTPApp()
{
}
//The one and only MFC app instance
CSNTPApp theApp;
CMyCommandLineInfo::CMyCommandLineInfo()
{
m_bSet = FALSE;
}
void CMyCommandLineInfo::ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast)
{
if (bFlag)
{
if (_tcsicmp(lpszParam, _T("s")) == 0)
m_bSet = TRUE;
}
//Let the parent class do its stuff
CCommandLineInfo::ParseParam(lpszParam, bFlag, bLast);
}
BOOL CSNTPApp::InitInstance()
{
//Handle the command line
CMyCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
//If there is not host specified, then log the error
if (cmdInfo.m_strFileName.IsEmpty())
{
_tprintf(_T("testsntp host [/s]\n"));
_tprintf(_T("where host is the NTP server address and\n"));
_tprintf(_T("/s means synchronize the time instead of just displaying the retrieved values\n"));
return FALSE;
}
//Initialise the winsock stack
if (!AfxSocketInit())
{
_tprintf(_T("Failed to load winsock stack\n"));
return FALSE;
}
//Do the actual SNTP Query
CSNTPClient sntp;
NtpServerResponse response;
if (sntp.GetServerTime(cmdInfo.m_strFileName, response))
{
_tprintf(_T("Time was successfully retreived from NTP server\n"));
SYSTEMTIME st1 = response.m_OriginateTime;
SYSTEMTIME st2 = response.m_ReceiveTime;
SYSTEMTIME st3 = response.m_TransmitTime;
SYSTEMTIME st4 = response.m_DestinationTime;
_tprintf(_T(" DD/MM/YYYY HH:MM:SS.MS\n"));
_tprintf(_T("Client Originate Date was %02d/%02d/%04d, %02d:%02d:%02d.%03d\n"), st1.wDay, st1.wMonth, st1.wYear, st1.wHour, st1.wMinute, st1.wSecond, st1.wMilliseconds);
_tprintf(_T("Server Receive Date was %02d/%02d/%04d, %02d:%02d:%02d.%03d\n"), st2.wDay, st2.wMonth, st2.wYear, st2.wHour, st2.wMinute, st2.wSecond, st2.wMilliseconds);
_tprintf(_T("Server Transmit Date was %02d/%02d/%04d, %02d:%02d:%02d.%03d\n"), st3.wDay, st3.wMonth, st3.wYear, st3.wHour, st3.wMinute, st3.wSecond, st3.wMilliseconds);
_tprintf(_T("Client Destination Date was %02d/%02d/%04d, %02d:%02d:%02d.%03d\n"), st4.wDay, st4.wMonth, st4.wYear, st4.wHour, st4.wMinute, st4.wSecond, st4.wMilliseconds);
_tprintf(_T("Round trip delay was %f seconds\n"), response.m_RoundTripDelay);
_tprintf(_T("Local clock offset was %f seconds\n"), response.m_LocalClockOffset);
if (cmdInfo.m_bSet)
{
if (fabs(response.m_LocalClockOffset) < 3600)
{
CNtpTime newTime(CNtpTime::GetCurrentTime() + response.m_LocalClockOffset);
if (sntp.SetClientTime(newTime))
_tprintf(_T("time was successfully synchronised\n"));
else
_tprintf(_T("Failed to set the local time\n"));
}
else
{
_tprintf(_T("Time difference was greater was 1 hour, not synchronizing clock\n"));
_tprintf(_T("You should first set the time manually to the approximate time\n"));
}
}
}
else
_tprintf(_T("Failed to retreive time from NTP server, GetLastError returned %d\n"), GetLastError());
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -