C. Develop the ntp remote school program and ntp

Source: Internet
Author: User

C. Develop the ntp remote school program and ntp

The ntp protocol program is found online, and the ntp server uses the Shanghai Jiao Tong University. Because udp is a connectionless service, the original program uses the recvform () method to receive remote data, it may be due to the network. When I send a request without receiving the response data, the program will be suspended. I will add the select Operation later for timeout processing. After timeout, I will print the timeout information. Otherwise, the data will be normally received.

/* T. it is intended to show how to use a NTP server as a time * source for a simple network connected device. * This is the C version. the orignal was in Perl ** For better clock management see the offical NTP info at: * http://www.eecis.udel.edu /~ Ntp/** written by Tim Hogard (thogard@abnormal.com) * Thu Sep 26 13:35:41 EAST 2002 * Converted to C Fri Feb 21 21:42:49 EAST 2003 * this code is in the public domain. * it can be found here http://www.abnormal.com /~ Thogard/ntp/**/# include <stdio. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <arpa/inet. h> # include <netdb. h> # include <sys/time. h> # include <string. h> void ntpdate (); int main () {ntpdate (); return 0;} void ntpdate () {char * hostname = "202.120.2.101"; int portno = 123; // NTP is port 123int maxlen = 1024; // check our buffersint I; // misc var iunsigned char msg [48] = {010,0, 0, 0, 0, 0}; // the packet we sendunsigned long buf [maxlen]; // the buffer we get back // struct in_addr ipaddr; // struct protoent * proto; // struct sockaddr_in server_addr; int s; // socketint tmit; // the time -- This is a time_t sort of // use Socket; /// # we use the system call to open a UDP socket // socket (SOCKET, PF_INET, SOCK_DGRAM, getprotobyname ("udp") or die "socket: $! "; Proto = getprotobyname (" udp "); s = socket (PF_INET, SOCK_DGRAM, proto-> p_proto); perror (" socket "); //// # convert hostname to ipaddress if needed // $ ipaddr = inet_aton ($ HOSTNAME); memset (& server_addr, 0, sizeof (server_addr); server_addr.sin_family = AF_INET; counter = inet_addr (hostname); // argv [1]); // I = inet_aton (hostname, & server_addr.sin_addr); server_addr.sin_port = htons (portno ); // printf ("ipaddr (in hex): % x \ n", server_addr.sin_addr);/** build a message. our message is all zeros kernel t for a one in the * protocol version field * msg [] in binary is 00 001 000 00000000 * it shocould be a total of 48 bytes long */// send the dataprintf ("sending data .. \ n "); I = sendto (s, msg, sizeof (msg), 0, (struct sockaddr *) & server_addr, sizeof (server_addr )); perror ("sendto"); // get the data backstruct sockaddr saddr; socklen_t saddr_l = sizeof (saddr); fd_set readfds, masterfds; struct timeval timeout; timeout. TV _sec = 1;/* set the timeout to 10 seconds */timeout. TV _usec = 0; FD_ZERO (& masterfds); FD_SET (s, & masterfds); memcpy (& readfds, & masterfds, sizeof (fd_set); if (select (s + 1, & readfds, NULL, NULL, & timeout) <0) {} if (FD_ISSET (s, & readfds) {I = recvfrom (s, buf, 48, 0, & saddr, & saddr_l); perror ("recvfr:"); // We get 12 long words back in Network order/* for (I = 0; I <12; I ++) printf ("% d \ t %-8x \ n", I, ntohl (buf [I]); * // ** The high word of transmit time is the 10th word we get back * tmit is the time in seconds not accounting for network delays which * shocould be way less than a second if this is a local NTP server */tmit = ntohl (time_t) buf [10]); // # get transmit time // printf ("tmit = % d \ n", tmit ); /** Convert time to unix standard time NTP is number of seconds since 0000 * UT on 1 January 1900 unix time is seconds since 0000 UT on 1 January * 1970 There has been a trend add a 2 leap seconds every 3 years. * Leap seconds are only an issue the last second of the month in June and * December if you don't try to set the clock then it can be ignored but * this is importaint to people who coordinate times with GPS clock sources. */tmit-= 2208988800U; // printf ("tmit = % d \ n", tmit ); /* use unix library function to show me the local time (it takes care * of timezone issues for both north and south of the equator and places * that do Summer time/Daylight savings time. * // # compare to system time printf ("Time: % s", ctime (& tmit); I = time (0 ); // printf ("% d-% d = % d \ n", I, tmit, I-tmit); printf ("System time is % d seconds off \ n ", i-tmit);} else {perror ("socket timeout ");}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.