home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / tliex2.exe / DSERVER.C < prev    next >
C/C++ Source or Header  |  1994-11-10  |  5KB  |  200 lines

  1. /* ************************************************************************ */
  2. /* DSERVER.C
  3.  
  4.    This example is the client side of a simple TLI example.  In this example,
  5.    a simple message string is sent by the client, received by the server,
  6.    and sent back to the client.  The client compares the information sent
  7.    back, and then terminates.
  8.  
  9.    This example can be found in  the NetWare Programmers Guide for C, under
  10.    the TLI Services section.
  11. */
  12. /* ************************************************************************ */
  13.  
  14. #include <os2.h>
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <fcntl.h>
  20.  
  21. #define TLIINT  short
  22. #define TLIUINT unsigned short
  23.  
  24. #define NWOS2
  25. #define IS32BIT   /* NW library define for 32 bit OS/2 apps. */
  26. #define BCPP      /* Defines use of Borland C++ compiler */
  27.  
  28. #include <nwcaldef.h>
  29. #include <tispxipx.h>
  30. #include <tiuser.h>
  31.  
  32. #define SPX_SOCKET 4646                   /* arbitrary socket */
  33.  
  34. void SPXDisconReason( TLIINT fd );
  35.  
  36. int flags;
  37. struct t_bind tbind;
  38. struct t_call tcall;
  39. char NWPTR xportEP;
  40. struct t_info NWPTR infoEP;
  41.  
  42. void main ( )
  43. {
  44.     IPX_ADDR spx_addr;
  45.     SPX_OPTS spx_options;
  46.     struct t_discon discon;
  47.     char buf[ 100 ];
  48.     TLIINT fd, nfd, len, c, listenResult;
  49.  
  50.     /* Step 1 */
  51.     xportEP = "/dev/nspx";
  52.     infoEP = NULL;
  53.     if ( ( fd = t_open( xportEP, O_RDWR | O_NDELAY, infoEP ) ) == -1 )
  54.     {
  55.         t_error( "open of /dev/nspx failed" );
  56.         exit( 1 );
  57.     }
  58.  
  59.     /* Step 2 */
  60.     spx_addr.ipxa_socket[ 0 ] = 0;
  61.     spx_addr.ipxa_socket[ 1 ] = SPX_SOCKET;
  62.     tbind.addr.len = sizeof( spx_addr );
  63.     tbind.addr.maxlen = sizeof( spx_addr );
  64.     tbind.addr.buf = ( char * ) &spx_addr;
  65.     tbind.qlen = 1;
  66.  
  67.     if ( t_bind( fd, &tbind, &tbind ) == -1 )
  68.     {
  69.         t_error( "bind failed" );
  70.         exit( 1 );
  71.     }
  72.  
  73.     /* Step 3 */
  74.     tcall.addr.buf = ( char * ) &spx_addr;
  75.     tcall.addr.maxlen = sizeof( spx_addr );
  76.     tcall.addr.len = sizeof( spx_addr );
  77.     spx_options.spx_connectionID[ 0 ] = 0;
  78.     spx_options.spx_connectionID[ 1 ] = 0;
  79.     spx_options.spx_allocationNumber[ 0 ] = 0;
  80.     spx_options.spx_allocationNumber[ 1 ] = 0;
  81.     tcall.opt.buf = ( char * )&spx_options;
  82.     tcall.opt.len = sizeof( spx_options );
  83.     tcall.opt.maxlen = sizeof( spx_options );
  84.     tcall.udata.buf = ( char * )0;
  85.     tcall.udata.maxlen = 0;
  86.     tcall.udata.len = 0;
  87.  
  88.     /* Step 4 */
  89.     do
  90.     {
  91.         do
  92.         {
  93.         if ( kbhit( ) )
  94.         {
  95.             getch( ); /* clean up keyboard buffer */
  96.             printf( "DSERVER terminated\n" );
  97.             t_close( fd );
  98.             exit( 0 );
  99.         }
  100.         }
  101.         while ( ( listenResult = t_listen( fd, &tcall ) ) == -1 && t_errno == TNODATA );
  102.  
  103.         /* Step 5 */
  104.         if ( listenResult != -1 )
  105.         {
  106.         if ((nfd = t_open( "/dev/nspx",O_RDWR,(struct t_info * )0))==-1)
  107.         {
  108.             t_error( "t_open after listen failed" );
  109.             t_close( fd );
  110.             exit( 1 );
  111.         }
  112.         if (t_bind( nfd,(struct t_bind * )0,(struct t_bind * )0) == -1 )
  113.         {
  114.             t_error( "t_bind failed" );
  115.             t_close( nfd );
  116.             t_close( fd );
  117.             exit( 1 );
  118.         }
  119.         if ( t_accept( fd, nfd, &tcall ) == -1 )
  120.         {
  121.             t_error( "t_accept failed" );
  122.             t_close( nfd );
  123.             t_close( fd );
  124.             exit( 1 );
  125.         }
  126.         printf( "Connection accepted.\n" );
  127.         flags = 0;                      /* initialize for t_rcv call */
  128.  
  129.         /* Step 6 */
  130.         while ( ( len = t_rcv( nfd, buf, sizeof( buf), &flags ) ) != -1)
  131.         {
  132.             printf( "Received message '%s'\n", buf );
  133.             if ( t_snd( nfd, buf, len, 0 ) == -1 )
  134.             {
  135.                 t_error( "t_snd failed" );
  136.                 t_close( nfd );
  137.                 t_close( fd );
  138.                 exit( 1 );
  139.             }
  140.             printf( "Returned message '%s'\n", buf );
  141.         }
  142.  
  143.             /* Step 7 */
  144.         printf("t_errno = %d, t_look() = %d\n",t_errno , t_look( nfd ));
  145.         if ( t_errno == TLOOK  &&  t_look( nfd ) == T_DISCONNECT )
  146.             SPXDisconReason( nfd );
  147.         else            /* some other error so disconnect */
  148.         {
  149.             if ( t_snddis( nfd, ( struct t_call * )0 ) == -1 )
  150.                 t_error( "t_snddis failed " );
  151.         }
  152.         t_close( nfd );
  153.         printf( "listening...\n" );
  154.         } /* End of if (listenResult != -1 ) */
  155.     }
  156.     while ( listenResult != -1 );   /* End of outer do-while loop */
  157.  
  158.     /* Step 8 */
  159.     if ( t_errno == TLOOK && t_look( fd ) == T_DISCONNECT )
  160.         SPXDisconReason( fd );
  161.     else
  162.         t_error( "t_listen failed" );
  163.     t_close( fd );
  164. } /* main */
  165.  
  166. void SPXDisconReason( TLIINT fd )
  167. {
  168.     struct  t_discon discon;
  169.     char buf[ 100 ];
  170.     char *msg;
  171.  
  172.     discon.udata.buf = buf;
  173.     discon.udata.len = 0;
  174.     discon.udata.maxlen = sizeof(buf);
  175.  
  176.     printf( "SPX Connection terminated\n" );
  177.     if ( t_rcvdis( fd, &discon ) == -1 )
  178.     {
  179.         t_error( "t_rcvdis failed" );
  180.         exit( 2 );
  181.     }
  182.     printf( "Reason = %d\n" , discon.reason );
  183.     switch( discon.reason )
  184.     {
  185.     case TLI_SPX_CONNECTION_FAILED:
  186.         msg = "Connection failed";
  187.         break;
  188.     case TLI_SPX_CONNECTION_TERMINATED:
  189.         msg = "Connection terminated by client";
  190.         break;
  191.     case TLI_SPX_MALFORMED_PACKET:
  192.         msg = "Internal SPX TLIINTerface error -- malformed packet";
  193.         break;
  194.     default:
  195.         msg = "Unknown termination reason";
  196.     }
  197.     printf( "SPX Connection terminated: %s\n", msg );
  198. } /* SPXDisconReason */
  199.  
  200.