Jelajahi Sumber

【删除】ntp 功能 select 依赖

Signed-off-by: chenyong <1521761801@qq.com>
chenyong 7 tahun lalu
induk
melakukan
970d3ac347
1 mengubah file dengan 9 tambahan dan 11 penghapusan
  1. 9 11
      ntp/ntp.c

+ 9 - 11
ntp/ntp.c

@@ -139,6 +139,15 @@ time_t ntp_get_time(const char *host_name)
         return 0;
         return 0;
     }
     }
 
 
+    timeout.tv_sec = NTP_GET_TIMEOUT;
+    timeout.tv_usec = 0;
+
+    /* set receive and send timeout option */
+    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeout,
+               sizeof(timeout));
+    setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeout,
+               sizeof(timeout));
+
     server = gethostbyname(host_name); // Convert URL to IP.
     server = gethostbyname(host_name); // Convert URL to IP.
 
 
     if (server == NULL) {
     if (server == NULL) {
@@ -176,17 +185,6 @@ time_t ntp_get_time(const char *host_name)
         goto __exit;
         goto __exit;
     }
     }
 
 
-    timeout.tv_sec = NTP_GET_TIMEOUT;
-    timeout.tv_usec = 0;
-
-    FD_ZERO(&readset);
-    FD_SET(sockfd, &readset);
-
-    if (select(sockfd + 1, &readset, RT_NULL, RT_NULL, &timeout) <= 0) {
-        ntp_error("select the socket timeout(5s)");
-        goto __exit;
-    }
-
     // Wait and receive the packet back from the server. If n == -1, it failed.
     // Wait and receive the packet back from the server. If n == -1, it failed.
 
 
     n = recv(sockfd, (char*) &packet, sizeof(ntp_packet), 0);
     n = recv(sockfd, (char*) &packet, sizeof(ntp_packet), 0);