Question: Using NTP _ Client.c and LocalTime _ Client.c examples, create a single program that obtains the current time from NTP and from the local computer.
Using NTPClient.c and LocalTimeClient.c examples, create a single program that obtains the current time from NTP and from the local computer. Print both times and print the difference between the two times. On most systems, this should be a very small number as most systems sync their time via NTP However, the exercise will give you an appreciation of how much local time can slip in between time
synchronization with an external clock.
NTPclient.c
#include
#include
#include
#include
#include
#include
#include
#include
#define NTPSERVER "pool.ntporg"
#define NTPPORT
#define NTPPACKETSIZE
#define NTPTIMESTAMPDELTA ull
typedef struct
uintt livnmode;
uintt stratum;
uintt poll;
uintt precision;
uintt rootdelay;
uintt rootdispersion;
uintt referenceid;
uintt reftssec;
uintt reftsfrac;
uintt origtssec;
uintt origtsfrac;
uintt recvtssec;
uintt recvtsfrac;
uintt transtssec;
uintt transtsfrac;
ntppacket;
void errorconst char msg
perrormsg;
exitEXITFAILURE;
int main
int sockfd, n;
struct sockaddrin servaddr;
ntppacket packet;
sockfd socketAFINET, SOCKDGRAM, IPPROTOUDP;
if sockfd
errorERROR opening socket";
memset&servaddr, sizeofservaddr;
servaddr.sinfamily AFINET;
servaddr.sinport htonsNTPPORT;
if inetptonAFINET, NTPSERVER, &servaddr.sinaddr
errorERROR invalid address";
memset&packet, sizeofpacket;
packet.livnmode xxx;
if sendtosockfd &packet, sizeofpacketstruct sockaddr &servaddr, sizeofservaddr
errorERROR sending packet";
n recvsockfd &packet, sizeofpacket;
if n
errorERROR receiving response";
closesockfd;
timet currenttime ntohlpackettranstssec NTPTIMESTAMPDELTA;
printfNTP time: s ctimettime;
return ;
LocalTimeClient.C
#include
#include
int main
timet currenttime;
struct tm localtime;
Get current time
currenttime timeNULL;
Convert to local time
localtime localtimettime;
Print local time
printfLocal time: s asctimelocaltime;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
