home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12408.ZIP / CLIENT.C next >
C/C++ Source or Header  |  1989-09-12  |  3KB  |  140 lines

  1. /*  Compilation and  Linking
  2.  
  3.     cl -Alfw -Zl -Gs -c client.c
  4.     link client,,,netapi nampipes
  5.  
  6.     Usage:
  7.        server2
  8.     Comments:
  9.        This program shows the usage of Name Pipes, Semaphores, and
  10.        Threads. Specifically:
  11.  
  12.       DosOpen
  13.       DosSetNmpHandState
  14.        1. The pipe is in non-blocked mode.
  15.        2. Hit <return> to terminate.
  16.     DisClaimer:
  17.        THIS PROGRAM IS FOR DEMONSTRATION ONLY. MICROSOFT MAKES NO WARRANTY
  18.        , EITHER EXPRESSED OR IMPLIED, AS TO IT'S USABILITY IN ANY GIVEN
  19.        SITUATION.
  20. */
  21.  
  22. #define INCL_BASE
  23. #include <os2.h>
  24.  
  25. #include <netcons.h>
  26. #include <nmpipe.h>
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30.  
  31.     /* Client DosOpen Parameters    */
  32. #define DUMMY_FILE_SIZE 0L
  33. #define NORMAL 0x00
  34. #define RESERVED 0L
  35. #define OPEN_FAIL 0x00
  36. #define OPEN_EXISTS 0x01
  37.  
  38.  
  39.     /* PIPE PARAMETERS */
  40. #define INBUFSZ 4096
  41. #define OUTBUFSZ 4096
  42. #define SIZESEND  2040
  43. #define TIMEOUT 0L
  44. #define OPEN_ACCESS_READWRITE 0x0002
  45. #define OPEN_SHARE_DENYNONE   0x0040
  46.  
  47. #define DEFAULTNAME "\\pipe\\demo"
  48.  
  49.  
  50.  
  51. main (int argc, char * argv[])
  52. {
  53.     unsigned short oWrt, oRd;
  54.     unsigned rtn;
  55.     char *pname;
  56.     USHORT pipeHdl, status;
  57.     char *buf, *msg;
  58.     int i, j, msglen;
  59.     int n = 0;            /* Message Count */
  60.  
  61.     if (argc >1)
  62.     pname = argv[1];
  63.     else
  64.     pname = DEFAULTNAME;
  65.  
  66.     msg = (char *)malloc(INBUFSZ);
  67.     buf = (char *)malloc(OUTBUFSZ);
  68.  
  69.  
  70.     printf ("***   CLIENT   ***\n\n");
  71.  
  72.     /* Open the Pipe at the Client end */
  73.  
  74.  
  75.     if (rtn=DosOpen (pname, &pipeHdl, &status, DUMMY_FILE_SIZE, NORMAL,
  76.              OPEN_EXISTS,
  77.              OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE,
  78.              RESERVED)){
  79.  
  80.         printf ("DosOpen error #%u status%u\n", rtn, status);
  81.         exit(1);
  82.  
  83.     }
  84.  
  85.         /* Change to Non_blocking  */
  86.      rtn = DosSetNmpHandState(pipeHdl, 0x8100);
  87.      if (rtn)
  88.         printf("DosSetNmpHandState Error rtn: %i\n", rtn);
  89.  
  90.       buf[0] = '\0';
  91.       printf("Client >>");
  92.       msglen = strlen(gets(buf));
  93.       if (msglen ==0) {
  94.      if (rtn=DosClose (pipeHdl))
  95.         printf ("DosClose error #%u\n", rtn);
  96.      exit(1);
  97.        }
  98.       buf[msglen] = '\0';
  99.  
  100.  
  101.     do {
  102.  
  103.     n++;
  104.         /* write   message */
  105.     if (rtn = DosWrite (pipeHdl, buf, strlen(buf), &oRd))
  106.         printf ("Client: DosWrite error #%u\n", rtn);
  107.     if (oRd != msglen)
  108.         printf ("Client: %i  write error: %i!!!\n", msglen-oRd, n);
  109.  
  110.         /* read message */
  111.     if (rtn = DosRead (pipeHdl, msg, INBUFSZ-1, &oRd))
  112.         printf( "DosRead error #%u\n", rtn);
  113.  
  114.     if (!rtn) {
  115.         printf("Message from Server: ");
  116.         for (j=0; j < oRd; )
  117.              printf("%c",  msg[j++]);
  118.         printf("\n");
  119.  
  120.     }
  121.     msg[0] = '\0';
  122.     buf[0] = '\0';
  123.     printf("Client >>");
  124.     msglen = strlen(gets(buf));
  125.     printf("\n");
  126.  
  127.     if (msglen ==0) {
  128.         if (rtn=DosClose (pipeHdl))
  129.         printf ("DosClose error #%u\n", rtn);
  130.         exit(1);
  131.     }
  132.     buf[msglen] = '\0';
  133.  
  134.  
  135.     } while ( 1 );
  136.  
  137.     if (rtn=DosClose (pipeHdl))
  138.     printf ("DosClose error #%u\n", rtn);
  139. }
  140.