home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PIPEDEME.ZIP / PSERVER.C < prev    next >
C/C++ Source or Header  |  1990-05-30  |  2KB  |  100 lines

  1. /*
  2.  
  3.     PSERVER.C - server (transmit) half of named pipe demo.
  4.  
  5.     Copyright (C) 1990, by John D. Ruley @ Zaphodyne Inc.
  6.  
  7.     Unlimited right is herewith granted to use, modify and otherwise
  8.     exploit this software at will, however neither the author, nor
  9.     Zaphodyne Inc. shall bear ANY responsibility for the results!
  10.  
  11.     Compile with MSC 5.1 using MKPDEMO.CMD
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. #define INCL_BASE
  19. #include <os2.h>
  20.  
  21. static unsigned char far *buffer;
  22.  
  23. main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27. int i;
  28. HPIPE hp;
  29. USHORT n;
  30.  
  31. if(argc < 2)
  32.     {
  33.     printf("useage pserver \\pipe\\PIPE_NAME");
  34.     exit(0);
  35.     }
  36.  
  37. /* make a byte-based pipe for output: */
  38.  
  39. if(( n = DosMakeNmPipe(argv[1],
  40.         &hp,
  41.         PIPE_ACCESS_OUTBOUND | PIPE_NOINHERIT | PIPE_NOWRITEBEHIND,
  42.         PIPE_WAIT | PIPE_READMODE_BYTE | PIPE_TYPE_BYTE | 1,
  43.         64000,
  44.         0,
  45.         1000L
  46.         )))
  47.     {
  48.     printf("ERROR %d - unable to make pipe %s\n",n,argv[1]);
  49.     exit(1);
  50.     }
  51.  
  52. /* make a buffer for data: */
  53.  
  54. buffer = (unsigned char far *)malloc(64000);
  55. if(!buffer)
  56.     {
  57.     printf("ERROR - unable to allocate buffer space!\n");
  58.     exit(3);
  59.     }
  60.  
  61. /* connect to the client: */
  62.  
  63. printf("Waiting for connection from client...");
  64.  
  65. if((n = DosConnectNmPipe(hp)))
  66.     {
  67.     printf("ERROR %d - bad connection!\n",n);
  68.     }
  69.  
  70. else{
  71.     printf("CONNECTED!\n");
  72.  
  73.     while(1)
  74.         {
  75.  
  76.     /* loop until user presses ESCape: */
  77.  
  78.         if(kbhit())
  79.             {
  80.             i = getch();
  81.             if(i == 27)break;
  82.             }
  83.  
  84.     /* write 64K to the client: */
  85.  
  86.         if(DosWrite(hp, buffer, 64000, &n))break;
  87.         printf(".");
  88.         }
  89.  
  90.     /* done: disconnect from the client: */
  91.  
  92.     DosDisConnectNmPipe(hp);
  93.     }
  94.  
  95. /* and clean up the mess: */
  96.  
  97. free(buffer);
  98. printf("done!\n");
  99. }
  100.