Introduction to programming with Sun/ONC RPC: step 3

Step 3. First test of the client and server

The template code written by rpcgen created a client program named add_client.c that accepts a single argument on the command line containing the name of the server, creates an RPC handle to the server process, and calls the add_1 function. A client template for an interface with more functions would contain a declaration of parameters and return values for each of the functions and call them one after the other.

The server function, contained in add_server.c is a function which does nothing but contains the comments:

/* * insert server code here */

We will replace those comments with a single print statement:

printf("add function called\n");

Important!
Before we compile, we will make a change to the makefile. We will make sure that the server is compiled so that the symbol RPC_SVC_FG is defined. This will cause our server to run in the foreground. For testing purposes, this is convenient since we'll be less likely to forget about it and it will be easier to kill (we don't have to look up its process ID).

Edit the makefile and find the line that defines CFLAGS:

CFLAGS += -g

and change it to:

CFLAGS += -g -DRPC_SVC_FG

Secondly, we want to make sure that rpcgen generates code that conforms to ANSI C, so we'll add a -C (capital C) parameter to the rpcgen command. Change the line in the makefile that defines:

RPCGENFLAGS =

to:

RPCGENFLAGS = -C

Now compile your program by running make. You'll see output similar to:

cc -g -DRPC_SVC_FG -c -o add_clnt.o add_clnt.c cc -g -DRPC_SVC_FG -c -o add_client.o add_client.c cc -g -DRPC_SVC_FG -c -o add_xdr.o add_xdr.c cc -g -DRPC_SVC_FG -o add_client add_clnt.o add_client.o add_xdr.o -lnsl cc -g -DRPC_SVC_FG -c -o add_svc.o add_svc.c cc -g -DRPC_SVC_FG -c -o add_server.o add_server.c cc -g -DRPC_SVC_FG -o add_server add_svc.o add_server.o add_xdr.o -lnsl

Note that the -lnsl argument is not needed when linking under Linux, *BSD, or OS X.

If you're running OS X, Linux, or BSD and don't have a makefile, run the above commands using gcc as the compiler and omitting -lnsl:

gcc -g -DRPC_SVC_FG -c -o add_clnt.o add_clnt.c gcc -g -DRPC_SVC_FG -c -o add_client.o add_client.c gcc -g -DRPC_SVC_FG -c -o add_xdr.o add_xdr.c gcc -g -DRPC_SVC_FG -o add_client add_clnt.o add_client.o add_xdr.o gcc -g -DRPC_SVC_FG -c -o add_svc.o add_svc.c gcc -g -DRPC_SVC_FG -c -o add_server.o add_server.c gcc -g -DRPC_SVC_FG -o add_server add_svc.o add_server.o add_xdr.o

Unfortunately, the compilation produces a number of warnings but you can ignore them.

The result is that you have two executables: add_client and add_server. You can move add_server to another machine or run it locally, giving add_client your local machine's name or the name localhost.

If you're running this on a non-SunOS machine, you may need to start the RPC port mapper first. Chances are you won't and you should try running the server first. If it does not exit immediately with an unable to register error, you're probably good to go. If you do need to start the portmapper then here are the commands that you'll need to run on several popular operating systems:

OS command
Mac OS X
 launchctl start com.apple.portmap 
Linux
/sbin/portmap
*BSD
/usr/sbin/rpcbind

In one window, run:

./add_server

If you're on OS X Leopard, you'll need to be root to start the server. Either su and get a root shell or run:

sudo ./add_server

In another window, run:

./add_client localhost

In the server window, you should see the following text appear:

add function called

This confirms that the client and server are communicating. If your client just seems to be hanging, chances are you have not started the portmapper.

Continue to step 4

Back to step 2