Netbula LLC                                                                   sitemapcontact us
about usproductssupportdownloadpurchase


Introduction
White Paper
JavaRPC API Doc
Online Tutorial
Online Demo
Msg Applet/Server
Download
RPC Forum

 

Java RPC Programming Tutorial

A step by step illustration on using the Java RPC SDK


In this tutorial, we illustrate how to build a Java RPC client for a simple Msg RPC server/client. This tutorial is not about programming ONC RPC, but about how to use the JavaRPC tool. If you are not familiar with ONC RPC, the book "Power Programming RPC" from O'Reilly is a good guide.

The Msg server is defined by the following RPC IDL

%cat msg.x

	version MSGSERV_V1 {

		string sendmsg(string)=2;
         
          		   } = 1;
          
       	} = 1234567;
             

The interface defines an RPC program with a single procedure sendmsg, the client sends a string to the server, and the server returns the same string back.

The C version of the Msg client/server is available from the Netbula ONC RPC For Win32 SDK.

Now let's build the Msg RPC client in Java.

Step 1. Compile the Msg.x with jrpcgen

At the command prompt, run

% jrpcgen msg.x

The demo package includes jrpcgen binaries for win32, solaris and linux, they generate identical Java code.

This would produce the following files:

Normally, jrpcgen would produce four kinds of Java source code files

  • XDR classes for user defined types
  • RPC program interface
  • Client stub class
  • Server stub class

In our case, there is no XDR classes, because string is a built-in type.

Step 2. Code the main client application

%cat ClientTest.java
import netbula.ORPC.*;
               
	import java.net.*;
	public class ClientTest {  
	public ClientTest () {}
                              
	static public void main(String args[]) {
               
	try {
               
	msgserv_cln cl = new msgserv_cln(args[0], "udp");
               
	cl.setAuth(new AuthUnix
	   ("localhost", 501, 100, new int[2]));                
               
	String msg = "hello world\n";
               
	System.out.println("sending.. ");
               
	for(int i=0; i<5; i++){
                                         
	String reply = cl.sendmsg(msg);
               
	System.out.println(
          "got " + reply +"\n");
               
             }
                                           
	}catch (rpc_err e) {
               
	System.out.println("rpc: " + e.toString());
               
             }
               
           }
        }

Here, we construct a Msg client which connects to the Msg server on localhost with UDP protocol, sends a message, and print out the reply.

Step 3. Compile the client

Make sure that the netbula.ORPC package is in the classpath ( simply add the orpc.jar file to the CLASSPATH environment variable).

Run the source through the Java compiler:

% javac ClientTest.java MSGSERV_1.java

This would produce two class files: ClientTest.class and MSGSERV_1.class.

Step 4. Run the Msg client

  1. Make sure the Msg server (C version or Java version) is running on localhost
  2. Run the client
    % java ClientTest

If the server is running, you should see the client print out the reply from the server, otherwise, it will print out an RPC error: Program not registered.

That is it!

Now, let's build the Msg server in Java

Step 5 Code the Msg server

The jrpcgen generates msgserv_svcb.java, which defines an abstract class msgserv_svcb with an abstract function sendmsg. To fully implement the server, one needs to derive a class which supplies a body for the sendmsg function.
 
	import netbula.ORPC.*;

	class msgsvc extends msgserv_svcb {
        //implement the server function, 
		let's just echo the msg back

	String sendmsg(String msg) {

	System.out.println("got msg from client "+ msg);

	return msg;

	 }//main function runs the server

	public static void main(String srgv[]) {

	//let's run the server
		
	new msgsvc().run(); 

	}
            }

Step 6 Compile and run the java server

%javac msgsvc.java
%java msgsvc

*) Implement the Msg server/client in C

1) Use rpcgen to compile msg.x file into client server stubs. rpcgen is available on unix, rpcgen for win32 is available from Netbula.

2) Code the server implmentation
See the cservs directory in the JavaRPC package for sample code.


| Home | Products | Support | Download | Purchase | Site Map | Contact Us |
Copyright © 2000, Netbula LLC, All Rights Reserved.

 

Anyemail Anyboard JavaRPC ONC RPC PowerRPC