7.12.1.  Examples

The examples were tested on Linux with the default RPC implementation. The default RPC implementation needs no special configuration to run the examples, but it will require running portmapper.

First, an example of an interface definition that contains a function for printing a string.

Figure 7.1.  Print Interface Example

program PRINT
{
  version VERSION
  {
    void PRINT_STR (string STR) = 1;    /* function number 1  */
  } = 1;                                /* version number 1   */
} = 666;                                /* service number 666 */

To generate stubs, store the interface definition in a file named Print.x and use the following command:

rpcgen -a Print.x

The -a command line option tells the middleware to generate sample client and server in addition to stubs. The generated files are:

Makefile.Print
Print_client.c
Print_clnt.c
Print.h
Print_server.c
Print_svc.c

Next, an example modification of the sample client and server that prints a Hello World message using the function for printing a string.

Figure 7.2.  Sample Client Modification Example

void print_1 (char *host)
{
  ...
  char * print_str_1_arg = "Hello World !\n";
  ...
}

Figure 7.3.  Sample Server Modification Example

#include <stdio.h>
...
void *print_str_1_svc (char **argp, struct svc_req *rqstp)
{
  static char *result = 0;
  printf ("%s", *argp);
  return (void *) &result;
}

To compile and run the example, use the following commands, with the server and the client in different windows:

make -f Makefile.Print
./Print_server
./Print_client localhost