Following file are needed to put on Client
Conversion.java
ConversionClient.java
Data.java
On server:
Conversion.java
ConversionImpl.java
ConversionServer.java
Data.java
Conversion.java
public interface Conversion extends java.rmi.Remote
{
public Data conversion1(Data km) throws java.rmi.RemoteException;
public Data conversion2(Data km) throws java.rmi.RemoteException;
}
Data.java
import java.io.*;
public class Data implements Serializable {
private static final long serialVersionUID = 1L;
private float km;
private int miles;
private long foot;
public Data() {
}
public float getKm() {
return km;
}
public void setKm(float km) {
this.km = km;
}
public int getMiles() {
return miles;
}
public void setMiles(int miles) {
this.miles =miles ;
}
public long getFoot() {
return foot;
}
public void setFoot(long foot) {
this.foot = foot;
}
}
ConversionClient.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.io.*;
import java.util.*;
import java.net.*;
public class ConversionClient
{
public static void main(String[] args) throws Exception
{
// Converting the hostname to ip address
InetAddress ia=InetAddress.getByName(args[0]);
// Looking up for the remote object in remote server
Conversion c=(Conversion) Naming.lookup("rmi://"+ia.getHostAddress()+":3000/ConversionServer");
// Enter kilometers
System.out.println("Enter Kilometers\n");
Scanner ob=new Scanner(System.in);
float km=ob.nextFloat();
System.out.println("val is "+km);
Data d=new Data();
d.setKm(km);
//passing values to two methods
int s=c.conversion1(d).getMiles();
long t=c.conversion2(d).getFoot();
// printing out the values
System.out.println(km+"Kilometers ="+s+"Miles ");
System.out.println(km+"Kilometers ="+t+"Foot ");
}
}
ConversionImpl.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class ConversionImpl extends java.rmi.server.UnicastRemoteObject implements Conversion
{
private static final long serialVersionUID = 1L;
public ConversionImpl() throws java.rmi.RemoteException
{
super();
}
// converts from Km to Miles
public synchronized Data conversion1(Data value) throws java.rmi.RemoteException
{
int r=(int)Math.round(value.getKm() * 0.62137119);
//System.out.println("r="+r);
value.setMiles(r);
return value;
}
// converts from Km to Foot
public synchronized Data conversion2(Data value) throws java.rmi.RemoteException
{
long r=(long)Math.round(value.getKm() * 3280.8399);
//System.out.println("r="+r);
value.setFoot(r);
return value;
}
}
ConversionServer.java
// This program is used to put the object referrence in rmiregistry
import java.rmi.*;
import java.net.*;
public class ConversionServer
{
public static void main(String args[])throws Exception
{
Conversion c=new ConversionImpl();
InetAddress ia=InetAddress.getLocalHost();
String h=ia.getHostName();
Naming.rebind("rmi://"+ia.getHostAddress()+":3000/ConversionServer",c); // putting the object referrence in rmi registry
}
}
To Compile:
1.a. Copy the folder "client" to the machine where you want to run client.
b. Go to the folder "client" and run the command
% javac *.java
2.a. Copy the folder "server" to the machine where you want to run the server.
b. Go to the folder "server" and run the command
% javac *.java
To Run the program:
1.On server machine
a. Open a window to start rmiregistry
% rmiregistry 3000
b. Go to the folder "server"
c. Start the Server on another window
java ConversionServer
2.a Go to the folder "client" in client machine
b. Start the client using the command
java ConversionClient
No comments:
Post a Comment