| Next (Step 2) |
This page illustrates the basics of using ONC (Sun) remote procedure calls (RPC). We'll start with a remote procedure to add two numbers. This program will accept two numbers from the command line, parse the ascii text to convert them to numbers, then call a remote procedure to add them and print the results.
Traditionally, ONC RPC restricted us to remote procedures that accept a single parameter and return a single parameter. Later versions added an option to support multiple parameters, but we'll stick with the traditional mechanism to provide greatest compatibility. The lack of multiple parameters isn't really a problem since all we need to do is define a data structure that holds all the parameters we need.
In this example we have one type definition to define a structure that holds two integers: this will be our input parameter for the add function. Our interface will also have one version and one progam. We have to assign a number to each function, version, and program. The function will be given an ID of 1. So will the version. The program number is a 32-bit number. Sun reserved the range from 0 to 0x1fffffff. We'll number this program 0x23451111.
The IDL, which we'll put in a file named add.x looks like:
struct intpair { int a; int b; }; program ADD_PROG { version ADD_VERS { int ADD(intpair) = 1; } = 1; } = 0x23451111;
We can compile this to test if we missed anything:
The -C flag (upper-case C) tells rpcgen to generate C code that conforms to ANSI C. This is the default on some versions of rpcgen. If we look at the files that were generated, we'll see:rpcgen -C add.x
The program also implements the listener for the program. This is the function named add_prog_1 (the _1 is used to distinguish the version number. The function contains a switch statment for all the remote procedures supported by this program and this version. In addition to the null procedure (which is always supported), the only entry in the switch statement is ADD, for our add function. This sets a function pointer (local) to server function, add_1_svc. Later in the procedure, the function is invoked with the unmarshaled parameter and the requestor's information.