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 is show you how will see function calls from client to server. This application for running requires a Remote Machine.

For running this application we need to machine in which first is server and second is client. the server IP address is 192.168.10.97 and client IP address is 192.168.10.113.

Here machine having IP address 192.168.10.113 is a client and machine having IP address 192.168.10.97 is a server.

So we divide the programs in two categories one for server and other for client.

Well in this application there are seven java files. List of java files and their code are shown below.

step 1: Create RemoteInterface1.java interface

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

step 2: Create RemoteInterface2.java interface

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

step 3: Create ServerImplements.java Class

package r4r;
import java.rmi.*;
import java.rmi.server.*;
interface RemoteInterface1 extends Remote {
public int add(int a, int b) throws Exception;
}
public class ServerImplements extends UnicastRemoteObject 
implements RemoteInterface1 {
public ServerImplements() throws Exception {
super();
}
public int add(int a, int b) {
return (a + b);
}
}

step 4: Create ServerImplements2.java class

package r4r;
import java.rmi.*;
import java.rmi.server.*;
interface RemoteInterface extends Remote {
public int sub(int a, int b) throws Exception;
}
public class ServerImplements2 extends UnicastRemoteObject
implements RemoteInterface2 {
public ServerImplements2() throws Exception {
super();
}
public int sub(int a, int b) {
return (a - b);
}
}

step 5: Create the Server1.java class

package r4r;
import java.rmi.*;
import java.net.*;
public class server1 {
public static void main(String args[]) {
try {
ServerImplements si1 = new ServerImplements();
Naming.rebind("SERVICE", si1);
System.out.println("Server 1 Started");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

step 6: Create the server2.java class

package r4r;
import java.rmi.*;
import java.net.*;
public class server2 {
public static void main(String args[]) {
try {
ServerImplements2 si2 = new ServerImplements2();
Naming.rebind("SERVICE2", si2);
System.out.println("Server 2 Started");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

step 7: Create Client.java class

package r4r;
import java.rmi.*;
import java.net.*;
import java.io.*;
public class Client {
public static void main(String args[]) {
try {
String ip1 = "rmi://192.168.10.97/SERVICE";
String ip2 = "rmi://192.168.10.113/SERVICE2";
RemoteInterface1 si1 = (RemoteInterface1) Naming.lookup(ip1);
RemoteInterface2 si2 = (RemoteInterface2) Naming.lookup(ip2);
System.out.println("Add:" + si1.add(1, 4));
System.out.println("sub:" + si2.sub(3, 1));
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Previous Home Next