programming-examples/java/Client Server Using RMI.java
2019-11-18 13:43:20 +01:00

60 lines
1.4 KiB
Java

//AddInterface.java
import java.rmi.*;
public interface AddInterface extends Remote
{
public int sum(int n1, int n2) throws RemoteException
}
//Add.java
import java.rmi.*;
import java.rmi.server.*;
public class Add extends UnicastRemoteObject implements AddInterface
{
int num1, num2 ;
public Add() throws RemoteException
{
}
public int sum(int n1, int n2) throws RemoteException
{
num1=n1;
num2=n2;
return n1+n2;
}
}
//AddServer.java
import java.rmi.Naming;
public class AddServer
{
public static void main(String args[])
{
try
{
Naming.rebind("Add",new Add());
System.out.println("Server is connected and waiting for client");
}
catch(Exception e)
{
System.out.println("server could not connect:"+e);
}
}
}
//AddClient.java
import java.rmi.Naming;
public class AddClient
{
public static void main(String args[])
{
try
{
AddInterface ai=(AddInterface)Naming.lookup("//localhost/Add");
System.out.println("The sum of two nos. is :"+ai.sum(10,2));
}
catch(Exception e)
{
System.out.println("Client Exception:"+e);
}
}
}