home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12408.ZIP / SERVER.C < prev   
C/C++ Source or Header  |  1989-09-12  |  6KB  |  252 lines

  1. /*  Compilation and  Linking
  2.  
  3.     cl -Alfw -Zi -Gs -c server.c
  4.     link server,,,netapi  nampipes
  5.  
  6.     Usage:
  7.        server3
  8.     Comments:
  9.        This program shows the usage of Name Pipes, Semaphores, and
  10.        DosMuxSemWait.    Specifically:
  11.  
  12.       DosCreateSem,
  13.       DosSetNmPipeSem,
  14.       DosSemSet,
  15.       DosConnectNmPipe,
  16.       DosMuxSemWait,
  17.  
  18.       DosQNmPipeSemState
  19.       DosDisconnectNmPipe,
  20.       DosSemClear,
  21.       DosOpen
  22.  
  23.        1. The pipe is in non-blocked mode.
  24.        2. Hit space bar to stop program.
  25.        3. Run server on one machine and test it by running
  26.       number of clients (local or remote)
  27.     DisClaimer:
  28.        THIS PROGRAM IS FOR DEMONSTRATION ONLY. MICROSOFT MAKES NO WARRANTY
  29.        , EITHER EXPRESSED OR IMPLIED, AS TO IT'S USABILITY IN ANY GIVEN
  30.        SITUATION.
  31. */
  32.  
  33.  
  34.  
  35.  
  36. #define INCL_BASE
  37. #define INCL_DOSFILEMGR
  38.  
  39. #define INCL_DOSSEMAPHORES
  40. #define INCL_DOSERRORS
  41.  
  42. #include <os2.h>
  43.  
  44. #include <netcons.h>
  45. #include <nmpipe.h>
  46.  
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50.  
  51.  
  52.     /* PIPE PARAMETERS */
  53. #define POMODE    0x0002 /* DUPLEX */
  54. #define PPMODE NP_RMESG | NP_ICOUNT | NP_WMESG | NP_NBLK  /* NON-BLOCKED */
  55. #define INBUFSZ 4096
  56. #define OUTBUFSZ 4096
  57. #define SIZESEND  2040
  58. #define TIMEOUT 0L
  59. #define NUMPIPES 3
  60. USHORT    usPipeIndex;            /* pipe instance counter */
  61. #define DEFAULTNAME "\\pipe\\demo"
  62.  
  63.     /* SEMAPHORE DECLARATIONS */
  64.  
  65. USHORT usSemIndex;
  66. DEFINEMUXSEMLIST(MuxList, NUMPIPES) /* Used with DosMuxSemWait()  */
  67.  
  68.  
  69.     /* BUFFER FOR  NPSS STRUCTURES  */
  70. #define bufsize 3*sizeof(struct npss)
  71.          /* bufsize must be a multiple of the size of npss, 
  72.               because multiple npss's can be returned.          */
  73.  
  74. unsigned char buffer[bufsize][10];
  75. unsigned short buflen = bufsize;
  76. struct npss *npss_check = (struct npss *)buffer;
  77.          /* Doing type casts like this and those below is
  78.               recommended. Use the /W3 compile switch and add
  79.               type casts until the warning messages are all gone
  80.               to save potential grief debugging at run time.    */
  81.  
  82.  
  83.  
  84. main (int argc, char *argv[]) {
  85.  
  86.     unsigned rc;
  87.     unsigned short oWrt, oRd;
  88.     int  i,j, msglen;
  89.  
  90.                  /* Buffers */
  91.     char *sndbuf,
  92.      *rtnbuf,
  93.      *pname;
  94.     unsigned srvPipeHdl[NUMPIPES];    /*  Pipe Handle */
  95.  
  96.     static char SemName[20] = "\\SEM\\READ.OK";
  97.     char NewName[20], *pCh;
  98.     char cSem[2];
  99.     /* get pipe name */
  100.     if (argc >1)
  101.     pname = argv[1];
  102.     else
  103.     pname = DEFAULTNAME;
  104.  
  105.  
  106.  
  107.  
  108.     /*    allocate buffers for receiving/sending data */
  109.     sndbuf = (char *)malloc(OUTBUFSZ);
  110.     rtnbuf = (char *)malloc(OUTBUFSZ);
  111.     cSem[0] = 'A';
  112.  
  113.     printf ("***   SERVER  STARTED   ***\n\n");
  114.  
  115.  
  116.     /* Make a Pipe and Set Semaphore for Each Instance */
  117.  
  118.     MuxList.cmxs = NUMPIPES;
  119.     for (usPipeIndex=0; usPipeIndex <NUMPIPES; usPipeIndex++) {
  120.  
  121.        printf("Making the Pipe %i \n", usPipeIndex);
  122.  
  123.        rc = DosMakeNmPipe (pname, &srvPipeHdl[usPipeIndex],
  124.                     POMODE, PPMODE,
  125.                     INBUFSZ, OUTBUFSZ, TIMEOUT);
  126.        if (rc)
  127.           printf ("DosMakeNmPipe error #%u\n", rc);
  128.  
  129.      /* add pipe instance to semaphore names to generate unique names */
  130.  
  131.         strcpy(NewName, SemName);
  132.         cSem[0] += usPipeIndex;
  133.         pCh = strcat(NewName, cSem);
  134.  
  135.        /* create a semaphore */
  136.  
  137.         rc    = DosCreateSem(CSEM_PUBLIC,
  138.                    &MuxList.amxs[usPipeIndex].hsem,
  139.                    pCh);
  140.         if (rc)
  141.           printf("ERROR - DosCreateSem, rc = %d\n", rc);
  142.  
  143.  
  144.       /*  set the semaphore to pipe */
  145.  
  146.        rc = DosSetNmPipeSem(srvPipeHdl[usPipeIndex],
  147.                 (long)MuxList.amxs[usPipeIndex].hsem,
  148.                 usPipeIndex);
  149.        if (rc)
  150.            printf("ERROR - DosSetNmPipeSem, rc = %d\n", rc);
  151.  
  152.       /* set the semaphore */
  153.  
  154.        rc = DosSemSet(MuxList.amxs[usPipeIndex].hsem);
  155.        if (rc)
  156.            printf("ERROR - DosSemSet, rc = %d\n", rc);
  157.  
  158.       /* open a connection    */
  159.  
  160.        rc = DosConnectNmPipe(srvPipeHdl[usPipeIndex]);
  161.  
  162.     }
  163.  
  164.  do  {
  165.       /*  Wait on Semaphores */
  166.  
  167.  
  168.     rc = DosMuxSemWait(&usSemIndex, &MuxList, 5000L);
  169.  
  170.     if (ERROR_SEM_TIMEOUT==rc)
  171.           printf("Sem timed out \n");
  172.     else if (rc!=0)
  173.           printf("ERROR - DosSemWait, rc = %d\n", rc);
  174.  
  175.     else {        /* Some semaphore was cleared */
  176.  
  177.       rc = DosQNmPipeSemState((long) MuxList.amxs[usSemIndex].hsem,
  178.                 (char far *)buffer,
  179.                 buflen);
  180.       if (rc)
  181.         printf("ERROR - DosQNmPipeSemState, rc = %d\n", rc);
  182.  
  183.      /* We may have more than one npss structure returned. So let's
  184.         check each one.  */
  185.  
  186.       npss_check = (struct npss *)buffer;
  187.  
  188.       for (i=0; i<3; i++) {
  189.  
  190.  
  191.  
  192.         if (NPSS_EOI == npss_check->npss_status) {      /* End of Information */
  193.  
  194.         /*    EOF   */
  195.  
  196.         /* The possible values for npss_info->npss_status are
  197.               correctly defined in C:\LANMAN\NETSRC\H\NMPIPE.H,
  198.               but are wrong in the documentation.               */
  199.          }
  200.  
  201.          if (NPSS_RDATA==npss_check->npss_status) {     /* Read the Data */
  202.  
  203.  
  204.           if (rc = DosRead (srvPipeHdl[usSemIndex],
  205.                 rtnbuf, INBUFSZ-1, &oRd))
  206.          printf ("Server: DosRead error #%u\n", rc );
  207.  
  208.           if (oRd !=0) {                   /* Message */
  209.            printf("Message pipe: %i ", usSemIndex);
  210.            for (j=0; j < oRd; )
  211.              printf("%c",  rtnbuf[j++]);
  212.            printf("\n");
  213.            rtnbuf[0] ='\0';
  214.           }
  215.          }
  216.  
  217.          if (NPSS_WSPACE == npss_check->npss_status) {  /* write the data */
  218.           printf("Server>>");
  219.           scanf("%s", sndbuf);
  220.           msglen = strlen(sndbuf);
  221.  
  222.           sndbuf[msglen] = '\0';
  223.           if (rc = DosWrite (srvPipeHdl[usSemIndex],
  224.                  sndbuf, strlen(sndbuf), &oWrt))
  225.          printf( "Server: DosWrite Error #%u\n", rc);
  226.           sndbuf[0] ='\0';
  227.          }
  228.  
  229.         if (NPSS_CLOSE==npss_check->npss_status) {       /* Close the Pipe */
  230.  
  231.           if (rc = DosDisconnectNmPipe (srvPipeHdl[usSemIndex]))
  232.           printf ("DosDisConnectNmPipe error #%u\n", rc);
  233.  
  234.           printf("Server: closing pipe number %i\n", usSemIndex);
  235.  
  236.           /* Now Reopen the connection for next Client */
  237.           rc = DosConnectNmPipe(srvPipeHdl[usSemIndex]);
  238.           if (rc)
  239.          printf("DosConnectNmPipe error #%u\n", rc);
  240.         }
  241.  
  242.         npss_check += 1;
  243.       }  /* for */
  244.     }    /* else */
  245.  
  246.      }    while (1);         /* end of semaphore loop */
  247.  
  248.  
  249.  
  250.  
  251. }
  252.