Sfoglia il codice sorgente

【完善】ntp 代码细节格式。

Signed-off-by: armink <armink.ztl@gmail.com>
armink 7 anni fa
parent
commit
7228ac6680
1 ha cambiato i file con 6 aggiunte e 6 eliminazioni
  1. 6 6
      ntp/ntp.c

+ 6 - 6
ntp/ntp.c

@@ -18,12 +18,12 @@
  *
  */
 
-#include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/select.h>
 #include <netinet/in.h>
 #include <netdb.h>
 #include <rtthread.h>
@@ -102,7 +102,7 @@ time_t ntp_get_time(void)
 
     int portno = 123; // NTP UDP port number.
 
-    char* host_name = NTP_HOSTNAME; // NTP server host-name.
+    char *host_name = NTP_HOSTNAME; // NTP server host-name.
 
     time_t new_time = 0;
 
@@ -127,7 +127,7 @@ time_t ntp_get_time(void)
 
     if (sockfd < 0) {
         ntp_error("opening socket");
-        goto __exit;
+        return 0;
     }
 
     server = gethostbyname(host_name); // Convert URL to IP.
@@ -160,7 +160,7 @@ time_t ntp_get_time(void)
 
     // Send it the NTP packet it wants. If n == -1, it failed.
 
-    n = write(sockfd, (char*) &packet, sizeof(ntp_packet));
+    n = send(sockfd, (char*) &packet, sizeof(ntp_packet), 0);
 
     if (n < 0) {
         ntp_error("writing to socket");
@@ -180,7 +180,7 @@ time_t ntp_get_time(void)
 
     // Wait and receive the packet back from the server. If n == -1, it failed.
 
-    n = read(sockfd, (char*) &packet, sizeof(ntp_packet));
+    n = recv(sockfd, (char*) &packet, sizeof(ntp_packet), 0);
 
     if (n < 0) {
         ntp_error("reading from socket");
@@ -203,7 +203,7 @@ time_t ntp_get_time(void)
 
 __exit:
 
-    close(sockfd);
+    closesocket(sockfd);
 
     return new_time;
 }