home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / CEREBRUM / WAT9609.ZIP / APPS / DAYTIME.C < prev    next >
Text File  |  1994-11-28  |  3KB  |  130 lines

  1.  
  2. /******************************************************************************
  3.     DAYTIME - read and print time of day string from internet
  4.  
  5.     Copyright (C) 1991, University of Waterloo
  6.     Portions Copyright (C) 1990, National Center for Supercomputer Applications
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it, but you may not sell it.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but without any warranty; without even the implied warranty of
  13.     merchantability or fitness for a particular purpose.
  14.  
  15.         Erick Engelke                   or via E-Mail
  16.         Faculty of Engineering
  17.         University of Waterloo          Erick@development.watstar.uwaterloo.ca
  18.         200 University Ave.,
  19.         Waterloo, Ont., Canada
  20.         N2L 3G1
  21.  
  22.     Returns:
  23.         0   - success
  24.  
  25.         2   - some failure in the connection (port unavailable,
  26.                 no response, etc.)
  27.         3   - unable to reach it - local host or first router is down
  28.  
  29. ******************************************************************************/
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <tcp.h>
  34.  
  35.  
  36.  
  37. #define DAYTIME_PORT 13
  38.  
  39. int daytime(longword host)
  40. {
  41. #ifdef TCP_DAYTIME
  42.     static tcp_Socket sock;
  43.     tcp_Socket *s;
  44. #else
  45.     static udp_Socket sock;
  46.     udp_Socket *s;
  47. #endif
  48.     char buffer[ 513 ];
  49.     int retcode = 3;
  50.     int status;
  51.     int udpretries = 3;
  52.     long udpretrytime;
  53. /*    int len; */
  54.  
  55.     s = &sock;
  56.     status = 0;
  57. #ifdef TCP_DAYTIME
  58.     if (!tcp_open( s, 0, host, DAYTIME_PORT, NULL )) {
  59.     puts("Sorry, unable to connect to that machine right now!");
  60.         return( 3 );
  61.     }
  62.     printf("waiting...\r");
  63.     sock_wait_established(s, sock_delay , NULL, &status);
  64.     printf("connected \n");
  65. #else
  66.     if (!udp_open( s, 0, host, DAYTIME_PORT, NULL )) {
  67.     puts("Sorry, unable to connect to that machine right now!");
  68.         return( 3 );
  69.     }
  70.     sock_write( s, "\n", 1 );
  71.     udpretrytime = set_timeout( 2 );
  72. #endif TCP_DAYTIME
  73.  
  74.     while ( 1 ) {
  75.         sock_tick( s, &status );
  76.  
  77. #ifndef TCP_DAYTIME
  78.         if ( chk_timeout( udpretrytime )) {
  79.             if ( udpretries-- == 0 ) break;
  80.             udpretrytime = set_timeout( 2 );
  81.             sock_write( s, "\n", 1 );
  82.         }
  83. #endif
  84.     if (sock_dataready( s ) ) {
  85.         sock_gets( s, buffer, sizeof( buffer ));
  86.         puts( buffer );
  87.             retcode = 0;
  88.         break;
  89.     }
  90.     }
  91.     sock_close( s );
  92.     sock_wait_closed( s, sock_delay, NULL, &status );
  93.     return(0);
  94.     
  95. sock_err:
  96.     switch (status) {
  97.     case 1 : /* foreign host closed */
  98.                  return(retcode);
  99.     case -1: /* timeout */
  100.          printf("\nConnection timed out!");
  101.                  return(2);
  102.     default: printf("Aborting");
  103.                  return(2);
  104.     }
  105. }
  106.  
  107.  
  108. int main(int argc, char **argv )
  109. {
  110.     int status;
  111.     longword host;
  112.  
  113.     if (argc != 2) {
  114.     puts("   DAYTIME server");
  115.     exit( 3 );
  116.     }
  117.  
  118.     sock_init();
  119.  
  120.     if ( (host = resolve( argv[1])) != 0uL )
  121.     status = daytime( host );
  122.     else {
  123.     printf("Could not resolve host '%s'\n", argv[1]);
  124.     status = 3;
  125.     }
  126.  
  127.     exit( status );
  128.     return (0);  /* not reached */
  129. }
  130.