home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IPCX.ZIP / NPSERVER.C < prev    next >
C/C++ Source or Header  |  1989-09-20  |  3KB  |  102 lines

  1. /* npserver.c RHS 5/1/89
  2.  *
  3.  This program demonstrates the use of named pipes by creating a server
  4.  process which will create more than instance of a named pipe. To see how
  5.  this works, run this program with:
  6.  
  7.  NPSERVER
  8.  
  9.  Then run multiple instances of the NPCLIENT program in other sessions.
  10.  
  11.  You can also run the NPRMODE program in the compatibility box to talk to
  12.  the server.
  13.  
  14.  */
  15. #define INCL_DOS
  16.  
  17. #include<os2.h>
  18. #include<mt\string.h>
  19. #include<mt\stdio.h>
  20. #include<mt\process.h>
  21. #include"nmpipe.h"
  22.  
  23. #define    MAXSERVERS            5
  24. #define    THREADSTACKSIZE    1200
  25.  
  26. unsigned long semhandle = 0L;
  27. typedef struct _servers
  28.     {
  29.     char    stack[THREADSTACKSIZE];
  30.     int    threadID;
  31.     } SERVER;
  32.  
  33. SERVER servers[MAXSERVERS];
  34. USHORT pipeopenmode = PIPE_ACCESS_DUPLEX;
  35. USHORT pipemode = (PIPE_WAIT | PIPE_READMODE_BYTE | PIPE_TYPE_BYTE |    MAXSERVERS);
  36.  
  37. void main(void);
  38. void error_exit(USHORT err, char *msg);
  39. void server_thread(int servernum);
  40.  
  41. void main(void)
  42.     {
  43.     int i;
  44.  
  45.     DosSemSet(&semhandle);
  46.  
  47.     for( i = 0; i < MAXSERVERS; i++)                    /* create server threads    */
  48.         {
  49.         if(servers[i].threadID = _beginthread(server_thread,servers[i].stack,
  50.                 THREADSTACKSIZE,(void *)i)    == -1)
  51.             error_exit(-1,"_beginthread");
  52.         DosSleep(100L);
  53.         }
  54.  
  55.     DosSemRequest(&semhandle,SEM_INDEFINITE_WAIT);/* wait for semaphore        */
  56.     DosExit(EXIT_PROCESS,0);                            /* kill the process            */
  57.     }    
  58.  
  59. void server_thread(int servernum)
  60.     {
  61.     char readbuf[PIPESIZE];
  62.     HPIPE pipehandle;
  63.     USHORT retval;
  64.     USHORT bytesread;
  65.  
  66.     printf("Server instance %d creating Named pipe...\n",servernum);
  67.                                                                 /* create pipe instance        */
  68.     if(retval = DosMakeNmPipe(NAMEDPIPE,&pipehandle,pipeopenmode,pipemode,
  69.             PIPESIZE,PIPESIZE,1000L))
  70.         error_exit(retval, "DosMakeNmPipe");
  71.  
  72.     printf("Server instance %d waiting for Client to open pipe...\n",servernum);
  73.  
  74.     if(retval = DosConnectNmPipe(pipehandle))        /* wait until connected        */
  75.         error_exit(retval,"DosConnectNmPipe");
  76.  
  77.     printf("Server instance %d: Client has opened pipe, waiting for message...\n",servernum);
  78.  
  79.     while(TRUE)
  80.         {
  81.                                                                 /* read a message                */
  82.         if(retval = DosRead(pipehandle,readbuf,PIPESIZE,&bytesread))
  83.             error_exit(retval,"DosRead");
  84.  
  85.         printf("Server instance %d received: \'%s\'...acknowledging\n",
  86.                 servernum,readbuf);
  87.                                                                 /* acknowledge it                */
  88.         if(retval = DosWrite(pipehandle,ACKNOWLEDGE,strlen(ACKNOWLEDGE)+1,
  89.                 &bytesread))
  90.             error_exit(retval,"DosWrite");
  91.         DosSleep(250L);
  92.         }
  93.     DosSemClear(&semhandle);
  94.     DosClose(pipehandle);
  95.     }
  96.  
  97. void error_exit(USHORT err, char *msg)
  98.     {
  99.     printf("Error %u returned from %s\n",err,msg);
  100.     DosExit(EXIT_PROCESS,0);
  101.     }
  102.