CPJNSNTPClient
v1.25 An C++ class to encapsulate the SNTP protocol
Included is an C++ class to support the SNTP protocol. SNTP for those not familiar
with all the internet protocols is the protocol used to support time synchronization.
Features
- Simple and clean C++ interface.
- The interface provided is synchronous which provides an easier programming
model than using asynchronous sockets.
- A configurable timeout for the connection can be set through the class API.
- The classes are fully Unicode compliant and include Unicode build configurations
in the project solution file.
The enclosed zip file contains the CPJNSNTPClient
source code and a simple VC2017 console program which exercises the class.
Copyright
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
Updates
v1.25 (27 April 2022)
- Updated copyright details.
- Updated the code to use C++ uniform initialization for all variable
declarations.
v1.24 (3 May 2020)
- Fixed various Clang-Tidy static code analysis warnings in the code.
v1.23 (12 March 2020)
- Updated copyright details.
- Fixed various Clang-Tidy static code analysis warnings in the code.
v1.22 (20 December 2019)
- Fixed various Clang-Tidy static code analysis warnings in the code.
- Updated initialization of various structs to use C++ 11 list
initialization.
v1.21 (30 October 2019)
- Fixed a compiler warning in the EnableSetTimePrivilege method. Thanks to
Mai Duc Hung for reporting this issue.
- Changed macro references of "CWSOCKET_MFC_EXTENSTIONS" to
"CWSOCKET_MFC_EXTENSIONS". Thanks to Mai Duc Hung for reporting this issue.
v1.20 (28 October 2019)
- Updated the code to correctly handle SNTP timestamps greater than the
first SNTP epoch date of 6:28:16AM on 7 February 2036. Thanks to Mai Duc
Hung for reporting this issue.
- Replaced BOOL with bool throughout the code.
v1.19 (16 September 2019)
- Fixed a number of compiler warnings when the code is compiled with VS
2019 Preview
v1.18 (1 June 2019)
- Updated copyright details.
- Updated the code to clean compile on VC 2019
v1.17 (19 August 2018)
- Updated copyright details.
- Fixed a number of C++ core guidelines compiler warnings. These changes
mean that the code will now only compile on VC 2017 or later.
- Removed PJNSNTP_NO_PRIVILEGE_CODE preprocessor from codebase
v1.16 (18 January 2016)
- Updated copyright details.
- Verified the code compiles cleanly in VC 2015
- Added SAL annotations to all the code
v1.15 (6 April 2015)
- Updated the code to clean compile on VC 2010 and later
- Updated the code to compile with the latest version of the CWSocket class
- Removed all the code which refers to a proxy connection
- Updated the code to operate optionally independent of MFC. If the preprocessor
value "CWSOCKET_MFC_EXTENSTIONS" is not defined then the SNTP code
will not use MFC, while if it is defined then the code will use MFC. This preprocessor
value is used to control the MFC mode of the CWSocket class.
- CPJNSNTPClient::SetClientTime now preserves the last error value and the
sample app now reports this value.
- Optimized the implementation of CPJNNTPTime::MsToNtpFraction, NtpFractionToMs &
NtpFractionToSecond methods. These optimizations have got rid of the the need
for the previous m_MsToNTP lookup table.
- Reworked the CPJNNTPTime::CPJNNTPTime(const SYSTEMTIME& st) constructor
to use SystemTimeToFileTime Windows API
- Reworked the CPJNNTPTime::operator SYSTEMTIME() method to use FileTimeToSystemTime
Windows API
- The sample app now displays the stratum and leap indicator values from the
received SNTP response.
v1.14 (16 March 2009)
- Updated copyright details.
- Included missing VC 2005 solution and project files in the download. Thanks
to Michael Haephrati for reporting this issue.
v1.13 (11 July 2008)
- Updated copyright details.
- Updated sample app to clean compile on VC 2008
- Removed VC 6 style classwizard comments from the code.
- The code has now been updated to support VC 2005 or later only.
- Code now compiles cleanly using Code Analysis (/analyze)
v1.12 (5 July 2006)
- Updated copyright details.
- Updated the documentation to use the same style as the web site.
- Code now uses newer C++ style casts instead of C style casts.
- Optimized CPJNNTPTime constructor code.
- Optimized CPJNSNTPClient constructor code
- Replaced calls to ZeroMemory with memset
- Updated code to work with latest version of CWSocket class.
- Made the code more robust in its handling of the m_hToken variable
- Optimized the privilege handling code to avoid the need for the m_bTakenPrivilege
variable.
- Verified all code compiles cleanly on VC 2005
- Renamed PJNSNTP_NO_PRIVILEDGE_CODE macro to PJNSNTP_NO_PRIVILEGE_CODE
v1.11 (12 September 2005)
- Fixed problem where code did not handle graceful disconnects when reading
the SNTP response.
- Reworked the class to now pass exceptions rather than use a Win32 style
return value.
- Renamed the classes to use a prefix of PJN. This is in line with the SMTP
class I also provide.
v1.10 (10 July 2005)
- Undone the change made on 04-04-2005 as the calculation of the round trip
time calculation was correct. For details on the formulae used, please check
out RFC 2030. Thanks to Thomas Fejes for reporting this problem.
18 May 2005
- Updated usage section for the code.
V1.09 (4 April 2005)
- Fix for a bug in the calculation of the m_RoundTripDelay value. Thanks to
Euan Lee for reporting this bug.
V1.08 (17 March 2005)
- Updated to work with the latest release of CWSocket. Thanks to Alex Dron
for reporting this issue.
- Addition of a preprocessor macro "PJNSNTP_EXT_CLASS" which allows the code
to be easily added to an extension DLL.
- General tidy up of the code in CSNTPClient::GetServerTime
- Replaced the macro _WIN32_WCE with PJNSNTP_NO_PRIVILEDGE_CODE. This allows
the privilege code to also be excluded on desktop versions of Windows if so
desired.
- Reviewed and updated all the TRACE statements for correctness.
V1.07 (21 September 2003)
- Class now uses CWSocket class (which you will need to download separately)
to provide sockets capability. This now allows the class to support connecting
via a Socks 5 proxy.
V1.06 (13 March 2003)
- Fixed a bug in CNtpSocket::IsReadible in the setup of the timval structure.
Thanks to "InBloom Support" for reporting this.
V1.05 (25 August 2002)
- Updated sample app to work correctly for the Unicode configurations
- Sample app now uses CWinApp instance in sample app
- Sample app now uses MFC support for initializing Winsock stack
- Sample app now uses MFC support for parsing its command line.
V1.04 (29 March 2001)
- Updated copyright message
- Fixed a handle leak RevertSetTimePriviledge where the process token was
not been closed
V1.03 (25 July 2000)
- Added Windows CE 2.11 compatibility. Thanks to Andre Rippstein for the work.
V1.02 (25 June 2000)
- Fixed an issue where the packing order of the NtpBasicInfo struct was causing
problems. Now packing is set to 1 byte for this struct.
V1.01 (17 November 1998)
- m_nOriginateTime was getting set incorrectly in the SNTP response. Has now
been fixed.
- GetLastError now works when a timeout occurs
10 August 1998
- Program now includes a VC 5 make file instead of VC 6
v1.0 (9 August 1998)