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

  1. /* qclient.c RHS 5/1/89
  2.  *
  3.  *
  4.  This program demonstrates the use of queues. To use it, you should first
  5.  start QSERVER.C with:
  6.  
  7.  QSERVER
  8.  
  9.  Then you can run multiple copies of this program with:
  10.  
  11.  QCLIENT
  12.  
  13.  */
  14. #define INCL_DOS
  15. #define INCL_KBD
  16. #define INCL_ERRORS
  17.  
  18. #include<os2.h>
  19. #include<string.h>
  20. #include<stdio.h>
  21. #include"q.h"
  22.  
  23. char *p = "Hello!";
  24. #define MAX_PRIORITY    15
  25.  
  26. void error_exit(USHORT err, char *msg);
  27. PID Os2GetPid(void);
  28. void main(void);
  29.  
  30. void main(void)
  31.     {
  32.     USHORT retval;
  33.     KBDKEYINFO kbdkeyinfo;
  34.     HQUEUE qhandle;
  35.     BYTE priority = 0;
  36.     SEL selector,qownersel;
  37.     PID qowner;
  38.     PCH ptr;
  39.     int len;
  40.     PID pid;
  41.  
  42.     pid = Os2GetPid();
  43.  
  44.     while(TRUE)
  45.         {
  46.         if(!(retval = DosOpenQueue(&qowner,&qhandle,QUEUENAME)))
  47.             break;
  48.         if(retval != ERROR_QUE_NAME_NOT_EXIST)
  49.             error_exit(retval,"DosOpenQueue");
  50.         else
  51.             {
  52.             DosSleep(1000L);
  53.             continue;
  54.             }
  55.         }
  56.     printf("Client (%u) has opened queue, now messaging...\n",pid);
  57.     
  58.     while(TRUE)
  59.         {
  60.         if(retval = DosAllocSeg(len = (strlen(p)+1),&selector,SEG_GIVEABLE))
  61.             error_exit(retval,"DosAllocSeg");
  62.  
  63.         ptr = MAKEP(selector,0);
  64.         strcpy(ptr,p);
  65.         if(retval = DosGiveSeg(selector,qowner,&qownersel))
  66.             error_exit(retval,"DosGiveSeg");
  67.  
  68.         ptr = MAKEP(qownersel,0);
  69.  
  70.         if(retval = DosWriteQueue(qhandle,0,len,(PBYTE)ptr,priority))
  71.             error_exit(retval,"DosWriteQueue");
  72.  
  73.         DosFreeSeg(selector);
  74.         printf("Client (%u) sent message, priority %u\n",pid,priority);
  75.  
  76.         KbdCharIn(&kbdkeyinfo,IO_NOWAIT,0);            /* check for key press        */
  77.         if(kbdkeyinfo.fbStatus & FINAL_CHAR_IN)    /* if pressed, break out    */
  78.             break;
  79.         if(++priority > MAX_PRIORITY)
  80.             priority = 0;
  81.         DosSleep(50L);
  82.         }
  83.  
  84.     DosCloseQueue(qhandle);
  85.     DosExit(EXIT_PROCESS,0);                            /* and exit                        */
  86.     }
  87.  
  88. void error_exit(USHORT err, char *msg)
  89.     {
  90.     printf("Error %u returned from %s\n",err,msg);
  91.     DosExit(EXIT_PROCESS,0);
  92.     }
  93.  
  94. PID Os2GetPid(void)                                        /* returns process id        */
  95.     {
  96.     PIDINFO pidinfo;
  97.  
  98.     DosGetPID(&pidinfo);                                    /* get process id                */
  99.     return pidinfo.pid;                                    /* return it                    */
  100.     }
  101.  
  102.  
  103.