Record Programs-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

RPC USING JAVA

Filename: rint.java

import java.io.*;

import java.rmi.*;

public interface rint extends Remote

public int add(int a,int b) throws RemoteException;

public int sub(int x,int y) throws RemoteException;

public int mul(int p,int q) throws RemoteException;

File Name: rclass.java

Import java.io.*;

import java.rmi.*;

import java.net.*;

import java.rmi.server.*;

public class rclass extends UnicastRemoteObject implements rint

public rclass() throws RemoteException

public int add(int a,int b)throws RemoteException

int c=a+b;

return c;

public int sub(intx,inty) throws RemoteException

int d=x-y;

return d;
}

public int mul (int p,int q) throws RemoteException

int e=p*q;

return e;

File Name: sclass.java

import java.io.*;

import java.rmi.*;

import java.net.*;

public class sclass

public static void main(String args[ ])

try

rclass r=new rclass();

Naming.rebind("sum",r);

catch(Exception e){}

}
Filename: cclass.java

import java.io.*;

import java.rmi.*;

public class cclass

public static void main(String args[])

try

int b,b1;

Strings;

s="rmi://localhost/sum";

rint x=(rint)Naming.lookup(s);

b=Integer.parseInt(args[0]);

b1=Integer.parseInt(args[1]);

int z=x.add(b,b1);

int z1=x.sub(b,b1);

int z2=x.mul(b,b1);

System.out.println("Sum"+z);

System.out.println("Difference"+z1);

System.out.println("Product"+z2);

catch(Exception e){ }

}
OUTPUT :

Sum 4

Difference 0

Production 4
To study Client server based program using RMI.

RMI stands for Remote Method Invocation

The coding steps:

1. Code the Remote Interface

2. Implement the Remote Interface while extending UnicastRemoteObject

3. Code the driver to register an object in the rmiregistry

4. Code the client who will look up the remote object in the registry

The first step is to create the Remote interface.

import java.rmi.*;

public interface rmi Interface extends Remote

double add (double d1, double d2) throws Exception;

The rules for the Remote interface are as follows:

1. The interface must extend java.rmi.Remote

2. The method signatures for remote methods need to be defined to throw a RemoteException

The second step is to implement the interface.

import java.rmi.*;

import java.rmi.server.*;

public class rmi Impl extends UnicastRemoteObject

implements rmi Interface

public rmiImpl () throws RemoteException

public double add (double d1, double d2) throws Exception

{
return d1+d2;

Step 3, is to code a driver program, which will create an instance of the class, and register it with
the rmiregistry

public static void main (String args [])

try

rmiImpl obj=new rmiImpl ();

Naming. rebind ("///rmi", rmiobj);

catch (Exception e)

System.out.println ("An error occurred trying to “+

“bind the object to the registry.");

The fourth step is to implement the client.

import java.rmi.*;

import java.io.*;

public class ConfusedClient

public static void main (String args [])

BufferedReader reader;

try

String url=”rmi: //”+args [0] +”/rmi”;

rmiInterface rmiinter= (rmiInterface) Naming. lookup(url));

System.out.println (“\n The first no is”+args[1]);


System.out.println (“\n The second no is”+args[2]);

double d1, d2;

d1=Double.valueOf (args [1]).doubleValue ();

d2=Double.valueOf (args [2].doubleValue () ;

System.out.println (“\n The sum of two values”+rmiinter.add (d1, d2));

catch (Exception e)

System.out.println ("An error occurred.");

}
OUTPUT :

The compiling & running steps:

1. Compile all of the .java files

2. Run rmic on the implementation of the Remote interface

3. Open a second terminal window

4. Type “start rmiregistry” in one of the windows

5. Register the remote object in the registry by running the driver

6. Run the client

7. Play
To study Implementation of Election algorithm.

import java.io.*;

import java.util.Scanner;

class Anele

static int n;

static int pro[] = new int[100];

static int sta[] = new int[100];

static int co;

public static void main(String args[])throws IOException

System.out.println("Enter the number of process");

Scanner in = new Scanner(System.in);

n = in.nextInt();

int i,j,k,l,m;

for(i=0;i<n;i++)

System.out.println("For process "+(i+1)+":");

System.out.println("Status:");

sta[i]=in.nextInt();

System.out.println("Priority");

pro[i] = in.nextInt();

System.out.println("Which process will initiate election?");

int ele = in.nextInt();

elect(ele);

System.out.println("Final coordinator is "+co);

}
static void elect(int ele)

