home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / tliex2.exe / TLISVIPX.CPP < prev   
C/C++ Source or Header  |  1994-11-09  |  4KB  |  155 lines

  1. /* ************************************************************************ */
  2. /* TLISVIPX.CPP
  3.  
  4.    This example is the server side of a simple connectionless mode TLI example.
  5.    In this example, a simple message string is sent by the server, and
  6.    received by the client.  The client prints this message out and continues
  7.    listening.  The server terminates at this point.
  8.                                         */
  9. /* ************************************************************************ */
  10.  
  11. #define     INCL_BASE
  12. #define     INCL_DOS
  13. #define        INCL_VIO
  14. #include <os2.h>
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <fcntl.h>
  20. #include <malloc.h>
  21. #include <string.h>
  22.  
  23.  
  24. #define NWOS2    /* These defines are necessary for 32 bit compilation in */
  25. #define IS32BIT  /* Borland C++.  Comparable includes are required for CSET2 */
  26. #define BCPP     /* and Watcom C++ */
  27.  
  28. #include <nwcaldef.h>
  29. #include <tispxipx.h>
  30. extern "C"{
  31.   #include <tiuser.h>
  32. }
  33.  
  34. #define         IPX_SOCKET              0x4645     /* Arbitrary Socket Number */
  35. #define         IPX_BUF                 1000        /* Arbitrary Buffer Size */
  36.  
  37. int  ParseAddress( char *address, IPX_ADDR *destination );
  38. int  PutAddress( char *string, char *buf, int hexBytes );
  39.  
  40.  
  41.  
  42. /*  ParseAddress and PutAddress build an internetwork address from an
  43.     input address from the user.  This address is then inserted into a
  44.     IPX_ADDR structure for use later in the program.                      */
  45.  
  46. int ParseAddress( char *addr, IPX_ADDR *destination )
  47. {
  48.     if ( strlen( addr ) == 21 && addr[ 8 ] == '/' )
  49.         if ( PutAddress( addr, destination->ipxa_net, 4 ) )
  50.              if ( PutAddress( &addr[ 9 ], destination->ipxa_node, 6 ) )
  51.             return 1;
  52.     return 0;
  53. } /* ParseAddress */
  54.  
  55. int PutAddress( char *string, char *buf, int hexBytes )
  56. {
  57.     int   i, j, value;
  58.     char   c;
  59.  
  60.     for ( i = 0; i < hexBytes; i++ )
  61.     {
  62.         value = 0;                  /* build a byte from two nibbles */
  63.         for ( j = 0; j < 2; j++ )
  64.         {
  65.         value <<= 4;
  66.         if ( ( c = toupper( *string ) ) >= '0' && c <= '9')
  67.            value += c - '0';
  68.            else if ( c >= 'A' && c <= 'F' )
  69.               value += c - 'A' + 10;
  70.            else
  71.               return 0;
  72.            string++;
  73.        }
  74.        *buf++ = value;
  75.     }
  76.     return 1;
  77. } /* PutAddress */
  78.  
  79.  
  80.  
  81. void main ( int argc, char *argv[] )
  82. {
  83.     IPX_ADDR ipx_addr;               /* server address from command line */
  84.     SPX_OPTS spx_options;
  85.     int fd, bufSize, bufPos;
  86.     char dataBuf[IPX_BUF];
  87.     struct t_unitdata *unitdata;
  88.     BOOL moreData;
  89.  
  90.  
  91.     if ( argc != 2 || !ParseAddress( argv[ 1 ], &ipx_addr ) )
  92.     {
  93.       printf( "Usage:\tTLISVIPX          Client Address\n" );
  94.       printf( "\tClient Address = Net/Node (in hex, leading zero's required)\n" );
  95.       printf( "\tExample:\"TLISVIPX 00001234/00001b02362e\"\n" );
  96.       exit( 1 );
  97.     }
  98.                /* NULL is used for the t_info parameter since the */
  99.                /* program is not making use of that information */
  100.  
  101.     if (( fd = t_open( "/dev/nipx", O_RDWR, ( struct t_info * )0) ) == -1 )
  102.     {
  103.         t_error( "open of /dev/nipx failed" );
  104.         exit( 2 );
  105.     }
  106.                /* No need to bind to a specific socket since */
  107.                /* this application is only sending, not receiving */
  108.                /*  The send socket is configured below in the */
  109.                /* ipx_addr.ipxa_socket variable */
  110.     if ( t_bind( fd, ( struct t_bind * )0, ( struct t_bind * )0 )== -1 )
  111.     {
  112.         t_error( "bind failed" );
  113.         exit( 2 );
  114.     }
  115.  
  116.  
  117.     unitdata=(struct t_unitdata *)t_alloc(fd, T_UNITDATA, T_ALL);
  118.     if(unitdata==NULL)
  119.     {
  120.         t_error( "t_alloc failed" );
  121.         exit( 2 );
  122.     }
  123.  
  124.         /* This message will be sent out */
  125.         /* set dataBuf to whatever data you want sent out */
  126.     memset(dataBuf, 'A', IPX_BUF);
  127.  
  128.     ipx_addr.ipxa_socket[ 0 ] = (IPX_SOCKET & 0xFF00)>>8;
  129.     ipx_addr.ipxa_socket[ 1 ] = IPX_SOCKET & 0x00FF;
  130.     unitdata->addr.buf = ( char * )&ipx_addr;
  131.  
  132.     bufPos=0;
  133.     moreData=TRUE;
  134.     while(moreData){
  135.      if(unitdata->udata.maxlen < (sizeof(dataBuf)- bufPos))
  136.       {
  137.        bufSize=unitdata->udata.maxlen;
  138.        moreData=TRUE;
  139.       }
  140.      else
  141.      {
  142.        bufSize=sizeof(dataBuf)-bufPos;
  143.        moreData=FALSE;
  144.      }
  145.      memcpy(unitdata->udata.buf,&dataBuf[bufPos],bufSize);
  146.      bufPos +=bufSize;
  147.      unitdata->udata.len=bufSize;
  148.      if (t_sndudata(fd, unitdata) == -1)
  149.       {
  150.          t_error("Send Failed ");
  151.          exit(1);
  152.       }
  153.       } //end while moreData
  154. }
  155.