home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / DAYTIME.C < prev    next >
Text File  |  1991-05-29  |  2KB  |  103 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. ******************************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <tcp.h>
  26.  
  27.  
  28. #define DAYTIME_PORT 13
  29.  
  30. daytime(host)
  31. longword host;
  32. {
  33.     tcp_Socket telsock;
  34.     static tcp_Socket *s;
  35.     char buffer[ 513 ];
  36.     int status;
  37.     int len;
  38.  
  39.     s = &telsock;
  40.     status = 0;
  41. #ifdef TCP_DAYTIME
  42.     if (!tcp_open( s, 0, host, DAYTIME_PORT, NULL )) {
  43.     puts("Sorry, unable to connect to that machine right now!");
  44.     return( 1 );
  45.     }
  46.     printf("waiting...\r");
  47.     sock_wait_established(s, sock_delay , NULL, &status);
  48.     printf("connected \n");
  49. #else
  50.     if (!udp_open( s, 0, host, DAYTIME_PORT, NULL )) {
  51.     puts("Sorry, unable to connect to that machine right now!");
  52.     return( 1 );
  53.     }
  54.     sock_write( s, "\n", 1 );
  55. #endif TCP_DAYTIME
  56.  
  57.     while ( 1 ) {
  58.     sock_tick( s, &status );
  59.  
  60.  
  61.     if (sock_dataready( s ) ) {
  62.         sock_gets( s, buffer, sizeof( buffer ));
  63.         puts( buffer );
  64.         sock_close( s );
  65.         break;
  66.     }
  67.     }
  68.  
  69. sock_err:
  70.     switch (status) {
  71.     case 1 : /* foreign host closed */
  72.          return(0);
  73.     case -1: /* timeout */
  74.          printf("\nConnection timed out!");
  75.          return(1);
  76.     default: printf("Aborting");
  77.          return(1);
  78.     }
  79. }
  80.  
  81.  
  82. main(int argc, char **argv )
  83. {
  84.     int status;
  85.     longword host;
  86.  
  87.     if (argc != 2) {
  88.     puts("   DAYTIME server");
  89.     exit( 3 );
  90.     }
  91.  
  92.     sock_init();
  93.  
  94.     if ( host = resolve( argv[1]))
  95.     status = daytime( host );
  96.     else {
  97.     printf("Could not resolve host '%s'\n", argv[1]);
  98.     status = 3;
  99.     }
  100.  
  101.     exit( status );
  102. }
  103.