ntpclient.h 907 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. typedef struct NTPClient_s {
  15. int sockfd;
  16. struct sockaddr server_addr;
  17. char ntp_packet[NTP_PACKET_SIZE];
  18. struct addrinfo *res;
  19. int flag;
  20. char *server;
  21. int port;
  22. } NTPClient;
  23. NTPClient* createNTPClient(const char* ntp_server, int port, int timeout_sec);
  24. // 销毁NTP客户端实例
  25. void destroyNTPClient(NTPClient* client) ;
  26. // 从NTP服务器获取当前时间
  27. int getCurrentTimeFromNTP(NTPClient* client, time_t* current_time) ;
  28. // 根据时间获取星期信息
  29. int getCurrentWeekday(time_t current_time,char *weekday) ;
  30. #endif // NTPCLIENT_H