ntpclient.h 846 B

1234567891011121314151617181920212223242526272829303132333435
  1. #ifndef NTPCLIENT_H
  2. #define NTPCLIENT_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <errno.h>
  12. #define NTP_PORT 123
  13. #define NTP_PACKET_SIZE 48
  14. // 定义NTP客户端类
  15. typedef struct NTPClient_s {
  16. int sockfd;
  17. struct sockaddr_in server_addr;
  18. char ntp_packet[NTP_PACKET_SIZE];
  19. } NTPClient;
  20. NTPClient* createNTPClient(const char* ntp_server, int port);
  21. // 销毁NTP客户端实例
  22. void destroyNTPClient(NTPClient* client) ;
  23. // 从NTP服务器获取当前时间
  24. int getCurrentTimeFromNTP(NTPClient* client, time_t* current_time) ;
  25. // 根据时间获取星期信息
  26. int getCurrentWeekday(time_t current_time,char *weekday) ;
  27. #endif // NTPCLIENT_H