Professional Documents
Culture Documents
LABORATORY MANUAL
B.TECH (I.T) FINAL YEAR (7TH SEMESTER) Distributed Computing (TIT-442)
Govind Ballabh Pant University of Agriculture and Technology Department of Information Technology Pantnagar
Program No.1
PROBLEM: To simulate the functioning of Lamports Logical Clock in C. Theory:
Algorithm:
This simulation takes place by using the following concepts. 1) Happened Before Relationship 2) Logical clocks For any two events a & b, a is said to happened before b is denoted as a b, if they are in same process. If events occur at different processes then for any message (m) Send (m) Receive (m) If a b and b c then a c i.e. is transitive. If events casually affect each other then they are said to be casually related events a b Two events are concurrent if a b and b a i.e. a||b Conditions satisfied by Logical Clocks. a) For any two events occurring on same process, a b if Ci[a] < Ci[b]. b) Clock Ci is implemented between any two events of the same process as Ci = Ci + d (d>0) c) If event a is sending message by process Pi and is received by process Pj, then tm = Ci(a) Cj = max(Cj+d, tm), d>0
Program:
#include<stdio.h> #include<conio.h> void time(); void timecheck(int,int); int r[2][10]; int t1[10],t2[10]; int max(int,int); void main() { int i,j,n=0,n1,n2; clrscr(); printf("Enter the number of events of process1 :"); scanf("%d",&n1); printf("for process 1\n:"); printf("\t Enter 0 for no message passing\n"); for(i=1;i<n1;i++) { printf("Event %d sends message to :",i); scanf("%d",&n); r[1][i]=n; } printf("Enter the number of events for the process2:"); scanf("%d",&n2); printf("for process2\n"); printf("\t Enter 0 for no message passing\n");
Program No.2
PROBLEM: To Simulate the functioning of Vector Logical Clock in C. Program:
#include<stdio.h> #include<conio.h> void time(); void timecheck(int,int); int max(int,int); int r[2][10]; int t1[10],t2[10],v1[10],v2[10]; void main() { int i,j,n=0,n1,n2; clrscr(); printf("Enter the number of events of process1 :"); scanf("%d",&n1); printf("for process 1\n:"); printf("\t Enter 0 for no message passing\n"); for(i=1;i<n1;i++) { printf("Event %d sends message to :",i); scanf("%d",&n); r[1][i]=n; } printf("Enter the number of events for the process2:");
for process 1 time stamps are: event timestamp 1 2 3 4 5 (1,0) (2,0) (3,0) (4,0) (5,4)
for process 2 time stamps are: event timestamp 1 2 3 4 (0,1) (0,2) (1,3) (1,4)
Program No.3
PROBLEM: To Simulate the Distributed Mutual Exclusion in Java. Program:
import java.lang.*; class theLock extends PROBLEM {
10
11
Program No.4
PROBLEM: To implement a Distributed Chat Server using TCP Sockets in Java. Program:
// CLIENT import java.io.*; import java.net.*; public class SimpleClient{ public static void main(String args[]){ Socket clientSocket= null; DataInputStream is= null; PrintStream os= null; DataInputStream inputLine= null; System.out.println("======This is a client program======\n\n\n"); try { clientSocket= new Socket("127.0.0.1",2222); System.out.println("Established the connection with remote server"); System.out.println("local port of client:"+clientSocket.getLocalPort()); os=new PrintStream(clientSocket.getOutputStream()); is=new DataInputStream(clientSocket.getInputStream()); System.out.println("enter the message to be sent to the client"); 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{ String responseLine; os.println(inputLine.readLine()); System.out.println("Message sent to remote server"); System.out.println("Waiting for reply from remote server"); while((responseLine=is.readLine())!=null) { if(responseLine.startsWith("BYE")) break; System.out.println("received reply from server: " +responseLine); os.println(inputLine.readLine()); } System.out.println("closing all the connections............");
12
13
Output
E:\java\ds\TCP>javac TCPSimpleClient.java E:\java\ds\TCP>java TCPSimpleClient E:\java\ds\TCP>java TCPSimpleClient From server Hello E:\java\ds\TCP>javac TCPSimpleServer.java E:\java\ds\TCP>java TCPSimpleServer Received Hello from/127.0.0.1
Program No.5
PROBLEM: To implement a Distributed Chat Server using UDP Sockets in Java. Program:
//CLIENT import java.net.*; import java.io.*; public class UDPSimpleClient { public static void main(String args[]) { try { DatagramSocket socket; DatagramPacket packet; InetAddress address; socket =new DatagramSocket(); address=InetAddress.getByName("127.0.0.1"); int port=2000; String mess="Hello"; byte message[]=mess.getBytes(); packet=new DatagramPacket(message,message.length,address,port); socket.send(packet); socket.receive(packet); String recmessage=new String(message); System.out.println("from server:"+recmessage); socket.close(); }
14
15
Program No.6
PROBLEM: To implement Java RMI mechanism for accessing methods of remote systems. Theory:
RMI applications are often comprised of two separate programs: a server and client to invoke the events and some of PROBLEM make reference to them accessible and wait for client to invoke methods on these PROBLEMs. A typical application gets remote reference to one or more remote PROBLEMs in the server and the client and pass information back and forth. Distributed Remote PROBLEMs 1. Locate remote PROBLEMs 2. Communicate with remote PROBLEM RMI = RPC + PROBLEM Orientation
Program:
import java.rmi.*; public class AddClient { public static void main(String[] args) { try { String addServerURL="rmi";//"+args[0]+"/AddServer"; AddServerIntf addServerIntf=(AddServerIntf)Naming.lookup(addServerURL); Double d1=Double.parseDouble(args[1]); Double d2=Double.parseDouble(args[2]); System.out.println("The Sum is" +addServerIntf.add(d1,d2)); } catch(Exception e) { System.out.println("Error"); } } }
16
OUTPUT
E:\java\ds\rmi>javac *.java E:\java\ds\rmi>rmic AddServerImpl E:\java\ds\rmi>start rmiregistry E:\java\ds\rmi>java AddServer E:\java\ds\rmi>java AddClient 10.202.244.14 25.5 10.5 The sum is:36.0
Program No.7
PROBLEM: Modeling a taskbar server using producer consumer in Java. Program:
17
18
OUTPUT
C:\Documents and Settings\geit>e: E:\>cd vikas E:\vikas>javac TaskBag.java E:\vikas>java TaskBag press ctrl + c to stop Put :0 Got :0 Put :1
Program No.8
PROBLEM: To implement concurrent echo client-server application. Program:
19
20
21
22
Program No.9
PROBLEM: To implement the persistence storage of PROBLEMs in Java. Program:
import java.io.*; public class Serialization { public static void main(String[] args) { try { PerObj PROBLEM1=new PerObj("Hello",-7,2.7e10); System.out.println("Created PROBLEM1:" + PROBLEM1); FileOutputStream fos=new FileOutputStream("serial"); PROBLEMOutputStream oos=new PROBLEMOutputStream(fos); oos.writePROBLEM(PROBLEM1); System.out.println("Saved PROBLEM to file:"+PROBLEM1); oos.flush(); oos.close(); } catch(Exception e) { System.out.println("Exception during serialization"+e); System.exit(0); } try { PerObj PROBLEM2; FileInputStream fis=new FileInputStream("serial"); PROBLEMInputStream ois=new PROBLEMInputStream(fis); PROBLEM2=(PerObj)ois.readPROBLEM(); ois.close(); System.out.println("restored PROBLEM from file:"+PROBLEM2); } catch(Exception e) { System.out.println("Exception during deserialization:"+e); System.exit(0); } }
23
OUTPUT
E:\java\ds>java Serialization Created PROBLEM1:s=Hello; i=-7; d=2.7E10 Saved PROBLEM to file:s=Hello; i=-7; d=2.7E10 restored PROBLEM from file:s=Hello; i=-7; d=2.7E10
Program No.10
PROBLEM: To implement multicasting using multicast sender and multicast sniffer in Java. Program:
import java.io.IOException; import java.net.*; public class MulticastSender { public static void main(String[] args) { InetAddress ia=null; int port=0; String characters="This ia multicast data\n"; byte[] data=new byte[characters.length()]; try { try { ia=InetAddress.getByName(args[0]); }catch(UnknownHostException e) { System.out.println("UnknownHostException Caught");
24
25
OUTPUT
E:\javac MulticastSender.java E:\java MulticastSender 224.1.1.1 2000 E:\javac MulticastSniffer.java E:\java MulticastSnifferr 224.1.1.1 2000 this is a multicast data this is a multicast data this is a multicast data this is a multicast data E:\javac MulticastSniffer.java E:\java MulticastSnifferr 224.1.1.1 2000 this is a multicast data this is a multicast data this is a multicast data this is a multicast data
26