Specifications

Remote Procedure Call Programming Guide Page 5
discussed later in this document. The remote server procedure corresponding to the above might look like
this:
char *
nuser(indata)
char *indata;
{
unsigned long nusers;
/*
* Code here to compute the number of users
* and place result in variable nusers.
*/
return((char *)&nusers);
}
It takes one argument, which is a pointer to the input of the remote procedure call (ignored in our example),
and it returns a pointer to the result. In the current version of C, character pointers are the generic pointers,
so both the input argument and the return value are cast to char *.
Normally, a server registers all of the RPC calls it plans to handle, and then goes into an infinite loop wait-
ing to service requests. In this example, there is only a single procedure to register, so the main body of the
server would look like this:
#include <stdio.h>
#include <rpc/rpc.h>
#include <utmp.h>
#include <rpcsvc/rusers.h>
char *nuser();
main()
{
registerrpc(RUSERSPROG, RUSERSVERS, RUSERSPROC_NUM,
nuser, xdr_void, xdr_u_long);
svc_run(); /* Never returns */
fprintf(stderr, "Error: svc_run returned!\n");
exit(1);
}
The registerrpc() routine registers a C procedure as corresponding to a given RPC procedure number. The
first three parameters, RUSERPROG, RUSERSVERS, and RUSERSPROC_NUM are the program, version,
and procedure numbers of the remote procedure to be registered; nuser() is the name of the local procedure
that implements the remote procedure; and xdr_void() and xdr_u_long() are the XDR filters for the remote
procedure’s arguments and results, respectively. (Multiple arguments or multiple results are passed as
structures).
Only the UDP transport mechanism can use registerrpc() thus, it is always safe in conjunction with calls
generated by callrpc().
Warning: the UDP transport mechanism can only deal with arguments and results less than 8K bytes
in length.
After registering the local procedure, the server program’s main procedure calls svc_run(), the RPC
library’s remote procedure dispatcher. It is this function that calls the remote procedures in response to
RPC call messages. Note that the dispatcher takes care of decoding remote procedure arguments and
encoding results, using the XDR filters specified when the remote procedure was registered.