RMI-Example-3

In this example you will see function calls from client to server and vice versa. This application for running requires a Remote Machine.

RMI-Example-3

RMI-Example-3

     

In this example you will see function calls from client to server and vice versa. This application for running requires a Remote Machine. 

For running this application lets assume there are two machine one with 192.168.10.97 and other 192.168.10.113.

Here machine having IP address 192.168.10.113 is a server and machine having IP address 192.168.10.97 is a client. 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:

Code for Server and Client Machine:

RemoteInterface.java

import java.rmi.*;

public interface RemoteInterface extends Remote {
  public int add(int x, int y) throws Exception;
}

RemoteInterface1.java

import java.rmi.*;

public interface RemoteInterface2 extends Remote {
  public int sub(int x, int y) throws Exception;
}

ServerImplements.java

import java.rmi.*;
import java.rmi.server.*;

interface RemoteInterface extends Remote {
  public int add(int x, int y) throws Exception;
}

public class ServerImplements extends UnicastRemoteObject implements
RemoteInterface {

  public ServerImplements() throws Exception {
  super();
  }

  public int add(int x, int y) {
  return (x + y);
  }
}

ServerImplements2.java

import java.rmi.*;
import java.rmi.server.*;

interface RemoteInterface2 extends Remote {
  public int sub(int x, int y) throws Exception;
}

public class ServerImplements2 extends UnicastRemoteObject implements
  RemoteInterface2 {

  public ServerImplements2() throws Exception {
  super();
  }

  public int sub(int x, int y) {
  return (x - y);
  }
}

Server.java

import java.rmi.*;
import java.net.*;

public class Server {
  public static void main(String args[]) {
  try {
  ServerImplements s = new ServerImplements();
  Naming.rebind("SERVICE", s);
  System.out.println("Server Started 1");
  catch (Exception e) {
  System.out.println(e.getMessage());
  }
  }
}

Server2.java

import java.rmi.*;
import java.net.*;

public class Server2 {
  public static void main(String args[]) {
  try {
  ServerImplements2 s2 = new ServerImplements2();
  Naming.rebind("SERVICE2", s2);
  System.out.println("Server Started 2");
  catch (Exception e) {
  System.out.println(e.getMessage());
  }
  }
}

Code only for Client Machine:

client.java

import java.rmi.*;
import java.io.*;

public class Client {
  public static void main(String args[]) {
  try {
  String ip = "rmi://192.168.10.97/SERVICE";
  String ip2 = "rmi://192.168.10.113/SERVICE2";

  RemoteInterface s = (RemoteInterface) Naming.lookup(ip);
  RemoteInterface2 s2 = (RemoteInterface2) Naming.lookup(ip2);

  System.out.println("Add:" + s.add(14));
  System.out.println("sub:" + s2.sub(31));
  catch (Exception e) {
  System.out.println(e.getMessage());
  e.printStackTrace();
  }
  }
}

Steps for execution at Server machine:

javac RemoteInterface.java

javac RemoteInterface1.java

javac ServerImplements.java

javac ServerImplements2.java

rmic ServerImplements

rmic ServerImplements2

javac Servet2.java

start rmiregistry

java server2

Steps for execution at Client machine:

javac RemoteInterface.java

javac RemoteInterface1.java

javac ServerImplements.java

javac ServerImplements2.java

rmic ServerImplements

rmic ServerImplements2

javac Server.java 0

start rmiregistry

start java server

javac Client.java 1

java Client

Output will be shown at client machine

Add:5 2

Sub:2

 

      3