1. Create two directories from a DOS or Unix prompt. Name one directory 'RMILabServer' and the other directory 'RMILabClient'.
2. Within the server directory, save the following three classes.
//************************************************************
// Calculator.java Interface for a Calculator import java.rmi.*;
public interface Calculator extends Remote {
// this method will be called from remote clients int add (int x, int y) throws RemoteException;
}
//*************************************************************
// CalculatorServant.java
// A Remote object class that implements Calculator.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class CalculatorServant extends UnicastRemoteObject implements Calculator {
public CalculatorServant() throws RemoteException { }
public int add(int x, int y) throws RemoteException {
System.out.println("Got request to add " + x + " and " + y);
return x + y; } }
//**************************************************************
// CalculatorServer.java Serve remote Calculator
// Creates a calculator object and gives
// it a name in the registry.
import java.rmi.*;
public class CalculatorServer {
public static void main(String args[]){
System.out.println("Calculator Server Running");
try{
// create the servant
Calculator c = new CalculatorServant();
System.out.println("Created Calculator object");
System.out.println("Placing in registry");
// publish to registry
Naming.rebind("CoolCalculator", c);
System.out.println("CalculatorServant object ready");
}catch(Exception e) {
System.out.println("CalculatorServer error main " + e.getMessage());
} } }
Compile all the code with the command "javac *.*". Run RMIC on the servant class with the command "rmic -v1.2 CalculatorServant". Copy the interface Calculator.java and the stub class to the client. The stub will be called CalculatorServant_Stub.class. Within the server directory, start the rmiregistry with the command "rmiregistry &" (Unix) or "start rmiregistry (DOS)". Run the server with the command "java CalculatorServer".
3. In the client directory, enter the following program:
//*******************************************************
// CalculatorClient.java
// This client gets a remote reference from the rmiregistry
// that is listening on the default port of 1099.
// It allows the client to quit with a "!".
// Otherwise, it computes the sum of two integers
// using the remote calculator.
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import java.util.StringTokenizer;
public class CalculatorClient {
public static void main(String args[]) throws Exception {
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
// connect to the rmiregistry and get a remote reference to the Calculator
// object.
Calculator c = (Calculator) Naming.lookup("//localhost/CoolCalculator");
System.out.println("Found calculator. Enter ! to quit");
while(true) { try {
// prompt the user
System.out.print("client>");
// get a line
String line = in.readLine();
// if a "!" is entered just exit
if(line.equals("!")) System.exit(0);
// if it's not a return get the two integers and call add
// on the remote calculator.
if(!line.equals("")) {
StringTokenizer st = new StringTokenizer(line);
String v1 = st.nextToken();
String v2 = st.nextToken();
int i = Integer.parseInt(v1);
int j = Integer.parseInt(v2);
int sum = c.add(i,j);
System.out.println(sum);
} } catch(RemoteException e)
{
System.out.println("allComments: " + e.getMessage());
} } } }
Compile the code on the client and run with the command "java CalculatorClient". Study what you have and then begin the main exersise of this lab. ===============================================================================
To Make two directories named as
RMILabServer,RMILabClient
-----------------------------------------------------------------
mkdir -p /home/user/{RMILabServer,RMILabClient}
---------------------
Calculator.java
---------------------
import java.rmi.*;
public interface Calculator extends Remote {
public int add(int a,int b) throws RemoteException;
public int sub(int a,int b) throws RemoteException;
public int mul(int a,int b) throws RemoteException;
public int div(int a,int b) throws RemoteException;
}
-------------------------------
CalculatorServant.java
-------------------------------
import java.rmi.*;
import java.rmi.server.*;
public class CalculatorServant extends UnicastRemoteObject
implements Calculator{
public CalculatorServant() throws RemoteException{
super();
}
public int sub(int a,int b)
{
int sub;
sub=a-b;
return sub;
}
public int mul(int a,int b)
{
int m;
m=a*b;
return m;
}
public int div(int a,int b)
{
int d;
d=a/b;
return d;
}
public int add(int a,int b)
{
int add;
add=a+b;
return add;
}
}
-----------------------------
CalculatorServer.java
-----------------------------
import java.rmi.*;
public class CalculatorServer{
public CalculatorServer() {}
public static void main(String args[]) throws Exception{
CalculatorServant robj=new CalculatorServant();
Naming.rebind("rmi://localhost:1099/CalculatorService",robj);
}
}
----------------------------
CalculatorClient.java
----------------------------
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.registry.*;
import java.util.Scanner;
import java.rmi.*;
public class CalculatorClient{
public static void main(String[] args) throws Exception{
Calculator
stub=(Calculator)Naming.lookup("rmi://localhost:1099/CalculatorService");
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st Number : ");
int a=sc.nextInt();
System.out.println("Enter 2nd Number : ");
int b=sc.nextInt();
int a=stub.add(a,b);
int s=stub.sub(a,b);
int mu=stub.mul(a,b);
int di=stub.div(a,b);
System.out.println("Addition : "+a);
System.out.println("Subtraction : "+s);
System.out.println("Multiplication : "+mu);
System.out.println("Divition : "+di);
}
}
Note:Could you please consider my effort on this work and give up vote. Thank you :)
1. Create two directories from a DOS or Unix prompt. Name one directory 'RMILabServer' and the...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
What is the problem with my Program ? also I need a Jframe that desplays the original input on the left side and the sorted input of the left side. my program is supose to read the number for basketball players, first name, last name, and float number that is less than 0 . on the left sideit is supposed to sort all this input based on last name. This is my demo class : package baseball; import java.io.*; import...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...
This is a java homework for my java class. Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number. The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is...
Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...
Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....
1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you have questions on file path. Copy SecretMessage.java into your NetBeans or other IDE tools. 2. Finish the main method that will read the file secret.txt, separate it into word tokens.You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to StringBuffer object to...
Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...
I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList { private ShoppingItem [] list; private int amtItems = 0; public ShoppingList() { list=new ShoppingItem[8]; } public void add(ShoppingItem item) { if(amtItems<8) { list[amtItems] = ShoppingItem(item);...