久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4244|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

嵌入式系統(tǒng)NTP時(shí)間同步軟件例子程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:238297 發(fā)表于 2017-10-10 15:38 | 只看該作者 回帖獎(jiǎng)勵(lì) |正序?yàn)g覽 |閱讀模式
嵌入式系統(tǒng)NTP時(shí)間同步軟件例子程序
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點(diǎn)eecis點(diǎn)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點(diǎn)abnormal點(diǎn)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. }
復(fù)制代碼

全部資料下載地址:
ntpdate.rar (1.84 KB, 下載次數(shù): 48)

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 亚洲欧美在线一区 | 日韩中文字幕 | 成人欧美一区二区三区在线观看 | 日日噜噜噜夜夜爽爽狠狠视频97 | 第一福利社区1024 | 国产精品久久久久久久久久免费 | 国产成人一区二区三区 | 成人精品一区亚洲午夜久久久 | 国产精品地址 | 九色在线观看 | 在线观看免费av网 | 成人a免费 | 欧美日日| 日日爱av | 一区二区精品 | 欧美在线a | 午夜视频免费在线观看 | 911精品美国片911久久久 | 青青草av网站 | 一区二区影视 | 国产熟熟 | 91精品无人区卡一卡二卡三 | 在线一级片 | 国产精品久久久久久影视 | 视频一区二区中文字幕 | 九九综合| 午夜精品福利视频 | av在线免费观看不卡 | 亚洲综合久久精品 | 精品国产乱码久久久久久图片 | 欧美福利在线 | 国产99视频精品免视看9 | 欧美日韩一| 一级黄在线观看 | 蜜桃在线一区二区三区 | 国产精品美女久久久久久久网站 | 国产精品美女久久久久aⅴ国产馆 | 丁香五月网久久综合 | 久久综合久 | 二区亚洲| 五月免费视频 |