home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / ncsa_tel / tel_2_2_ / source / minitel.c < prev    next >
C/C++ Source or Header  |  1988-07-15  |  2KB  |  125 lines

  1. /*
  2. *   minitel
  3. *   Example TCP/IP program for the NCSA TCP/IP kernel
  4. */
  5. #include "stdio.h"
  6. #include "whatami.h"
  7. #include "hostform.h"
  8.  
  9. struct machinfo *mp;
  10. char *neterrstring(),buf[256];
  11.  
  12. main(argc,argv)
  13.     int argc;
  14.     char *argv[];
  15.     {
  16.     int i,cnt,ev,pnum,what,dat;
  17.     char *errmsg;
  18.     char c;
  19.  
  20.     puts("National Center for Supercomputing Applications");
  21.     puts("Mini-telnet example program");
  22.     puts("July 1987\n");
  23.  
  24.     if (argc < 2)
  25.         exit(1);
  26.  
  27.     if (Snetinit()) {            /* call session initialization */
  28.         errhandle();            /* Snetinit() reads config.tel file */
  29.         exit(1);
  30.     }
  31.  
  32.     mp = Sgethost(argv[1]);        /* look up in hosts cache */
  33.     if (!mp)
  34.         Sdomain(argv[1]);        /* not in hosts, try domain */
  35.     else {
  36.         if (0 > (pnum = Snetopen(mp,23))) {
  37.             errhandle();
  38.             netshut();
  39.             exit(1);
  40.         }
  41.     }
  42.  
  43.     c = 0;
  44.  
  45.     do {
  46.         if (kbhit()) {                /* has key been pressed */
  47.             c = getch();
  48.             netwrite(pnum,&c,1);    /* user input sent on connection */
  49.         }
  50.  
  51. /*
  52. *  get event from network, these two classes return all of the events
  53. *  of user interest.
  54. */
  55.         ev = Sgetevent(USERCLASS | CONCLASS | ERRCLASS,&what,&dat);
  56.         if (!ev)
  57.             continue;
  58.  
  59.         if (what == ERRCLASS) {                /* error event */
  60.             errmsg = neterrstring(dat);
  61.             puts(errmsg);
  62.         }
  63.         else if (what == CONCLASS) {        /* event of user interest */
  64.             switch (ev) {
  65.                 case CONOPEN:                /* connection opened or closed */
  66.                     netpush(dat);            /* negotiation */
  67.                     netwrite(dat,"\377\375\001\377\375\003\377\374\030",9);
  68.                     break;
  69.                 default:
  70.                     break;
  71.                 case CONDATA:                /* data arrived for me */
  72.                     cnt = netread(dat,buf,80);
  73.                     for (i=0; i<cnt; i++) 
  74.                         if (buf[i] < 128)
  75.                             putchar(buf[i]);
  76.                     break;
  77.                 case CONFAIL:
  78.                     puts("Connection attempt failed");
  79.                     /* drop through to exit */
  80.  
  81.                 case CONCLOSE:
  82.                     netshut();
  83.                     exit(1);
  84.             }
  85.         }
  86.         else if (what == USERCLASS) {
  87.             switch (ev) {
  88.                 case DOMOK:                            /* domain worked */
  89.                     mp = Slooknum(dat);                /* get machine info */
  90.                     pnum = Snetopen(mp,23);            /* open to host name */
  91.                     break;
  92.                 case DOMFAIL:    /* domain failed */
  93.                     n_puts("domain failed");
  94.                     netshut();
  95.                     exit(1);
  96.                 default:
  97.                     break;
  98.             }
  99.         }
  100.  
  101.     } while (c != 16);            /* Ctrl-P, arbitrary escape */
  102.  
  103.     netclose(pnum);
  104.     netshut();
  105.     exit(0);
  106.  
  107. }
  108.  
  109. /*********************************************************************/
  110. /*  errhandle
  111. *   write error messages to the console window
  112. */
  113. errhandle()
  114.     {
  115.     char *errmsg;
  116.     int i,j;
  117.  
  118.     while (ERR1 == Sgetevent(ERRCLASS,&i,&j)) {
  119.         errmsg = neterrstring(j);
  120.         puts(errmsg);
  121.     }
  122.  
  123. }
  124.  
  125.