Record Programs-1
Record Programs-1
Record Programs-1
Filename: rint.java
import java.io.*;
import java.rmi.*;
Import java.io.*;
import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
int c=a+b;
return c;
int d=x-y;
return d;
}
int e=p*q;
return e;
import java.io.*;
import java.rmi.*;
import java.net.*;
try
Naming.rebind("sum",r);
catch(Exception e){}
}
Filename: cclass.java
import java.io.*;
import java.rmi.*;
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.
4. Code the client who will look up the remote object in the registry
import java.rmi.*;
2. The method signatures for remote methods need to be defined to throw a RemoteException
import java.rmi.*;
import java.rmi.server.*;
{
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
try
catch (Exception e)
import java.rmi.*;
import java.io.*;
BufferedReader reader;
try
catch (Exception e)
}
OUTPUT :
7. Play
To study Implementation of Election algorithm.
import java.io.*;
import java.util.Scanner;
class Anele
static int n;
n = in.nextInt();
int i,j,k,l,m;
for(i=0;i<n;i++)
System.out.println("Status:");
sta[i]=in.nextInt();
System.out.println("Priority");
pro[i] = in.nextInt();
elect(ele);
}
static void elect(int ele)
ele = ele-1;
co = ele+1;
for(int i=0;i<n;i++)
if(pro[ele]<pro[i])
if(sta[i]==1)
elect(i+1);
}
OUTPUT :
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
Final coordinator is 6
To study Implementation of Mutual Exclusion algorithms.
import java.util.Random;
class Util
try
Thread.sleep(R);
} catch (InterruptedException e) {
e.printStackTrace();
return R;
for(;;)
synchronized (mutex) {
System.out.print("-");
Util.pause(100, 300);
System.out.print("/");
Util.pause(0, 200);
class MutualExclusion
new Printer().start();
new Printer().start();
}
OUTPUT :
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\-\-\-\-\
5. To write program multi-threaded client/server processes.
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;
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");
}
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;
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);
}
}
}
Open a shell window on your computer and change the current directory to the
directory where you saved these files.
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
java Server
Open a new shell window and change the 0current directory to the directory where
you saved the application files.
java
Client
telling you that the message Hello was sent to the server and the echo was received by
the client from the server.