home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / COOKIE.C < prev    next >
C/C++ Source or Header  |  1991-07-17  |  2KB  |  123 lines

  1. /******************************************************************************
  2.     COOKIE - read and print a witty saying from internet
  3.  
  4.     By: Jim Martin                      Internet: jim@dorm.rutgers.edu
  5.         Dormitory Networking Project    UUCP: {backbone}!rutgers!jim
  6.         Rutgers University              Phone: (908) 932-3719
  7.  
  8.     Uses the Waterloo TCP kernal.
  9. ******************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <tcp.h>
  13.  
  14.  
  15. #define COOKIE_PORT 17
  16.  
  17. cookie(host)
  18. longword host;
  19. {
  20.     tcp_Socket telsock;
  21.         static tcp_Socket *s;
  22.         char buffer[ 2049 ];
  23.         int status;
  24.         int len;
  25.     int i;
  26.     int fortunelen;
  27.  
  28.         s = &telsock;
  29.         status = 0;
  30.  
  31.     if ( host != 0 )
  32.     {
  33.         status=udp_open( s, 0, host, COOKIE_PORT, NULL );
  34.     }
  35.     else
  36.     {
  37.         if (_last_cookie == 0)
  38.         {
  39.             puts("Sorry, I can't seem to remember where my cookie jars are. ");
  40.             puts("Could you tell me where one is? (Hint: host on the comand line)");
  41.             exit(3);
  42.         }
  43.         for (i=0; i < _last_cookie; i++)
  44.         {
  45.             if (status=udp_open( s, 0, _cookie[i], COOKIE_PORT, NULL ))
  46.             {
  47.                 break;
  48.             }
  49.         }
  50.     }
  51.  
  52.     if ( status == 0 )
  53.     {
  54.         puts("None of the cookie jars are open!");
  55.         return( 1 );
  56.     }
  57.  
  58.     sock_write( s, "\n", 1 );
  59.  
  60.  
  61.         while ( 1 )
  62.     {
  63.         sock_tick( s, &status );
  64.  
  65.         if (sock_dataready( s ) )
  66.         {
  67.             fortunelen=sock_fastread( s, buffer, sizeof( buffer ));
  68.             buffer[fortunelen]='\0';
  69.             printf("%s\n", buffer);
  70.             sock_close(s);
  71.             return(0);
  72.         }
  73.         }
  74.  
  75. sock_err:
  76.     switch (status)
  77.     {
  78.         case 1 : /* foreign host closed */
  79.              return(0);
  80.         case -1: /* timeout */
  81.              printf("\nConnection timed out!");
  82.              printf("ERROR: %s\n\r", sockerr( s ));
  83.              return(1);
  84.         }
  85.  
  86. }
  87.  
  88.  
  89. main(int argc, char **argv )
  90. {
  91.     int status;
  92.     longword host;
  93.  
  94.     if (argc > 2)
  95.     {
  96.         puts("Quote of the Day (Cookie) - retrieves a witty message");
  97.         puts("Usage: COOKIE [server]");
  98.         exit( 3 );
  99.     }
  100.  
  101.     sock_init();
  102.  
  103.     if ( argc == 1)
  104.     {
  105.         status = cookie ((longword) NULL);
  106.     }
  107.     else
  108.     {
  109.         if ( host = resolve( argv[1]))
  110.         {
  111.             status = cookie( host );
  112.         }
  113.         else
  114.         {
  115.             printf("Could not resolve host '%s'\n", argv[1]);
  116.             status = 3;
  117.         }
  118.     }
  119.  
  120.     exit( status );
  121. }
  122.  
  123.