home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / ms / s12410 / npclient.c < prev    next >
C/C++ Source or Header  |  1989-09-20  |  2KB  |  89 lines

  1. /* npclient.c RHS 5/1/89
  2.  *
  3.  This program creates a client process which will communicate with an
  4.  instance of the named pipe created by NPSERVER. After running NPSERVER,
  5.  you can run several instances of this program with:
  6.  
  7.  NPCLIENT
  8.  
  9.  
  10.  */
  11. #define INCL_DOS
  12. #define INCL_KBD
  13. #define INCL_ERRORS
  14.  
  15. #include<os2.h>
  16. #include<string.h>
  17. #include<stdio.h>
  18. #include"nmpipe.h"
  19.  
  20. char writebuf[PIPESIZE];
  21.  
  22. void main(void);
  23. void error_exit(USHORT err, char *msg);
  24. PID Os2GetPid(void);
  25.  
  26.  
  27. void main(void)
  28.     {
  29.     HPIPE pipehandle;
  30.     USHORT retval;
  31.     USHORT bytesread;
  32.     USHORT action;
  33.     PID pid;
  34.  
  35.     pid = Os2GetPid();
  36.     printf("Client %u trying to open named pipe...\n",pid);
  37.     while(TRUE)                                                /* open the pipe                */
  38.         {
  39.         if(retval = DosOpen(NAMEDPIPE,&pipehandle,&action,0L,FILE_NORMAL,
  40.                 FILE_OPEN,OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,0L))
  41.             {
  42.             printf("%u: Error %u trying to open %s, sleeping...\n",pid,retval,
  43.                     NAMEDPIPE);
  44.             if(retval == ERROR_PIPE_BUSY)
  45.                 while(TRUE)
  46.                     {
  47.                     if(!(retval = DosWaitNmPipe(NAMEDPIPE,1000L)))
  48.                         break;
  49.                     if(retval != ERROR_SEM_TIMEOUT)
  50.                         error_exit(retval,"DosWaitNmPipe");
  51.                     }
  52.             printf("Trying again...\n");
  53.             }
  54.         else
  55.             break;
  56.         }
  57.     
  58.     printf("Client %u: server has connected pipe, sending message...\n",pid);
  59.  
  60.     while(TRUE)
  61.         {
  62.         strcpy(writebuf,"Anything I want?");        /* write a message            */
  63.         if(retval = DosWrite(pipehandle,writebuf,strlen(writebuf)+1,&bytesread))
  64.             error_exit(retval,"DosWrite");
  65.  
  66.                                                                 /* read acknowledgment        */
  67.         if(retval = DosRead(pipehandle,writebuf,PIPESIZE,&bytesread))
  68.             error_exit(retval,"DosRead");
  69.         printf("%u: Server has acknowledged: \'%s\'\n",pid,writebuf);
  70.         DosSleep(200L);
  71.         }
  72.     DosClose(pipehandle);
  73.     DosExit(EXIT_PROCESS,0);
  74.     }
  75.  
  76. PID Os2GetPid(void)                                        /* returns process id        */
  77.     {
  78.     PIDINFO pidinfo;
  79.  
  80.     DosGetPID(&pidinfo);                                    /* get process id                */
  81.     return pidinfo.pid;                                    /* return it                    */
  82.     }
  83.  
  84. void error_exit(USHORT err, char *msg)
  85.     {
  86.     printf("Error %u returned from %s\n",err,msg);
  87.     DosExit(EXIT_PROCESS,0);
  88.     }
  89.