JavaRMI Report
JavaRMI Report
{
public EchoImpl() throws RemoteException {
super();
System.out.println("constructor");
}
public String echo(String msg) throws java.rmi.RemoteException
{
System.out.println("Received n echo");
return msg;
}
public float get_price( String symbol )
{
float price = 0;
for( int i = 0; i < symbol.length(); i++ )
{
price += (int) symbol.charAt( i );
}
price /= 5;
return price;
}
public double input ( double unit, double rate){
double price=0;
price=unit*rate;
return price;
}
}
File for defining Client
//EchoClient.java
import java.rmi.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class EchoClient {
public static final int PORT=8080;
public static void main(String[] args)
{
String[ ] uid1 = { "S1", "S2", "B1", "B2"};
String[ ] p = {"HUTCH", "Infosys","Hutch","Infosys"};
double [ ] unit = {5,6,5,6};
double [ ] noshares = {10,4,7,3};
double [ ] total = {0,0,0,0};
int port=0;
int ch;
try {
port=Integer.parseInt(args[0]);
String messg="Hello";
System.setProperty("java.security.policy", "all_policy.policy");
System.setSecurityManager(new RMISecurityManager());
String URL="rmi://localhost:"+port+"/EchoServer";
EchoServer server=(EchoServer) Naming.lookup(URL);
System.out.println(server.echo(messg));
for (int i=0; i<4; i++)
total[i] = server.input(unit[i], noshares[i]);
System.out.println("STOCK MARKET EXCHANGE:");
System.out.println("----------------------");
System.out.println("STATUS OF NSE:");
System.out.println("------------");
System.out.println("Uid Company name \t unit\t
No.of.Shares \t total:");
for (int i=0; i<4; i++)
{
System.out.println("\t"+uid1[i]+"\t"+p[i]+"\t"+unit[i]+"\t\t"+noshares[i]+
"\t\t"+total[i]);
}
BufferedReader bufferedReader = new
BufferedReader(new
InputStreamReader(System.in));
String choice = "y";
do
{
if(p [i].equals(str2))
{
noshares[i] = noshares[i]
- doub1;
total[i] = unit[i] *
noshares[i];
}
}
}
break;
case 2:
System.out.println("Enter Seller ID:");
String str4 = bufferedReader.readLine();
System.out.println("Enter Company Name:");
String str5 = bufferedReader.readLine();
System.out.println("No of Shares u want to buy:");
double doub2 =
Double.parseDouble(bufferedReader.readLine());
System.out.println("Enter the Seller id:");
String str6 = bufferedReader.readLine();
for (int i=0; i<4; i++)
{
if (uid1 [i].equals(str4))
{
if(p [i].equals(str5))
{
noshares[i] = noshares[i] doub2;
total[i] = unit[i] * noshares[i];
}
}
}
for (int i=0; i<4; i++)
{
if (uid1 [i].equals(str6))
{
if(p [i].equals(str5))
{
noshares[i] = noshares[i] +
doub2;
total[i] = unit[i] * noshares[i];
}
}
}
break;
case 3:
System.out.println("\t\t Status of NSE:");
System.out.println("\t uid \t Company name \t\t
unit \tNo.of.Share\t total:");
for (int i=0; i<4; i++)
{
System.out.println("\t"+uid1[i]+"\t"+p[i]+"\t"+unit[i]+"\t\t"
+noshares[i]+"\t\t"+total[i]);
}
break;
}
System.out.println("Do you want to continue Y/N:");
choice = bufferedReader.readLine();
}while(choice.equals("y"));
System.out.println( "The price of MY COMPANY is " +
server.get_price("MY_COMPANY") );
}
catch(Exception e)
{ e.printStackTrace();
}}}
File for setting up server
//EchoServerSetup.java
import java.rmi.*;
import java.rmi.registry.*;
javac *.java
5.)Start the rmi registry by giving the command
start rmiregistry
6.) the EchoImpl.class needs to be compiled by rmic
rmic EchoImpl
7.)Now run the server by the command:
start java EchoServerSetup
8.)
Open new command prompt window.Set the current directory to Echo and
run the client.
java EchoClient
and the output will be displayed as shown in snapshots..
Snapshots..
1.) Compiling java files and starting rmiregistry
Rmiregistry window..
2.) Compile EchoImpl through rmic and run server EchoServerSetup
3.)Server.exe
grant
{
Permission java.security.AllPermission;
}