ntpclient.h 882 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. int flag;
  19. char *server;
  20. int port;
  21. } NTPClient;
  22. NTPClient* createNTPClient(const char* ntp_server, int port, int timeout_sec);
  23. // 销毁NTP客户端实例
  24. void destroyNTPClient(NTPClient* client) ;
  25. // 从NTP服务器获取当前时间
  26. int getCurrentTimeFromNTP(NTPClient* client, time_t* current_time) ;
  27. // 根据时间获取星期信息
  28. int getCurrentWeekday(time_t current_time,char *weekday) ;
  29. #endif // NTPCLIENT_H