ele = ele-1;

co = ele+1;

for(int i=0;i<n;i++)

if(pro[ele]<pro[i])

System.out.println("Election message is sent from "+(ele+1)+" to "+(i+1));

if(sta[i]==1)

elect(i+1);

}
OUTPUT :

Enter the number of process

For process 1:

● Status:

Priority

For process 2:

Status:

Priority

For process 3:

Status:

Priority

For process 4:

Status:

Priority

For process 5:

Status:

Priority

For process 6:
Status:

Priority

For process 7:

Status:

Priority

Which process will initiate election?

Election message is sent from 4 to 5

Election message is sent from 5 to 6

Election message is sent from 6 to 7

Election message is sent from 5 to 7

Election message is sent from 4 to 6

Election message is sent from 6 to 7

Election message is sent from 4 to 7

Final coordinator is 6
To study Implementation of Mutual Exclusion algorithms.

import java.util.Random;

class Util

public static void pause(int low, int high) {

Random r = new Random();

int R = r.nextInt(high-low) +low;

try

Thread.sleep(R);

} catch (InterruptedException e) {

e.printStackTrace();

public static int random(int low, int high) {

Random r = new Random();

int R = r.nextInt(high-low) + low;

return R;

class Printer extends Thread {

static Object mutex = new Object();

public void run() {

for(;;)

synchronized (mutex) {

System.out.print("-");

Util.pause(100, 300);
System.out.print("/");

Util.pause(0, 200);

class MutualExclusion

public static void main(String[] args)

new Printer().start();

new Printer().start();

}
OUTPUT :

\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\-\-\-\-\
5. To write program multi-threaded client/server processes.

A simple Client/Server application


The client

This is a simple client which reads a line from the standard input and sends it to the
echo server. The client keeps then reading from the socket till it receives the
message "Ok" from the server. Once it receives the "Ok" message then it breaks.
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {


public static void main(String[] args) {

Socket clientSocket = null;


DataInputStream is = null;
PrintStream os = null;
DataInputStream inputLine = null;

try {
clientSocket = new Socket("localhost", 2222);
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
inputLine = new DataInputStream(new
BufferedInputStream(System.in));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to
host");
}

if (clientSocket != null && os != null && is != null) {


try {

System.out.println("The client started. Type any text. To quit


it type 'Ok'.");
String responseLine;
os.println(inputLine.readLine());
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
os.println(inputLine.readLine());
}

os.close();
is.close();
clientSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}

The server
This is a simple echo server. The server is dedicated to echo messages received from
clients. When it receives a message it sends the message back to the client. Also, it
appends the string "From server :" in from of the echoed message.
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;

public class Server {


public static void main(String args[])
{

ServerSocket echoServer = null;


String line;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;

try {
echoServer = new
ServerSocket(2222);
} catch (IOException e) {
System.out.println(e);
}
System.out.println("The server started. To stop it press <CTRL><C>.");
try {
clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());

while (true) {
line = is.readLine();
os.println("From server: " + line);
}
} catch (IOException e) {
System.out.println(e);
}
}
}

Save these programs on your computer.

Name the files Client.java and Server.java.

Open a shell window on your computer and change the current directory to the
directory where you saved these files.

Type the following two commands in the shell window.

javac
Server.java
javac
Client.java
If java compiler is installed on your computer and the PATH variable is configured for
the shell to find javac compiler, then these two command lines will create two new
files in the current directory : the files Server.class and Client.class

Start the server in the shell window using the command:

java Server

You will see the following message in this window

The server started. To stop it press <CTRL><C>.

The server started. To stop it press


<CTRL><C>.

telling you that the server is started.

Open a new shell window and change the 0current directory to the directory where
you saved the application files.

Start the client in the shell window using the command:

java
Client

You will see the following message in this window

The client started. Type any text. To quit it type


'Ok'.

telling you that the client is started.


Type, for example, the text Hello in this window. You will see the following output.
hello
From server: hello

telling you that the message Hello was sent to the server and the echo was received by
the client from the server.

You might also like