home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / info / redbook2 / pipeos2.c < prev    next >
Text File  |  1992-06-28  |  6KB  |  162 lines

  1. /**********************************************************/
  2. /**********************************************************/
  3. /***                                                    ***/
  4. /***  Program name: PIPEOS2.C                           ***/
  5. /***                                                    ***/
  6. /***  Created     : 16th May 1990                       ***/
  7. /***                                                    ***/
  8. /***  Revised     : 26th February 1992                  ***/
  9. /***                                                    ***/
  10. /***  Author      : Tim Sennitt, Dorle Hecker           ***/
  11. /***                                                    ***/
  12. /***  Purpose     : To demonstrate the use of an OS/2   ***/
  13. /***                created named pipe connecting to    ***/
  14. /***                many DOS sessions                   ***/
  15. /***                                                    ***/
  16. /***  Compile     : icc /O+ pipeos2.c                   ***/
  17. /***  or          : cl386 pipeos2.c                     ***/
  18. /***                                                    ***/
  19. /***  Input param : A number between 1 and 255          ***/
  20. /***                (number of pipe instances)          ***/
  21. /***                                                    ***/
  22. /**********************************************************/
  23.  
  24. /**********************************************************/
  25. /***  DEFINES                                           ***/
  26. /**********************************************************/
  27. #define INCL_DOS
  28. #define INCL_DOSNMPIPES
  29. /**********************************************************/
  30. /***  INCLUDES and VARIABLES                            ***/
  31. /**********************************************************/
  32. #include <os2.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #ifdef __IBMC__
  37.    void     _System NewThread(ULONG threadArg);
  38. #else
  39.    void     NewThread(ULONG threadArg);
  40. #endif
  41.    TID      threadID[512];
  42.    HPIPE    piphand[255];
  43.    ULONG    threadArg, threadFlags, stack_size;
  44.    ULONG    outbuffer, inbuffer, timeout, BytesWrit;
  45.    USHORT   rc, loopsize, i;
  46.    char     prep_string[11];
  47. /**********************************************************/
  48. /***  MAIN PROGRAM                                      ***/
  49. /**********************************************************/
  50. main(argc, argv, envp)
  51. int argc;
  52. char *argv[];
  53. char *envp[];
  54. {
  55.    BOOL fEnd_Correct=FALSE;
  56.    threadFlags = 0;          /* start thread immediatly    */
  57.    stack_size  = 1024;       /* give stack size in bytes   */
  58.    threadArg   = 1;
  59.  
  60.    if ( argc != 2 || (loopsize = atoi(argv[1])) == 0 )
  61.      { printf("You have not specified the correct bacon size !\n");
  62.        printf("The syntax is PIPEOS2 NNNN (where NNNN is a number between 1 and 255)\n");
  63.        exit(0);
  64.       } /*end-if*/
  65.    for (i = 1; i < loopsize+1; i++)
  66.      {
  67.        rc = DosCreateThread(&threadID[i], NewThread, i,
  68.                             threadFlags, stack_size);
  69.        if (rc != 0)
  70.          { printf("DosCreateThread error = %d\n",rc);
  71.           exit (1);
  72.          } /*end-if*/
  73.        printf("Pipe-Thread number %d created\n",i);
  74.        printf("Please start the DOS client\n");
  75.      } /*end-for*/
  76.  
  77.    printf("Now lets send some data to it......\n");
  78.  
  79.    /****************************************************************/
  80.    /* At this point we will loop getting keyboard input            */
  81.    /* and writing this to our named pipe (until we enter null)     */
  82.    /****************************************************************/
  83.    printf("ENTER\n [B]lue, [C]yan, [G]reen, [P]urple, \
  84. [R]ed, [W]hite, [Y]ellow or [Q]uit\n");
  85.    gets(prep_string);
  86.    while (prep_string[0] != 0)
  87.      {
  88.       if (prep_string[0] == 'q' || prep_string[0] == 'Q')
  89.         { for (i = 1; i < loopsize+1; i++)
  90.              { rc = DosWrite(piphand[i],
  91.                              (PVOID)prep_string,
  92.                              strlen(prep_string),
  93.                              &BytesWrit);
  94.                if (rc !=0) printf("Return code from DosWrite         = %d\n",rc);
  95.              } /* end-for */
  96.           prep_string[0]=0; fEnd_Correct=TRUE;
  97.         }
  98.       else
  99.         { for (i = 1; i < loopsize+1; i++)
  100.              {
  101.                rc = DosWrite(piphand[i],
  102.                              (PVOID)prep_string,
  103.                              strlen(prep_string),
  104.                              &BytesWrit);
  105.                if (rc !=0) printf("Return code from DosWrite         = %d\n",rc);
  106.               } /* end-for */
  107.           printf("ENTER\n [B]lue, [C]yan, [G]reen, [P]urple, \
  108. [R]ed, [W]hite, [Y]ellow or [Q]uit\n");
  109.           gets(prep_string);
  110.  
  111.         } /* endif */
  112.      } /* endwhile */
  113.  
  114.    if (!fEnd_Correct)
  115.      { prep_string[0]='q';
  116.        rc = DosWrite(piphand[i],
  117.                      (PVOID)prep_string,
  118.                      strlen(prep_string),
  119.                      &BytesWrit);
  120.        if (rc !=0) printf("Return code from DosWrite         = %d\n",rc);
  121.      }
  122.  exit(0);
  123. }
  124.  
  125. /****************************************************************/
  126. /* This is our thread process which creates N named pipes then  */
  127. /* waits for the DOS sessions to connect to them.               */
  128. /****************************************************************/
  129. void NewThread(ULONG threadArg)
  130. {
  131.  
  132. outbuffer = 25;
  133. inbuffer  = 25;
  134. timeout   = 200;
  135.  
  136.   rc = DosCreateNPipe("\\PIPE\\TIMSP\0",      /* create pipe    */
  137.                       &piphand[threadArg],
  138.                       0x4081,
  139.                       0x0008,
  140.                       outbuffer,
  141.                       inbuffer,
  142.                       timeout);
  143.   if (rc != 0)
  144.     { DosBeep(300,200); DosBeep(100,200);
  145.       exit(1);
  146.     }
  147.   DosBeep(300,200); DosBeep(600,200);
  148.  
  149. /****************************************************************/
  150. /* now we wait for our DOS session to connect to us             */
  151. /****************************************************************/
  152.  
  153.   rc = DosConnectNPipe(piphand[threadArg]);
  154.   if (rc != 0)
  155.     { DosBeep(100,200);
  156.       exit(1);
  157.     }
  158.   DosBeep(600,200);
  159.   printf("DOS Session number %d connected\n",threadArg);
  160. }
  161.  
  162.