Sunday, March 7, 2010

Simple RMI Example

This is a simple RMI Client and Server application in Java. RMI is Remote Method Invocation, and is similar to RPC in .NET.

RmiServer.java (RMI Server)
1 import java.rmi.*;
2 import java.rmi.registry.*;
3 import java.rmi.server.*;
4 import java.net.*;
5
6 public class RmiServer extends java.rmi.server.UnicastRemoteObject
7 implements ReceiveMessageInterface
8 {
9 int thisPort;
10 String thisAddress;
11 Registry registry; // rmi registry for lookup the remote objects.
12
13 // This method is called from the remote client by the RMI.
14 // This is the implementation of the “ReceiveMessageInterface”.
15
16 static int fact(int n) {
17
18 // Base Case:
19 // If n <= 1 then n! = 1.
20 if (n <= 1) {
21 return 1;
22 }
23 // Recursive Case:
24 // If n > 1 then n! = n * (n-1)!
25 else {
26 return n * fact(n-1);
27 }
28 }
29
30 public void receiveMessage(String x) throws RemoteException
31 {
32 try
33 {
34 int y = fact(Integer.parseInt(x.trim()));
35 System.out.println("From Client, Factorial of " + x + " is " + y);
36 }
37 catch (NumberFormatException nfe)
38 {
39 System.out.println(x);
40 }
41
42 }
43
44 public RmiServer() throws RemoteException
45 {
46
47 try{
48 // get the address of this host.
49 thisPort=4544;
50 thisAddress= (InetAddress.getLocalHost()).toString();
51 }
52 catch(Exception e){
53 throw new RemoteException("can't get inet address.");
Assignment 2 – Java RMI – Amit Barman 11
54 }
55
56 System.out.println("this address="+thisAddress+",port="+thisPort);
57 try{
58 // create the registry and bind the name and object.
59 registry = LocateRegistry.createRegistry( thisPort );
60 registry.rebind("rmiServer", this);
61 }
62 catch(RemoteException e){
63 throw e;
64 }
65 }
66
67 static public void main(String args[])
68 {
69 //int thisPort2=Integer.parseInt(args[0].trim());
70 try{
71 RmiServer s=new RmiServer();
72 }
73 catch (Exception e) {
74 e.printStackTrace();
75 System.exit(1);
76 }
77 }
78 }
79
RmiClient.java (RMI Client)
1 import java.rmi.*;
2 import java.rmi.registry.*;
3 import java.net.*;
4
5 public class RmiClient
6 {
7 static public void main(String args[])
8 {
9 ReceiveMessageInterface rmiServer;
10 Registry registry;
11 String serverAddress=args[0];
12 String serverPort=args[1];
13 String text=args[2];
14 System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
15
16 try {
17 java.net.InetAddress i = java.net.InetAddress.getLocalHost();
18 System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
19
20 System.out.println(i.getHostName()); // name
21 System.out.println(i.getHostAddress()); // IP address only
22 System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
23
24 }
25 catch(Exception e){e.printStackTrace();}
26
27 try{
28 // get the “registry”
29 registry=LocateRegistry.getRegistry(
30 serverAddress,
Assignment 2 – Java RMI – Amit Barman 12
31 (new Integer(serverPort)).intValue()
32 );
33 // look up the remote object
34 rmiServer=
35 (ReceiveMessageInterface)(registry.lookup("rmiServer"));
36 // call the remote method
37 rmiServer.receiveMessage("----------------------------------");
38 try {
39 java.net.InetAddress x = java.net.InetAddress.getLocalHost();
40 rmiServer.receiveMessage(x.getHostName() + " | " + x.getHostAddress());
41
42 }
43 catch(Exception e){e.printStackTrace();}
44
45
46 rmiServer.receiveMessage(text);
47 rmiServer.receiveMessage("----------------------------------");
48
49 }
50 catch(RemoteException e){
51 e.printStackTrace();
52 }
53 catch(NotBoundException e){
54 e.printStackTrace();
55 }
56 }
57
58 }
59
60
ReceiveMessageInterface.java (Message Receiving Interface Implementation)
1 import java.rmi.*;
2
3
4 public interface ReceiveMessageInterface extends Remote
5
6 {
7
8 void receiveMessage(String x) throws RemoteException;
9
10 }
11

No comments:

Post a Comment