Sie sind auf Seite 1von 21

DEPARTMENT OF INFORMATION TECHNOLOGY

INDEX
Title: Distributed Systems
Branch: IT
S.no.

Content

Roll No.:Semester: 8th


Date

Page No.

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

Signature

DEPARTMENT OF INFORMATION TECHNOLOGY

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"));
}
}

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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"));
}
}

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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();

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


}
else if (msg==2)
{
v[o]++;
ServerSocket s= new ServerSocket(1001);
Socket incoming=s.accept();
InputStream inStream=incoming.getInputStream();
Scanner in = new Scanner(inStream);
for(int i=0;i<3;i++)
v[i]=Math.max(v[i],in.nextInt());
System.out.println("My system clock");
for(int i=0;i<3;i++)
System.out.print(v[i]+',');
s.close();
}
}
catch(IOException e){}
}
}

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

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


{
if(msg==0)
{
v[1]++;
System.out.println("My Vector Clock");
for(int i=0;i<3;i++)
{
System.out.print(v[i]+',');
}
}
else
{
if(msg==1)
{
v[1]++;
Socket s = new Socket("127.0.0.1",add);
OutputStream outStream = s.getOutPutStream();
PrintWriter out = new PrintWiiter(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.outprint(v[i]+',');
}
s.close();
}
else
{
if(msg==2)
{
v[1]++;
ServerSocket s = new ServerSocket(1002);
Socket incoming = s.accept();
InputStream inStream = incoming.getInputStream();
Scanner in = new Scanner(inStream);
for(int i=-0;i<3;i++)
{
v[i] = Math.max(v[i],in.nextInt();
}
System.out.println("My System Clock");p
for(int i=0;i<3;i++)
{
System.out.print(v[i]+',');

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


}
s.close();
}
}
}
}
catch(Exception e)
{}
}
}

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.

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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.*;

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


public class Registry{
public static void main(String[] args){
try{
my.MyServer m = new my.MyServer();
Naming.rebind("ducat",m);
}catch(Exception e){}
}
}

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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";

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


else
serverName=args[0];
String rmiName="rmi://"+serverName+"/CalculatorService";
System.out.println("Connecting to:"+rmiName);
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("\n\nEnter first number:");
inp=stdin.readLine();
n1=Double.parseDouble(inp);
System.out.print("\n\nEnter Second number:");
inp=stdin.readline();
n2=Double.parseDouble(inp);
}
catch(IOException ioex)
{
System.out.println("Input error");
System.exit(1);
}
try
{Calculator c=(Calculator)
Naming.lookup(rmiName);
System.out.println(c.sub(n1,n2));
System.out.println(c.add(n1,n2));
System.out.println(c.mul(n1,n2));
System.out.println(c.div(n1,n2));
}
catch(MalformedURLException murle){
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch(RemoteException re){
System.out.println();
System.out.println("RemotException");
System.out.println(re);
}
catch(NotBoundException nbe)
{
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch(

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


java.lang.ArithmeticException ae)
{
System.out.println();
System.out.println(
"java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
/*
Running Steps
javac *.java
rmic CalculatorImpl
rmiregistry in seperate console
java CalculatorServer in seperate console
java CalculatorClient in seperate console
*/

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{

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


System.out.println("Multiplying"+a+"by"+b);
return a*b;
}
public double div(double a,double b)
throws java.rmi.RemoteException{
System.out.println("Dividing"+a+"by"+b);
return a/b;
}
}

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();
}
}

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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;
/**
*

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


* @author Student
*/
public class TimeClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
IntTime i=(IntTime) Naming.lookup("rmi://localhost/TimeService");
while (true) {
String str=i.get();
System.out.print(str);
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.print(e);
}
}
}

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();
}
}

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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();

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


}
catch(NotBoundException 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;

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


}
}
static public void main(String args[])
{
try{
RmiServer s=new RmiServer();
}
catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
}
/*
Compile
1. javac RmiServer.java
2. rmic RmiServer
3. javac RmiClient.java
Execution
1. (at one host,) java RmiServer
2. (at another host) java RmiClient<server's address>3232<message text>
*/

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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;
}
}

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY

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;
/**
*

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

DEPARTMENT OF INFORMATION TECHNOLOGY


* @author Student
*/
import java.rmi.*;
public interface HelloInterface extends Remote {
public String say()throws RemoteException;
public String code()throws RemoteException;
}

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);
}
}
}

I.T.S ENGINEERING COLLEGE, GREATER NOIDA

Das könnte Ihnen auch gefallen