├── README.md ├── .gitignore ├── NTPClient.h ├── LICENSE └── NTPClient.cpp /README.md: -------------------------------------------------------------------------------- 1 | NetworkTimeProtocol 2 | =================== 3 | 4 | A C++ Library to use the Network Time Protocol 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | -------------------------------------------------------------------------------- /NTPClient.h: -------------------------------------------------------------------------------- 1 | #ifndef NTPClIENT_H 2 | #define NTPCLIENT_H 3 | 4 | #include 5 | #include 6 | 7 | //Components of the Boost Library 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | namespace MultipleKinectsPlatformServer{ 14 | 15 | /** 16 | * A Network Time Protocol Client that queries the DateTime from the Time Server located at hostname 17 | */ 18 | class NTPClient{ 19 | private: 20 | string _host_name; 21 | unsigned short _port; 22 | public: 23 | NTPClient(string i_hostname); 24 | long RequestDatetime_UNIX(); 25 | }; 26 | 27 | } 28 | 29 | #endif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Ethan Lim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /NTPClient.cpp: -------------------------------------------------------------------------------- 1 | #include "NTPClient.h" 2 | 3 | namespace MultipleKinectsPlatformServer{ 4 | 5 | /** 6 | * NTPClient 7 | * @Param i_hostname - The time server host name which you are connecting to obtain the time 8 | * eg. the pool.ntp.org project virtual cluster of timeservers 9 | */ 10 | NTPClient::NTPClient(string i_hostname) 11 | :_host_name(i_hostname),_port(123) 12 | { 13 | //Host name is defined by you and port number is 123 for time protocol 14 | } 15 | 16 | /** 17 | * RequestDatetime_UNIX() 18 | * @Returns long - number of seconds from the Unix Epoch start time 19 | */ 20 | long NTPClient::RequestDatetime_UNIX(){ 21 | 22 | time_t timeRecv; 23 | 24 | boost::asio::io_service io_service; 25 | 26 | boost::asio::ip::udp::resolver resolver(io_service); 27 | boost::asio::ip::udp::resolver::query query( 28 | boost::asio::ip::udp::v4(), 29 | this->_host_name, 30 | "ntp"); 31 | 32 | boost::asio::ip::udp::endpoint receiver_endpoint = *resolver.resolve(query); 33 | 34 | boost::asio::ip::udp::socket socket(io_service); 35 | socket.open(boost::asio::ip::udp::v4()); 36 | 37 | boost::array sendBuf = {010,0,0,0,0,0,0,0,0}; 38 | 39 | socket.send_to(boost::asio::buffer(sendBuf), receiver_endpoint); 40 | 41 | boost::array recvBuf; 42 | boost::asio::ip::udp::endpoint sender_endpoint; 43 | 44 | try{ 45 | size_t len = socket.receive_from( 46 | boost::asio::buffer(recvBuf), 47 | sender_endpoint 48 | ); 49 | 50 | timeRecv = ntohl((time_t)recvBuf[4]); 51 | 52 | timeRecv-= 2208988800U; //Unix time starts from 01/01/1970 == 2208988800U 53 | 54 | }catch (std::exception& e){ 55 | 56 | std::cerr << e.what() << std::endl; 57 | 58 | } 59 | 60 | return (long)timeRecv; 61 | } 62 | 63 | } --------------------------------------------------------------------------------