RPC and RMI

Yeah I know this is after a looong time but what to do writing a post is not like writing a poem it takes time. I didn’t have any free time for the past few days but now I’m in a really good mood to write a post :D
This is basically an assignment we got from the subject called Middleware Architecture I’m not gonna talk about middleware now but this is a basic example of RPC and RMI. This might shock you but I’m gonna tell it to you it is… I did this using windows :-| I couldn’t find a way to do it in ubuntu its simply because I don’t have time to google it but there is something to tell you guys that is I did it in the virtualbox using Guest Additions makes it really cool it gives you the feeling that you are actually in windows ;)
Ok then lets get to the serious business (its not that serious at all simple like a piece of cake)
The following link has the codes you need to understand RPC and RMI
RPC and RMI Code

Now its time to tell you how this works first go to Windows ;) and open three command prompts go to the RPC folder from all the three command prompt windows

Example cd C:\RMI&RPC\RPC_example or cd C:\RMI&RPC\RMI_example

If you are in the RPC folder extract the orpc.jar file it will create two folders called META-INF and netbula now we are good to go.

Note this is the RPC part do this in the command prompt(cmd)

1. Type jrpcgen msg.x
2. Then javac ClientTest.java msgserv.java
3. Now javac msgsvc.java
4. Finally javac pmapsvc.java

this finishes compiling and now we have to run the code for that

1. Take the first terminal
2. Type java pmapsvc this runs the netbula server
3. Then type java msgsvc this runs the message server
4. Finally java ClientTest localhost this takes the commandline argument localhost

Now the testing part (this is a really simple demo)
when you run this you can see Hello World will get printed Five times now you have to do this
comment the following part

/*
String msg=”Hello World!\n”;
System.out.println(“Sending…”);
for(int i=0;i<5;i++){
String reply=c1.sendmsg(msg);
System.out.println("got"+reply+"\n");
}
*/

and now add this part instead of that

BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println(“Your message :”);
String msg = breader.readLine();
if(msg.equals(“exit”)){
break;
}
String reply=c1.sendmsg(msg);
System.out.println(“got”+reply+”\n”);
}

and don’t forget to catch the exception which will be thrown by the BufferedReader and the InputStreamReader by adding these lines (hope you can get the difference)

}catch(rpc_err e){
System.out.print(“rpc: “+e.toString());
} catch (Exception e) {
System.out.println(“Exception :: ” + e);
}

and also don’t forget to import

import java.io.*;

now run the code again and you simply have to type something in the cmd where you ran ClientTest vola you can see the String you wrote in the cmd window where you ran msgsvc
Thats it!! you used RPC :D then its time for experimenting :D

Note this is the RMI part do this in the command prompt(cmd)

Yet again you need three command prompts
First go to cd C:\RMI&RPC\RMI_example or the folder with the RMI code

1. Run javac SampleClient.java SampleServerImpl.java
2. Finally then run rmic SampleServerImpl this will create some more class files

This finishes compiling now its the running part

1. Take a cmd type rmiregistry and hit enter
2. Then take the second cmd java SampleServerImpl
3. Finally type java SampleClient

And again now its time for you to experiment :D

Try it out and tell me I’m glad to help you in

~~ Rusiru Boteju ~~

Follow

Get every new post delivered to your Inbox.