Introduction to programming with Sun/ONC RPC: step 3
Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | Step 6 |
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:
We will replace those comments with a single print statement:
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:
and change it to:
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:
to:
Now compile your program by running make. You'll see output similar to:
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:
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:
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:
In another window, run:
In the server window, you should see the following text appear:
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.