[go: up one dir, main page]

0% found this document useful (0 votes)
860 views18 pages

RMI Example: Sum of Two Numbers in Java

The document describes an example program that uses RMI (Remote Method Invocation) to find the sum of two numbers. It includes code for an interface defining remote methods, a server class that implements the interface, and a client class that looks up and calls methods on the remote server. The server accepts two integer values from the client, stores them as instance variables, and returns their sum when called.

Uploaded by

Nagaraju
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
860 views18 pages

RMI Example: Sum of Two Numbers in Java

The document describes an example program that uses RMI (Remote Method Invocation) to find the sum of two numbers. It includes code for an interface defining remote methods, a server class that implements the interface, and a client class that looks up and calls methods on the remote server. The server accepts two integer values from the client, stores them as instance variables, and returns their sum when called.

Uploaded by

Nagaraju
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
  • Program Example for RMI
  • Client Setup
  • Steps to Execute RMI
  • Serialization and Deserialization
  • RMI Connection and Database Details

Example program to find the sum of 2 numbers using rmi

[Link]

import [Link].*;

public interface inter extends Remote

public void getdata(int m,int n) throws RemoteException;

int adddata() throws RemoteException;

[Link]

import [Link].*;

import [Link].*;

public class server extends UnicastRemoteObject


implements inter

int x,y;

public server() throws RemoteException

public int adddata() throws RemoteException


{

return x+y;

public void getdata(int m, int n) throws RemoteException

x=m; y=n;

public static void main(String arg[])

try

server s = new server();

[Link]("Addserver",s);

catch(Exception e)

[Link]("can not bund the name " + e);

}
 

[Link]

import [Link].*;

public class client

public static void main(String arg[ ])

try

int a = [Link](arg[1]);

int b = [Link](arg[2]);

int result;

inter i = (inter) [Link]("rmi://" + arg[0] +

"/Addserver");

[Link]("client");

[Link](a,b);

result = [Link]();

[Link](result);

catch(Exception e)
{

[Link]("can not connect with server " + e);

Important points

1. import [Link].* ; all methods in this package throws

RemoteException that must be caught.

2. Remote in interface indicates that this is remote


interface,

the methods can be used by object in the remote


machine.

3. Server extends UnicastRemoteObject implements

remote interface

Also import [Link].* for server program

4. [Link]("servername", serverobject);

Others methods are bind , rebind, unbind

5. In client, [Link]("rmi://ipaddress/servername")
method is used to identify the server and returns
remote interface.

6. Use [Link] to call the method in client.

 
Steps to exucute RMI

1. Write programs for interface, server and client and

compile them.

javac [Link]

javac [Link]

javac [Link] or javac *.java

2. Then use rmi compiler to create stub and skeleton class.

rmic server

3. Copy inter, server, skeleton to server machine and

inter, client and stub to client program

4. Then start rmi registry by

start rmiregistry

5. Run the server program in server machine by

java server

6. Execute the client program by

java client ipaddress arg1 arg2

java client [Link] 5 8

import [Link].*;

public class client

{
public static void main(String arg[])

try

int a = [Link](arg[1]);

int b = [Link](arg[2]);

int result;

inter i = (inter) [Link]("rmi://" + arg[0] +


"/Addserver");

[Link]("client");

[Link](a,b);

result = [Link]();

[Link](result);

catch(Exception e)

[Link]("error " + e);

}
 

/* Example for deserialization

ObjectInputStream(FileInputStream f)

*/

import [Link].*;

class myclass implements Serializable

String s;

int i;

int j;

myclass(String s1,int m,int n)

s=s1; i=m;j=n;

public String toString()

return (s + i + j);

class deser
{

public static void main(String arg[]) throws Exception

myclass ob2 ;

FileInputStream f = new FileInputStream("obj");

ObjectInputStream o = new ObjectInputStream(f);

ob2 = (myclass) [Link]();

[Link](ob2.s + ob2.i);

[Link]();

[Link]("Obeject2 " + ob2);

import [Link].*;

public interface inter extends Remote

public void getdata(int m,int n) throws RemoteException;

int adddata() throws RemoteException;

 
/* Example for serialization

ObjectOutputStream(FileOutputStream f)

[Link](ob)

*/

import [Link].*;

class myclass implements Serializable

String s;

int i;

int j;

myclass(String s1,int m,int n)

s=s1; i=m;j=n;

class ser

public static void main(String arg[]) throws IOException,


NotSerializableException

{
myclass ob = new myclass("Murali",4,5);

FileOutputStream f = new FileOutputStream("obj");

ObjectOutputStream o = new ObjectOutputStream(f);

[Link](ob);

[Link]();

[Link]();

[Link](ob);

import [Link].*;

import [Link].*;

public class server extends UnicastRemoteObject


implements inter

int x,y;

public server() throws RemoteException

public int adddata() throws RemoteException


{

return x+y;

public void getdata(int m, int n) throws RemoteException

x=m; y=n;

public static void main(String arg[])

try

server s = new server();

[Link]("Addserver",s);

catch(Exception e)

[Link]("Exception e");

}
 

import [Link].*;

import [Link].*;

public class clientport

public void actionPerformed(ActionEvent e)

if([Link]() == submit)

try

rmiserver server = (rmiserver)


[Link]("rmi//localhost/connect");

String name = getData();

catch(Exception e)

[Link]("Unable to connect");

}
}

import [Link].*;

public class details

private Connection connect = null;

private Statement query =null;

private ResultSet result = null;

String dsn;

public details(String dsn)

[Link] = "jdbc:odbc:" + dsn ;

try

[Link]("[Link]");

connect = [Link](dsn,"","");

query = [Link]();

catch(Exception e)
{

[Link]();

[Link]("Connection failed");

public void setData(String Table, String data)

try

// String sql = "Insert into " + table + (name)


values("'+data+'")";

String sql = "Insert into table(name) values('rrr')";

[Link](sql);

catch(Exception e)

[Link]("Query failed");

}
 

import [Link].*;

import [Link].*;

public class rmidatabase extends UnicastRemoteObject


implements rmiserver

private details det=null;

public rmidatabase(String name) throws RemoteException

super();

try

[Link](name,this);

catch(Exception e)

[Link]("Unable to bind");

det = new details("student");

}
public String receiveData(String s)

return null;

public void sendData(String data)

[Link]("Server sending data");

[Link]("registration",data);

public static void main(String a[])

try

[Link](new RMISecurityManager());

rmidatabase connect = new rmidatabase("connect");

[Link]("Server started");

catch(Exception e)

[Link]("Error in server");
}

import [Link].*;

interface rmiserver extends Remote

public void sendData(String data) throws RemoteException ;

public String receiveData(String data) throws


RemoteException ;

You might also like