home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / doctor / doctorc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  6.1 KB  |  187 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                        Doctor Example
  5.  
  6.     FILE:       doctorc.c
  7.  
  8.     USAGE:      doctorc  -n network_address
  9.                          -p protocol_sequence
  10.                          -e endpoint
  11.                          -o options
  12.  
  13.     PURPOSE:    Client side of RPC distributed application
  14.  
  15.     FUNCTIONS:  main() - binds to server and calls remote procedure
  16.  
  17.     COMMENTS:   This version of the distributed application prints
  18.                 strings from the Doctor server.  Doctor is an Eliza-like
  19.                 personal therapist program that works by simple
  20.                 pattern-matching.
  21.  
  22. ****************************************************************************/
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include "doctor.h"    // header file generated by MIDL compiler
  29.  
  30. #define PURPOSE \
  31. "This Microsoft RPC Version 2.0 sample program demonstrates\n\
  32. the use of the [string] and [size_is] attributes. For more\n\
  33. information about attributes and RPC API functions, see the\n\
  34. RPC programming guide and reference.\n\n"
  35.  
  36. #define GREETING \
  37. "The doctor is in.\n\
  38. I am at your service; just tell me anything that troubles\n\
  39. or concerns you. Please end your sentences with a period,\n\
  40. a question mark, or an exclamation point, and then press\n\
  41. the RETURN or ENTER key.  When you are ready to quit our\n\
  42. session, just enter \"bye\" and press RETURN or ENTER.\n\n\
  43. What is your name? >"
  44.  
  45. #define FAREWELL \
  46. "I hope I have been of some service to you.\n\
  47. Let's get together again some time.\n\n"
  48.  
  49.  
  50. void Usage(char * pszProgramName)
  51. {
  52.     fprintf(stderr, "%s", PURPOSE);
  53.     fprintf(stderr, "Usage:  %s\n", pszProgramName);
  54.     fprintf(stderr, " -p protocol_sequence\n");
  55.     fprintf(stderr, " -n network_address\n");
  56.     fprintf(stderr, " -e endpoint\n");
  57.     fprintf(stderr, " -o options\n");
  58.     exit(1);
  59. }
  60.  
  61. void _CRTAPI1 main(int argc, char **argv)
  62. {
  63.     RPC_STATUS status;                 // returned by RPC API function
  64.     unsigned char pszName[STRSIZE];    // patient name
  65.     unsigned char achIn[STRSIZE];      // patient input
  66.  
  67.     unsigned char * pszUuid             = NULL;
  68.     unsigned char * pszProtocolSequence = "ncacn_np";
  69.     unsigned char * pszNetworkAddress   = NULL;
  70.     unsigned char * pszEndpoint         = "\\pipe\\doctor";
  71.     unsigned char * pszOptions          = NULL;
  72.     unsigned char * pszStringBinding    = NULL;
  73.     int i;
  74.  
  75.     /* allow the user to override settings with command line switches */
  76.     for (i = 1; i < argc; i++) {
  77.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  78.             switch (tolower(*(argv[i]+1))) {
  79.             case 'p':  // protocol sequence
  80.                 pszProtocolSequence = argv[++i];
  81.                 break;
  82.             case 'n':  // network address
  83.                 pszNetworkAddress = argv[++i];
  84.                 break;
  85.             case 'e':
  86.                 pszEndpoint = argv[++i];
  87.                 break;
  88.             case 'o':
  89.                 pszOptions = argv[++i];
  90.                 break;
  91.             case 'h':
  92.             case '?':
  93.             default:
  94.                 Usage(argv[0]);
  95.             }
  96.         }
  97.         else
  98.             Usage(argv[0]);
  99.     }
  100.  
  101.     /* Use a convenience function to concatenate the elements of  */
  102.     /* the string binding into the proper sequence.               */
  103.     status = RpcStringBindingCompose(pszUuid,
  104.                                      pszProtocolSequence,
  105.                                      pszNetworkAddress,
  106.                                      pszEndpoint,
  107.                                      pszOptions,
  108.                                      &pszStringBinding);
  109.     if (status) {
  110.         printf("RpcStringBindingCompose returned 0x%x\n", status);
  111.         printf("pszStringBinding = %s\n", pszStringBinding);
  112.         exit(status);
  113.     }
  114.  
  115.     /* Set the binding handle that will be used to bind to the server. */
  116.     status = RpcBindingFromStringBinding(pszStringBinding,
  117.                                          &doctor_IfHandle);
  118.     if (status) {
  119.         printf("RpcBindingFromStringBinding returned 0x%x\n", status);
  120.         exit(status);
  121.     }
  122.  
  123.     /* RPC is now initialized.  Call remote procedures as if        */
  124.     /* they were local procedures.                                  */
  125.  
  126.     /* The doctor program consists of patient statements and doctor */
  127.     /* responses.  The patient string is transmitted to the server, */
  128.     /* and all processing is performed on the server.               */
  129.  
  130.     printf("%s", GREETING);
  131.     gets(pszName);
  132.     printf("\n%s>", pszName);
  133.  
  134.     while (gets(achIn)) {
  135.         if (strncmp(achIn, "bye", 3) == 0)  // end of session?
  136.             break;
  137.         RpcTryExcept {
  138.             Analyze(&achIn[0]);
  139.         }
  140.         RpcExcept(1) {
  141.             printf("Runtime reported exception %ld\n", RpcExceptionCode() );
  142.             break;
  143.         }
  144.         RpcEndExcept
  145.  
  146.         printf("%s%s>", achIn, pszName);    // no, continue
  147.     }
  148.  
  149.     RpcTryExcept {
  150.         Shutdown();                             // yes, shutdown the server
  151.     }
  152.     RpcExcept(1) {
  153.         printf("Runtime reported exception %ld\n", RpcExceptionCode() );
  154.     }
  155.     RpcEndExcept
  156.  
  157.     /*  The calls to the remote procedure are complete.  */
  158.     /*  Free the binding handle.                         */
  159.     status = RpcBindingFree(&doctor_IfHandle);  // remote calls done; unbind
  160.     if (status) {
  161.        printf("RpcBindingFree returned 0x%x\n", status);
  162.        exit(status);
  163.     }
  164.  
  165.     printf("%s", FAREWELL);
  166.  
  167.     exit(0);
  168.  
  169. }  // end main()
  170.  
  171.  
  172. /*********************************************************************/
  173. /*                 MIDL allocate and free                            */
  174. /*********************************************************************/
  175.  
  176. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  177. {
  178.     return(malloc(len));
  179. }
  180.  
  181. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  182. {
  183.     free(ptr);
  184. }
  185.  
  186. /* end file doctorc.c */
  187.