home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / s / slurp103.zip / TIME.C < prev   
C/C++ Source or Header  |  1992-12-20  |  1KB  |  62 lines

  1. /*
  2.  * time - obtain the time from the remote server
  3.  *
  4.  * Copyright (C) 1992 Stephen Hebditch. All rights reserved.
  5.  * TQM Communications, BCM Box 225, London, WC1N 3XX.
  6.  * steveh@orbital.demon.co.uk  +44 836 825962
  7.  *
  8.  * See README for more information and disclaimers
  9.  *
  10.  * Obtain the current time from the remote server in standard unix time
  11.  * format for use with the next NEWNEWS. If the client is unable to
  12.  * connect to the time server or the read fails then the error is
  13.  * reported and the program is exited.
  14.  *
  15.  * 1.00   7 Aug 92  SH  Initial coding.
  16.  *
  17.  */
  18.  
  19. #include "slurp.h"
  20.  
  21. #include <unistd.h>
  22. #include <netinet/in.h>
  23.  
  24.  
  25.     time_t
  26. server_time (char *hostname)
  27.     {
  28.     int server, ret;
  29.     u_long timebuf;
  30.  
  31. /* First open the socket */
  32.  
  33.     if ((server = tcp_open (hostname, "time")) < 0)
  34.         return ((time_t) 0);
  35.  
  36.     ret = read (server, &timebuf, 4);
  37.  
  38. /* Close the socket and check we got 4 bytes */
  39.  
  40.     (void) close (server);
  41.  
  42.     if (ret != 4)
  43.         {
  44.         log_ret ("server_time: Read error on time server socket");
  45.         return ((time_t) 0);
  46.         }
  47.  
  48. /* Convert byte order if needed */
  49.  
  50.     timebuf = ntohl (timebuf);
  51.  
  52.     if (debug_flag)
  53.         (void) fprintf (stderr, "time is currently %ld at server %s\n",
  54.                         timebuf, hostname);
  55.  
  56. /* Convert the time from seconds since 1900 to seconds since 1970 */
  57.  
  58.     return ((time_t) (timebuf - 2208988800));
  59.     }
  60.  
  61. /* END-OF-FILE */
  62.