The following code example shows the client-side implementation of the READDIR procedure.
Example 3-6 Client-side Implementation of rls.c
As in other examples, execution is on systems named local and remote. The files are compiled and run as follows:
remote$ rpcgen dir.x remote$ cc -c dir_xdr.c remote$ cc rls.c dir_clnt.c dir_xdr.o -o rls -lnsl remote$ cc dir_svc.c dir_proc.c dir_xdr.o -o dir_svc -lnsl remote$ dir_svc |
When you install rls() on system local, you can list the contents of /usr/share/lib on system remote as follows:
local$ rls remote /usr/share/lib ascii eqnchar greek kbd marg8 tabclr tabs tabs4 local $ |
Client code generated by rpcgen does not release the memory allocated for the results of the RPC call. Call xdr_free() to release the memory when you are finished with it. Calling xdr_free() is similar to calling the free() routine, except that you pass the XDR routine for the result. In this example, after printing the list, xdr_free(xdr_readdir_res, result); was called.
Note - Use xdr_free() to release memory allocated by malloc(). Failure to use xdr_free() to release memory results in memory leaks.
Preprocessing Directives
rpcgen supports C and other preprocessing features. C preprocessing is performed on rpcgen input files before they are compiled. All standard C preprocessing directives are allowed in the.x source files. Depending on the type of output file being generated, five symbols are defined by rpcgen.
rpcgen provides an additional preprocessing feature: any line that begins with a percent sign (%) is passed directly to the output file, with no action on the line's content. Use caution because rpcgen does not always place the lines where you intend. Check the output source file and, if needed, edit it.
rpcgen uses the preprocessing directives listed in the following table.
Table 3-1 rpcgen Preprocessing Directives
Symbol | Use |
---|---|
Header file output | |
XDR routine output | |
Server stub output | |
Client stub output | |
Index table output |
The following code example is a simple rpcgen example. Note the use of rpcgen`s pre-processing features.
Example 3-7 Time Protocol rpcgen Source