Skip to content

Instantly share code, notes, and snippets.

@ats1999
Last active February 29, 2020 09:54
Show Gist options
  • Save ats1999/7afa7352b9106cb8a6a63597dc98a288 to your computer and use it in GitHub Desktop.
Save ats1999/7afa7352b9106cb8a6a63597dc98a288 to your computer and use it in GitHub Desktop.
Web Application design and development (Rahul kumar)
package rmi;
/**
* This file contains class which have implemention of AddI interface
*/
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* This class defines add method, which is called by the client.
* @version 1.0
* @author Rahul kumar
*
*/
public class AddC extends UnicastRemoteObject implements AddI{
/**
* This is constructor. This constructor will call parent class
* constructor indirectly.
* @throws RemoteException
*/
protected AddC() throws RemoteException {
super();
}
/**
* This method will return sum of both arguments.
* @param x first number
* @param y second number
* @return x+y result
* @throws Exception
*/
@Override
public int add(int x, int y) throws Exception {
return x+y;
}
}
package rmi;
/**
* This file contains interface AddI.
*/
import java.rmi.Remote;
/**
* This interface contains method AddI.
* @version 1.0
* @author Rahul kumar
*
*/
public interface AddI extends Remote{
/**
* This method accepts two integer arguments.
* This method return an integer number.
* @param x first number.
* @param y second number
* @return number( result)
* @throws Exception
*/
public int add(int x,int y) throws Exception;
}
package rmi;
/**
* This package contains client class.
*/
import java.rmi.Naming;
/**
* This class represent Client.
* Thsi class will call method add, from the server.
* @class Client
* @author Rahul kumar
*
*/
public class Client {
/**
* This is main method.
* @param args unused
* @throws Exception
*/
public static void main(String[] args) throws Exception{
//search for the method add by looking the reference "ADD"
AddI obj=(AddI)Naming.lookup("ADD");
// call the method
int sum=obj.add(5, 5);
// print the result
System.out.println("Sum is : "+sum);
}
}
package rmi;
/**
* @author Rahul kumar
* This has server class which represent remote server.
*/
import java.rmi.Naming;
/**
* @class Server
* @version 1.0
* This class will provide service to the client.
*/
public class Server {
/**
* This method will serve client.
* @param args unused
* @throws Exception
*/
public static void main(String[] args) throws Exception{
// create object of AddC
AddC obj=new AddC();
// bind this object with "ADD", this object will be referenced by "ADD"
Naming.rebind("ADD", obj);
// print message ...
System.out.println("SERVER.........STARTED");
}
}
    • Open a terminal.

    • Go to the path where java files are stored.
    • Compile all java files => javac *.java
    • Create Stub and Skelton => rmic AddC
    • Start server => java Server
    • Open a another terminal.

    • Start Client => java Client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment