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

  1. /* ************************************************************************ */
  2. /* TLICLIPX.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 server and received by the client.
  6.  
  7.    This example is connectionless, or IPX.  Once the client receives the packet,
  8.    it issues another receive.  This example will work in blocking or
  9.    non-blocking mode (the t_open will need to have O_NDELAY or'ed with
  10.    O_RDWR for non-blocking mode.
  11.                                         */
  12. /* ************************************************************************ */
  13.  
  14. #define     INCL_BASE
  15. #define     INCL_DOS
  16. #define        INCL_VIO
  17. #include <os2.h>
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <fcntl.h>
  23. #include <malloc.h>
  24. #include <string.h>
  25. #include <conio.h>
  26.  
  27. #define NWOS2
  28. #define IS32BIT
  29. #define BCPP
  30.  
  31. #include <nwcaldef.h>
  32. #include <tispxipx.h>
  33. extern "C"{
  34.   #include <tiuser.h>
  35. }
  36.  
  37. #define         IPX_SOCKET              0x4645   /* arbitrary socket */
  38. #define        DATABUFSIZE        100       /*arbitrary buffer size */
  39.  
  40.  
  41. void main ( void )
  42. {
  43.     IPX_ADDR ipxReqAddr;
  44.     IPX_ADDR ipxRetAddr;
  45.     IPX_ADDR ipxAddr;
  46.     IPX_OPTS ipxOptions;
  47.     struct t_bind tbindReq;
  48.     struct t_bind tbindRet;
  49.     int fd;
  50.     char dataBuf[DATABUFSIZE];
  51.     char dataBuf2[DATABUFSIZE*20];
  52.     struct t_unitdata unitdata;
  53.     int flags=0;
  54.     int dataPos;
  55.  
  56.  
  57.  
  58.                    /* Open in blocking mode, replace O_RDWR */
  59.                    /* with O_RDWR|O_NDELAY for non-blocking mode */
  60.     if (( fd = t_open( "/dev/nipx", O_RDWR, ( struct t_info * )0) ) == -1 )
  61.     {
  62.         t_error( "open of /dev/nipx failed" );
  63.         exit( 2 );
  64.     }
  65.                 /* set up the socket that we will be listening on */
  66.  
  67.     ipxReqAddr.ipxa_socket[ 0 ] = (IPX_SOCKET & 0xFF00)>>8;
  68.     ipxReqAddr.ipxa_socket[ 1 ] = IPX_SOCKET & 0x00FF;
  69.     tbindReq.addr.buf = ( char * )&ipxReqAddr;
  70.     tbindReq.addr.len = sizeof( IPX_ADDR );
  71.     tbindReq.addr.maxlen = sizeof( IPX_ADDR ); /*Can be any value for Req*/
  72.  
  73.     tbindRet.addr.buf = ( char * )&ipxRetAddr;
  74.     tbindRet.addr.len = sizeof( IPX_ADDR );
  75.     tbindRet.addr.maxlen = sizeof(IPX_ADDR);
  76.  
  77.  
  78.     if ( t_bind( fd, &tbindReq, &tbindRet )== -1 )
  79.     {
  80.         t_error( "bind failed" );
  81.         exit( 2 );
  82.     }
  83.  
  84.                 /* set up the unitdata structure for the receive */
  85.  
  86.     ipxAddr.ipxa_socket[ 0 ] = (IPX_SOCKET & 0xFF00)>>8;
  87.     ipxAddr.ipxa_socket[ 1 ] = IPX_SOCKET & 0x00FF;
  88.  
  89.     unitdata.addr.buf = ( char * )&ipxAddr;
  90.     unitdata.addr.len = sizeof( ipxAddr );
  91.     unitdata.addr.maxlen = sizeof( ipxAddr );
  92.  
  93.     unitdata.opt.buf=(char *)&ipxOptions;  /* Options structure is defined*/
  94.     unitdata.opt.len = sizeof(IPX_OPTS);   /* in tispxipx.h*/
  95.     unitdata.opt.maxlen = sizeof(IPX_OPTS);
  96.     unitdata.udata.buf = (char *)&dataBuf; /* this is where the data will */
  97.     unitdata.udata.len = sizeof(dataBuf);  /* be returned */
  98.     unitdata.udata.maxlen = DATABUFSIZE;
  99.  
  100.  
  101.  
  102.                /* kbhit is of limited use here.  Since t_rcvudata is
  103.               executing in blocking mode, while( !kbhit() ) only
  104.               gets evaluated after a packet has been received */
  105.  
  106.     while(!kbhit())
  107.     {
  108.       dataPos=0;
  109.       printf("Listening... \n");
  110.       if ((t_rcvudata(fd, &unitdata, (short NWFAR *)&flags)) == -1)
  111.        {
  112.               /* this is more for non-blocking mode, this code should
  113.              never execute in blocking mode */
  114.          if(t_errno != TNODATA)
  115.           {
  116.         t_error("Error Receiving data");
  117.         exit(1);
  118.           }
  119.        }
  120.       else       /* receive succeeded */
  121.       {
  122.  
  123.         memcpy(&dataBuf2[dataPos],unitdata.udata.buf,DATABUFSIZE);
  124.  
  125.         /* This is just so we can print the data to check it. */
  126.         dataBuf2[dataPos+unitdata.udata.len+1]='\0';
  127.  
  128.         /*  There is more data to process for this packet */
  129.         while (flags==T_MORE)
  130.         {
  131.           flags=0;
  132.           printf(".");
  133.           dataPos +=DATABUFSIZE;
  134.           t_rcvudata(fd, &unitdata, (short NWFAR *)&flags);
  135.           memcpy(&dataBuf2[dataPos], unitdata.udata.buf,DATABUFSIZE);
  136.  
  137.         /* This is just so we can print the data to check it    */
  138.           dataBuf2[dataPos+unitdata.udata.len+1]='\0';
  139.  
  140.         }
  141.  
  142.         printf("Data Received \nSize %d\n- %s\n",(strlen(dataBuf2)-1),dataBuf2);
  143.     }
  144.        }
  145. }
  146.