home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / WATTCP.ZIP / APPS / DAYTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-12  |  3.0 KB  |  123 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 <tcp.h>
  33.  
  34.  
  35.  
  36. #define DAYTIME_PORT 13
  37.  
  38. daytime(host)
  39. longword host;
  40. {
  41.     tcp_Socket telsock;
  42.     static tcp_Socket *s;
  43.     char buffer[ 513 ];
  44.     int retcode = 3;
  45.     int status;
  46.     int udpretries = 3;
  47.     long udpretrytime;
  48.     int len;
  49.  
  50.     s = &telsock;
  51.     status = 0;
  52. #ifdef TCP_DAYTIME
  53.     if (!tcp_open( s, 0, host, DAYTIME_PORT, NULL )) {
  54.     puts("Sorry, unable to connect to that machine right now!");
  55.         return( 3 );
  56.     }
  57.     printf("waiting...\r");
  58.     sock_wait_established(s, sock_delay , NULL, &status);
  59.     printf("connected \n");
  60. #else
  61.     if (!udp_open( s, 0, host, DAYTIME_PORT, NULL )) {
  62.     puts("Sorry, unable to connect to that machine right now!");
  63.         return( 3 );
  64.     }
  65.     sock_write( s, "\n", 1 );
  66.     udpretrytime = set_timeout( 2 );
  67. #endif TCP_DAYTIME
  68.  
  69.     while ( 1 ) {
  70.         sock_tick( s, &status );
  71.  
  72. #ifndef TCP_DAYTIME
  73.         if ( chk_timeout( udpretrytime )) {
  74.             if ( udpretries-- == 0 ) break;
  75.             udpretrytime = set_timeout( 2 );
  76.             sock_write( s, "\n", 1 );
  77.         }
  78. #endif
  79.     if (sock_dataready( s ) ) {
  80.         sock_gets( s, buffer, sizeof( buffer ));
  81.         puts( buffer );
  82.             retcode = 0;
  83.         break;
  84.     }
  85.     }
  86.     sock_close( s );
  87.     sock_wait_closed( s, sock_delay, NULL, &status );
  88.     
  89. sock_err:
  90.     switch (status) {
  91.     case 1 : /* foreign host closed */
  92.                  return(retcode);
  93.     case -1: /* timeout */
  94.          printf("\nConnection timed out!");
  95.                  return(2);
  96.     default: printf("Aborting");
  97.                  return(2);
  98.     }
  99. }
  100.  
  101.  
  102. main(int argc, char **argv )
  103. {
  104.     int status;
  105.     longword host;
  106.  
  107.     if (argc != 2) {
  108.     puts("   DAYTIME server");
  109.     exit( 3 );
  110.     }
  111.  
  112.     sock_init();
  113.  
  114.     if ( host = resolve( argv[1]))
  115.     status = daytime( host );
  116.     else {
  117.     printf("Could not resolve host '%s'\n", argv[1]);
  118.     status = 3;
  119.     }
  120.  
  121.     exit( status );
  122. }
  123.