找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3735|回复: 0
打印 上一主题 下一主题
收起左侧

嵌入式系统NTP时间同步软件例子程序

[复制链接]
跳转到指定楼层
楼主
ID:238297 发表于 2017-10-10 15:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
嵌入式系统NTP时间同步软件例子程序
source code
  1. /* This code will query a ntp server for the local time and display

  2. * it.  it is intended to show how to use a NTP server as a time
  3. * source for a simple network connected device.
  4. * This is the C version.  The orignal was in Perl
  5. *
  6. * For better clock management see the offical NTP info at:
  7. * http://www点eecis点udel.edu/~ntp/
  8. *
  9. * written by Tim Hogard (thogard@abnormal.com)
  10. * Thu Sep 26 13:35:41 EAST 2002
  11. * Converted to C Fri Feb 21 21:42:49 EAST 2003
  12. * this code is in the public domain.
  13. * it can be found here http://www点abnormal点com/~thogard/ntp/
  14. *
  15. */
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <netdb.h>
  22. #include <time.h>
  23. #include <string.h>

  24. void ntpdate();

  25. int main() {
  26.     ntpdate();
  27.     return 0;
  28. }

  29. void ntpdate() {
  30. char *hostname="192.168.15.100";
  31. int portno=123;     //NTP is port 123
  32. int maxlen=1024;        //check our buffers
  33. int i;          // misc var i
  34. unsigned char msg[48]={010,0,0,0,0,0,0,0,0};    // the packet we send
  35. unsigned long  buf[maxlen]; // the buffer we get back
  36. //struct in_addr ipaddr;        //  
  37. struct protoent *proto;     //
  38. struct sockaddr_in server_addr;
  39. int s;  // socket
  40. int tmit;   // the time -- This is a time_t sort of

  41. //use Socket;
  42. //
  43. //#we use the system call to open a UDP socket
  44. //socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
  45. proto=getprotobyname("udp");
  46. s=socket(PF_INET, SOCK_DGRAM, proto->p_proto);
  47. perror("socket");
  48. //
  49. //#convert hostname to ipaddress if needed
  50. //$ipaddr   = inet_aton($HOSTNAME);
  51. memset( &server_addr, 0, sizeof( server_addr ));
  52. server_addr.sin_family=AF_INET;
  53. server_addr.sin_addr.s_addr = inet_addr(hostname);
  54. //argv[1] );
  55. //i   = inet_aton(hostname,&server_addr.sin_addr);
  56. server_addr.sin_port=htons(portno);
  57. //printf("ipaddr (in hex): %x\n",server_addr.sin_addr);

  58. /*
  59. * build a message.  Our message is all zeros except for a one in the
  60. * protocol version field
  61. * msg[] in binary is 00 001 000 00000000
  62. * it should be a total of 48 bytes long
  63. */

  64. // send the data
  65. printf("sending data..\n");
  66. i=sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(server_addr));
  67. perror("sendto");
  68. // get the data back
  69. struct sockaddr saddr;
  70. socklen_t saddr_l = sizeof (saddr);
  71. i=recvfrom(s,buf,48,0,&saddr,&saddr_l);
  72. perror("recvfr:");

  73. //We get 12 long words back in Network order

  74. for(i=0;i<48;i++)
  75.     //printf("%d\t%-8x\n",i,ntohl(buf[i]));
  76.     printf("%02X-", buf[i]);


  77. /*
  78. * The high word of transmit time is the 10th word we get back
  79. * tmit is the time in seconds not accounting for network delays which
  80. * should be way less than a second if this is a local NTP server
  81. */

  82. tmit=ntohl((time_t)buf[10]);    //# get transmit time
  83. printf("\ntmit=%d\n",tmit);

  84. /*
  85. * Convert time to unix standard time NTP is number of seconds since 0000
  86. * UT on 1 January 1900 unix time is seconds since 0000 UT on 1 January
  87. * 1970 There has been a trend to add a 2 leap seconds every 3 years.
  88. * Leap seconds are only an issue the last second of the month in June and
  89. * December if you don't try to set the clock then it can be ignored but
  90. * this is importaint to people who coordinate times with GPS clock sources.
  91. */

  92. tmit-= 2208988800U;
  93. printf("tmit=%d\n",tmit);
  94. /* use unix library function to show me the local time (it takes care
  95. * of timezone issues for both north and south of the equator and places
  96. * that do Summer time/ Daylight savings time.
  97. */


  98. //#compare to system time
  99. printf("Time: %s",ctime(&tmit));
  100. i=time(0);
  101. //printf("%d-%d=%d\n",i,tmit,i-tmit);
  102. printf("System time is %d seconds off\n",i-tmit);
  103. }
复制代码

全部资料下载地址:
ntpdate.rar (1.84 KB, 下载次数: 48)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表