Beruflich Dokumente
Kultur Dokumente
INDEX
Title: Distributed Systems
Branch: IT
S.no.
Content
Page No.
Signature
Program 1
Aim: Write a program for client server.
Client.java
import java.io.*;
import java.net.*;
public class Client{
Socket s;
//ServerSocket ss;
DataInputStream dis;
DataOutputStream dos;
public Client(){
try{
s=new Socket("192.168.0.1",1000);
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
ClientChat();
}catch(IOException e){}
}
public static void main(string[] args) {
new client();
}
public void ClientChat() throws IOException {
String s1,s2;
do{
InputStreamReader isr=new InputStreamReader(System.in);
BufferReader br=new BufferReader(isr);
s1=br.readLine();
dos.writeUTF(s1);
dos.flush();
s2=dis.readUTF();
System.out.println(s2);
}while(!s.equals("stop"));
}
}
Server.java
import java.io.*;
import java.net.*;
public class Server {
ServerSocket ss;
Socket s;
DataInputStream dis;
DataOutputStream dos;
Server()
{try{
ss=new ServerSocket(1000);
s=ss.accept();
System.out.println("Client Connected");
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
ServerChat();
}catch(Exception e){}
}
public static void main(String[] args){
new Server();
}
public void ServerChat() throws IOException
{
String str;
do{
str=dis.readUTF();
System.out.println(Client message:"+str);
dos.writeUTF("Hello"+str);
dos.flush();
}while(!str.equals("stops"));
}
}
Program 2
Aim: Write a program for Vector Clock.
Client1.java
package vectorclock;
import java.net.*;
import java.io.*;
import java.util.*;
public class client1{
static int[] v= new int[3];
int msg;
int add;
public client1(int a){
msg=a;
}
public client1(int a, int s){
msg=a;
add=s;
}
public void run(){
try{
if(msg==0){
v[0]++;
System.out.println("My system clock");
for(int i=0;i<3;i++)
System.out.print(v[i]+',');
}
else if(msg==1){
v[0]++;
Socket s= new Socket("127.0.0.1",add);
OutputStream outStream= s.getOutputStream();
PrintWriter out= new PrintWriter(outstream,true);
for(int i=0;i<3;i++)
out.print(v[i]);
System.out.println("My system clock");
for(int i=0;i<3;i++)
System.out.print(v[i]+',');
s.close();
Client2.java
package vectorclock;
import java.net.*;
import java.util.*;
public class Client2 implements Runnable
{
static int[] v = new int[3];
int msg;
int add;
public Client2(int a)
{
msg = a;
}
public Client2(int a, int s)
{
msg = a;
add = s;
}
public void run()
{
try
VectorClock.java
package vectorclock;
import java.net.*;
import java.io.*;
import java.util.*;
public class vectorclock{
public static void main(String args[]){
System.out.println(" There are three processes \n Following is an event scenario and the
individual vector clocks ");
System.out.println("e11");
client1 c1=new client1(0);
c1.run();
System.out.println("e21");
Runnable r1=new client2(0);
Thread t1=new Thread(r1);
t1.start();
Thread.sleep(1000);
System.out.println("e12");
Runnable r2= new client1(1,1002);
Thread t2=new Thread(r2);
t2.start();
Thread.sleep(1000);
Sustem.out.println("e31");
Runnable r3=new client3(0);
Thread t3=new Thread(r3);
t3.start();
}
}
Program 3
Aim: Write a program to implement RMI protocol.
Client.java
impot my.*;
import java.rmi.*;
import my.MyRemote;
public class Client{
try{
my.MyRemote
m = (my.MyRemote)Naming.lookup("rmi://localhost:1099/ducat");
System.out.println(m.add(10,15));
}
catch(Exception e){System.out.println(e);
}
}
MyRemote.java
package my;
import java.rmi.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyRemote extends Remote
{
public int add(int x, int y)throws RemoteException;
}
MyServer.java
package my;
import my.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MyServer implements MyRemote extends UnicastRemoteObject{
public MyServer () throws RemoteException{}
public int add(int x, int y)throws RemoteException { return(x+y);}
}
Registry.java
package my;
import java.rmi.*;
Program 4
Aim: Write a program where client will send an arithmetic expression to RMI
server1. RMI server1 will connect to RMI server2 to get the result and then server1
will send the output to the client.
Calculator.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote{
public double add(double a,double b)
throws RemoteException;
public double sub(double a,double b)
throws RemoteException;
public double mul(double a,double b)
throws RemoteException;
public double div(double a,double b)
throws RemoteException;
}
CalculatorClient.java
import java.io.*;
import java.util.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient{
public static void main(String[] args)
{
String serverName;
String inp;
double n1=0.0,n2=0.0;
if(args.length==0)
serverName="127.0.0.1";
CalculatorImpl.java
import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl extends UnicastRemoteObject
implements Calculator{
public CalculotorImpl()
throws java.rmi.RemoteException{
super();
}
public double add(double a,double b)
throws java.rmi.RemoteException{
System.out.println("Adding"+a+"and"+b);
return a+b;
}
public double sub(double a,double b)
throws java.rmi.RemoteException{
System.out.println("Subtracting"+b+"from"+a);
return a-b;
}
public double mul(double a,double b)
throws java.rmi.RemoteException{
CalculatorServer.java
import java.rmi.Naming;
public class CalculatorServer{
public CalculatorServer()
{
try{
Calculator c=new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService",c);
}catch(Exception e){
System.out.println("Trouble:"+e);
}
}
public static void main(String args[])
{
new CalculatorServer();
}
}
Program 5
Aim: Write a program where the client displays the time information from the
server in real-time. The data needs to be refreshed (suppose for every millisecond).
IntTime.java
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
*
* @author Student
*/
public interface IntTime extends Remote {
public String get()throws RemoteException;
}
IntTimeImpl.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
/**
*
* @author Student
*/
public class IntTimeImpl extends UnicastRemoteObject implements IntTime{
public IntTimeImpl()throws RemoteException{
super();
}
public String get() throws RemoteException {
Date date=new Date();
String str=date.toString();
return str;
}
}
TimeClient.java
import java.rmi.Naming;
/**
*
TimeServer.java
import java.rmi.Naming;
/**
*
* @author Student
*/
public class TimeServer {
public TimeServer(){
try {
System.out.print("Server Started");
IntTime i=new IntTimeImpl();
Naming.rebind("rmi://localhost:1099/TimeService", i);
} catch (Exception e) {
System.out.print(e);
}
}
public static void main(String args[]){
new TimeServer();
}
}
Program 6
Aim: Write a program where server displays the IP address, system date and time
of the client m/c on connection to its console.
ReceiveMessageInterface.java
import java.rmi.*;
public interface ReceiveMessageInterface extends Re4mote
{
void receiveMessage(String x) throws RemoteException;
}
RmiClient.java
import java.rmi.*;
import java.rmui.registry.*;
import java.net.*;
public class RmiClient
{
static public void main(String args[])
{
ReceiveMessageInteface rmiServer;
Registry registry;
String serverAdddress = args[0];
String serverPort = args[0];
String text = args[2];
System.out.println("Sending....."+text+"to"+serverAddress+":"+serverPort);
try
{
//get the "registry"
registry = LocateTregistry.getRegistry(serverAddress,(new
Integer(serverPort)).intValue());
//look up the remote object
rmiServer = (ReceiveMessageInterface)(registry.lookup("rmiServer"));
//call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e)
{
e.printStackTrace();
RmiServer.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; //rmi registry for lookup the remote objects
//This method is called from the remote client by the RMI
//This is the implementation of the "ReceiveMessageInterface"
public void receiveMessage(String x)throws RemoteException
{
System.out.println(x);
}
public RmiServer() throws RemoteException
{
try{
// get the address of this host
this address=(InetAddress.grtLocalHost()).toString();
}
catch(Exception e){
throw new RemoteException(can't get inet address.");
}
thisPort=3232; //this port(registry's port)
System.out.println("thisaddress="+thisAddress+",port="+thisPort);
try{
// create the registry and bind the name and object.
registry=LocateRegistry.createRegistry(this Port);
registry.rebind("rmiServer",this);
}
catch(RemoteException e){
throw e;
Program 7
Aim: Write a program to send a message from server to client in the encrypted
form & the client will display the same message in plain-text format on its console.
Hello.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author Student
*/
import java.rmi.*;
import java.rmi.server.*;
public class Hello extends UnicastRemoteObject implements HelloInterface
{
private String message;
public Hello(String msg)throws RemoteException
{
message=msg;
}
public String say() throws RemoteException
return message;
public String code()throws RemoteException
{
int len=message.length();
char[] tempCharArray=new char[len];
char[] charArray=new char[len];
for(int i=0;i<len;i++)
{
tempCharArray[i]=message.charAt(i);
}
for(int j=0;j<len;j++)
{
charArray[j]=tempCharArray[len 1 j];
}
String reverseMessage=new String(charArray);
return reverseMessage;
}
}
HelloClient.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author Student
*/
import java.rmi.Naming;
import java.rmi.*;
public class HelloClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
HelloInterface hello=(HelloInterface)Naming.lookup("//127.0.0.1/Hello");
System.out.println(hello.say());
System.out.println(hello.code());
}
catch(Exception e){
System.out.println("HelloClient extends :"+e);
}
}
}
HelloInterface.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
HelloServer.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.rmi.Naming;
import java.rmi.*;
public class HelloServer {
public static void main(String args[])
{
String msg1;
try
{
msg1=args[0];
Naming.rebind("Hello", new Hello(msg1));
System.out.println("Server is connected");
}
catch(Exception e)
{
System.out.println("Server not connected:"+e);
}
}
}