Sie sind auf Seite 1von 4

Chaitanya Koparkar Roll no : 10-130 TE-INFT 1

Experiment No 1
Aim: To study and implement Fibonacci series and Factorial number using RMI technology Explanation/ Stepwise-Procedure/ Algorithm: Java Supports sockets to facilitate flexible communication between client and server. But it requires application level protocols to encode and decode messages which are cumbersome & error prone. Hence RPC which abstracts the communication interface to level of procedure call can be used. However, RPC is a communication mechanism between function and not objects. Thus to match object invocation remote object. In these systems, a stub object manages invocation on a remote object. RPC & RMI reside on top of implementation of request-reply protocol. The following models can be used for accessing interface in such a middleware: 1. Procedure Oriented Interface: Here the methods of remote objects and procedure. RPC is an example of such an interface. 2. Object Oriented Interface: Here methods of remote objects are defined as an object interface. In this paradigm, Objects can be parameters and results of methods. The IDL compilers are used to define interface in both paradigms. The process of RMI is done using following steps when client process makes calls to remote function. Following steps are executes to complete the RPC: 1.The Remote object which is willing to provide service registers in RMI registry. RMI registry has .details of remote object name and remote object reference. 2. The client refers RMI registry using object name. 3. The RMI registry returns the IOR of remote object. 4. Client procedure calls client stub using local procedure call. Conceptually, stubs can be generated automatically using stub compiler like RMIC and included in code during compilation. 5. The client stub calls network routine using system calls. It includes preparing message buffer, packaging parameter into buffer, building message and issuing send request to kernel. 6. In this step, local kernel sends a message to remote kernel. The message is copied into kernel and destination address is determined. Then kernel network interface is set up and a timer is

Chaitanya Koparkar Roll no : 10-130 TE-INFT 1 started for retransmission of message. 7. Once message is successfully received by remote kernel, it is transferred to RRL. Message is initially checked for validity and then stub & RRL to which message is to be sent are determined. 8. RRL then transfers call to server stub using system calls. If stub is awaiting then message is copied onto server stub. 9. Server stub unpacks parameters and calls server procedure. This process is called unmarshalling. 10. Server does work and returns result to server stub. 11. The server stub then packs result in message to RRL. 12. RRL contacts request reply protocol layer. 13. The server's kernel then transmits results to the client kernel. 14. The client's kernel gives result to client's stub. 15. The client's stub unpacks the result and returns them to client procedure.

Program Code:
import java.rmi.*; public interface FibInterface extends Remote {publicint factorial(int n1) throws RemoteException; public String fib(int n1) throws RemoteException; } FIBCLIENT importjava.rmi.Naming; import java.io.*; 1. public class FibClient { public static void main(String args[]) { try { FibInterface fi=(FibInterface)Naming.lookup("//192.168.27.113/Add"); inta,b; BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in)); a=Integer.parseInt(br.readLine()); System.out.println("Factorial is"+fi.factorial(a)); System.out.println("Fibbonacci is:"+fi.fib(a)); } catch(Exception e) { System.out.println("client exception:"+e); } } } FIBSERVER importjava.rmi.Naming;

Chaitanya Koparkar Roll no : 10-130 TE-INFT 1 importjava.rmi.server.*; importjava.rmi.*; public class FibServer extends UnicastRemoteObject implements FibInterface { publicFibServer() throws RemoteException{} publicint factorial(int n1) { int fact=1; for(inti=1;i<=n1;i++) { fact=fact*i; } return fact; } public String fib(int n1) { String s=""; intfibonacci=1; inti=0,pre=0,next=1,ans=0; s="0 1"; while(i<n1) { ans=pre+next; s=s+ans+""; pre=next; next=ans; i++; } return s; } public static void main(String args[]) { try { FibServer FS=new FibServer(); Naming.rebind("Add",FS); System.out.println("server is connected and waiting for client...."); } catch(Exception e) { System.out.println("Server could not connect +e"); } }

Chaitanya Koparkar Roll no : 10-130 TE-INFT 1 }

Output:

Conclusion: Thus we have studied and implemented RMI program. Real Life Application:
An understanding of Java RMI is fundament for implementing middleware in Object Oriented scenarios. RMI provides a significant advantage over RPC in that its implementation is a part of the Java VM provided in the standard SDKs. This practical provides students with the basic knowledge of deploying distributed object-oriented applications.

Das könnte Ihnen auch gefallen