RMI

Java RMI Projects

RMI PROJECT 1

adplus-dvertising
RMI-Example
Previous Home Next

In this page of the tutorials we are going to take an example which demonstrate the RMI Addition Application. Well in this application you will use the four java files. List of java files and their code are shown below:

Step 1: Create Remote Interface.java Interface

package r4r;
import java.rmi.*;
public interface RemoteInterface extends Remote
{
public int addition(int x,int y)throws Exception;
}

step 2: Create ServerImplements.java Class

package r4r;
import java.rmi.*;
import java.rmi.server.*;
import java.lang.String;
import java.net.*;
interface RemoteInterface extends Remote {
public int addition(int x, int y) throws Exception;
}
public class ServerImplements extends UnicastRemoteObject 
implements RemoteInterface {
public ServerImplements() throws Exception {
super();
}
public int addition(int x, int y) {
return (x + y);
}
}

step 3: Create the Server.java class

package r4r;
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class client extends JFrame {
TextField tf1 = new TextField(30);
TextField tf2 = new TextField(30);
Label lb = new Label("Sum= 0");
JButton jb = new JButton("Add");
Panel pl = new Panel(new GridLayout(4, 1, 5, 5));
RemoteInterface si;
public client() {
super("Client Side");
setSize(250, 250);
setLocation(300, 300);
getContentPane().add(pl, "North");
pl.add(tf1);
pl.add(tf2);
pl.add(lb);
pl.add(jb);
try {
String ipp = JOptionPane
.showInputDialog("Please enter the IP Address to Connect");
String ip = "rmi://" + ipp + "/RMIAPPLICATION";
si = (RemoteInterface) Naming.lookup(ip);
} catch (Exception exp) {
JOptionPane.showMessageDialog(null, exp.getMessage());
}
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
try {
int r = si.addition(a, b);
lb.setText("Sum of two no =" + r);
} catch (Exception epx) {
}
}
});
}
public static void main(String args[]) {
client c = new client();
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setVisible(true);
}
}
Previous Home Next