Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RMI EXECUTION(3 TERMINAL OPEN)
- notepad type inside folder create
- path="c-pf-java-jdk-bin vala path copy "
- 1) i)>javac *.java
- iii)>rmic implfile
- iv)>java serverfile
- 2)ii)>start rmiregistry
- 3)v)>java clientfile
- TCP
- 1. tcpServerPrime.java
- import java.net.*; import java.io.*;
- class tcpServerPrime
- {
- public static void main(String args[])
- {
- try
- {
- ServerSocket ss = new ServerSocket(8001); System.out.println("Server Started..............."); Socket s = ss.accept();
- DataInputStream in = new DataInputStream(s.getInputStream()); int x= in.readInt(); DataOutputStream otc = new DataOutputStream(s.getOutputStream()); int y = x/2; if(x ==1 || x ==2 || x ==3)
- {
- otc.writeUTF(x + "is Prime"); System.exit(0);
- }
- for(int i=2; i<=y; i++)
- {
- if(x%i != 0)
- {
- }
- else
- {
- }
- }
- }
- otc.writeUTF(x + " is Prime");
- otc.writeUTF(x + " is not Prime");
- catch(Exception e)
- {
- System.out.println(e.toString());
- }
- }
- }
- 2. tcpClientPrime.java
- import java.net.*; import java.io.*;
- class tcpClientPrime
- {
- public static void main(String args[])
- {
- try
- {
- Socket cs = new Socket("LocalHost",8001); BufferedReader infu = new BufferedReader(new
- InputStreamReader(System.in));
- System.out.println("Enter a number : "); int a = Integer.parseInt(infu.readLine()); DataOutputStream out = new
- DataOutputStream(cs.getOutputStream());
- out.writeInt(a); DataInputStream in = new
- DataInputStream(cs.getInputStream()); System.out.println(in.readUTF()); cs.close();
- }
- catch(Exception e)
- {
- System.out.println(e.toString());
- }
- }
- }
- CHAT
- 1. ChatServer.java
- import java.net.*; import java.io.*;
- class ChatServer
- {
- public static void main(String args[])
- {
- try
- {
- ServerSocket ss = new ServerSocket(8000); System.out.println("Waiting for client to connect.."); Socket s = ss.accept();
- BufferedReader br = new BufferedReader(new
- InputStreamReader(System.in));
- DataOutputStream out = new DataOutputStream(s.getOutputStream()); DataInputStream in = new DataInputStream(s.getInputStream()); String receive, send; while((receive = in.readLine()) != null)
- {
- if(receive.equals("STOP")) break;
- System.out.println("Client Says : "+receive); System.out.print("Server Says : ");
- send = br.readLine(); out.writeBytes(send+"\n");
- }
- br.close();
- in.close();
- out.close();
- s.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- 2. ChatClient.java
- import java.net.*; import java.io.*;
- class ChatClient
- {
- public static void main(String args[])
- {
- try
- {
- Socket s = new Socket("Localhost",8000); BufferedReader br = new BufferedReader(new
- InputStreamReader(System.in));
- DataOutputStream out = new DataOutputStream(s.getOutputStream()); DataInputStream in = new DataInputStream(s.getInputStream()); String msg; System.out.println("To stop chatting with server type
- STOP"); System.out.print("Client Says: "); while((msg = br.readLine()) != null)
- {
- out.writeBytes(msg+"\n"); if(msg.equals("STOP"))
- break;
- System.out.println("Server Says : "+in.readLine()); System.out.print("Client Says : ");
- }
- br.close();
- in.close();
- out.close();
- s.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- UDP
- 1. udpServerEO.java
- /*Program which finds entered number is even or odd */ import java.io.*;
- import java.net.*;
- public class udpServerEO
- {
- public static void main(String args[])
- {
- try
- {
- DatagramSocket ds = new DatagramSocket(2000); byte b[] = new byte[1024];
- DatagramPacket dp = new DatagramPacket(b,b.length); ds.receive(dp);
- String str = new String(dp.getData(),0,dp.getLength()); System.out.println(str);
- int a= Integer.parseInt(str); String s= new String();
- if (a%2 == 0)
- s = "Number is even";
- else
- s = "Number is odd";
- byte b1[] = new byte[1024]; b1 = s.getBytes(); DatagramPacket dp1 = new
- DatagramPacket(b1,b1.length,InetAddress.getLocalHost(),1000);
- ds.send(dp1);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- 2. udpClientEO.java
- /*Program which finds entered number is even or odd*/
- import java.io.*; import java.net.*;
- public class udpClientEO
- {
- public static void main(String args[])
- {
- try
- {
- DatagramSocket ds = new DatagramSocket(1000); BufferedReader br = new BufferedReader(new
- InputStreamReader(System.in));
- System.out.println("Enter a number : "); String num = br.readLine();
- byte b[] = new byte[1024]; b=num.getBytes(); DatagramPacket dp = new
- DatagramPacket(b,b.length,InetAddress.getLocalHost(),2000);
- ds.send(dp);
- byte b1[] = new byte[1024]; DatagramPacket dp1 = new
- DatagramPacket(b1,b1.length); ds.receive(dp1);
- String str = new String(dp1.getData(),0,dp1.getLength()); System.out.println(str);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- 1. udpServerFact.java
- /*Program which calculate factorial of a number*/ import java.io.*;
- import java.net.*;
- public class udpServerFact
- {
- public static void main(String args[])
- {
- try
- {
- DatagramSocket ds = new DatagramSocket(2000); byte b[] = new byte[1024];
- DatagramPacket dp = new DatagramPacket(b,b.length); ds.receive(dp);
- String str = new String(dp.getData(),0,dp.getLength()); System.out.println(str);
- int a= Integer.parseInt(str); int f = 1, i;
- String s= new String(); for(i=1;i<=a;i++)
- {
- f=f*i;
- }
- s=Integer.toString(f);
- String str1 = "The Factorial of " + str + " is : " + f; byte b1[] = new byte[1024]; b1 = str1.getBytes(); DatagramPacket dp1 = new
- DatagramPacket(b1,b1.length,InetAddress.getLocalHost(),1000);
- ds.send(dp1);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- 2. udpClientFact.java
- /*Program which calculate factorial of a number*/ import java.io.*;
- import java.net.*;
- public class udpClientFact
- {
- public static void main(String args[])
- {
- try
- {
- DatagramSocket ds = new DatagramSocket(1000); BufferedReader br = new BufferedReader(new
- InputStreamReader(System.in));
- System.out.println("Enter a number : "); String num = br.readLine();
- byte b[] = new byte[1024]; b=num.getBytes(); DatagramPacket dp = new
- DatagramPacket(b,b.length,InetAddress.getLocalHost(),2000);
- ds.send(dp);
- byte b1[] = new byte[1024];
- DatagramPacket dp1 = new DatagramPacket(b1,b1.length); ds.receive(dp1);
- String str = new String(dp1.getData(),0,dp1.getLength()); System.out.println(str);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- RMI
- 1
- 1. InterDate.java
- import java.rmi.*;
- public interface InterDate extends Remote
- {
- public String display() throws Exception;
- }
- 2. ServerDate.java
- import java.rmi.*; import java.rmi.server.*; import java.util.*;
- public class ServerDate extends UnicastRemoteObject implements InterDate {
- public ServerDate() throws Exception
- {
- }
- public String display() throws Exception
- {
- String str = "";
- Date d = new Date(); str = d.toString(); return str;
- }
- public static void main(String args[]) throws Exception {
- ServerDate s1 = new ServerDate(); Naming.bind("DS",s1); System.out.println("Object registered.....");
- }
- }
- 3. ClientDate.java
- import java.rmi.*; import java.io.*;
- public class ClientDate
- {
- public static void main(String args[]) throws Exception {
- String s1;
- InterDate h1 = (InterDate)Naming.lookup("DS"); s1 = h1.display();
- System.out.println(s1);
- }
- }
- 1STEP= javac file name.java(3file)
- 2step= rmic server
- 3step= rmiregistry
- java server
- java client
- 2)
- 1. InterConvert.java
- import java.rmi.*;
- public interface InterConvert extends Remote
- {
- public String convertDigit(String no) throws Exception;
- }
- 2. ServerConvert.java
- import java.rmi.*; import java.rmi.server.*;
- public class ServerConvert extends UnicastRemoteObject implements InterConvert {
- public ServerConvert() throws Exception
- {
- }
- public String convertDigit(String no) throws Exception
- {
- String str = "";
- for(int i = 0; i < no.length(); i++)
- {
- int p = no.charAt(i); if( p == 48)
- {
- str += "zero ";
- }
- if( p == 49)
- {
- str += "one ";
- }
- if( p == 50)
- {
- str += "two ";
- }
- if( p == 51)
- {
- str += "three ";
- }
- if( p == 52)
- {
- str += "four ";
- }
- if( p == 53)
- {
- str += "five ";
- }
- if( p == 54)
- {
- str += "six ";
- }
- if( p == 55)
- {
- str += "seven ";
- }
- if( p == 56)
- {
- str += "eight ";
- }
- if( p == 57)
- {
- str += "nine ";
- }
- }
- return str;
- }
- public static void main(String args[]) throws Exception {
- ServerConvert s1 = new ServerConvert(); Naming.bind("Wrd",s1); System.out.println("Object registered....");
- }
- }
- 3. ClientConvert.java
- import java.rmi.*; import java.io.*;
- public class ClientConvert
- {
- public static void main(String args[]) throws Exception {
- InterConvert h1 = (InterConvert)Naming.lookup("Wrd"); BufferedReader br = new BufferedReader(new
- InputStreamReader(System.in));
- System.out.println("Enter a number : \t"); String no = br.readLine();
- String ans = h1.convertDigit(no);
- System.out.println("The word representation of the entered digit is : " +ans);
- }
- }
- RPC
- 1. RPCServer.java
- import java.util.*; import java.net.*;
- class RPCServer
- {
- DatagramSocket ds;
- DatagramPacket dp;
- String str,methodName,result; int val1,val2;
- RPCServer()
- {
- try
- {
- ds=new DatagramSocket(1200); byte b[]=new byte[4096];
- while(true)
- {
- dp=new DatagramPacket(b,b.length); ds.receive(dp);
- str=new String(dp.getData(),0,dp.getLength());
- if(str.equalsIgnoreCase("q"))
- {
- }
- else
- {
- System.exit(1);
- StringTokenizer st = new StringTokenizer(str," "); int i=0;
- while(st.hasMoreTokens())
- {
- String token=st.nextToken(); methodName=token;
- val1 = Integer.parseInt(st.nextToken()); val2 = Integer.parseInt(st.nextToken());
- }
- }
- System.out.println(str);
- InetAddress ia = InetAddress.getLocalHost();
- if(methodName.equalsIgnoreCase("add"))
- {
- result= "" + add(val1,val2);
- }
- else if(methodName.equalsIgnoreCase("sub"))
- {
- result= "" + sub(val1,val2);
- }
- else if(methodName.equalsIgnoreCase("mul"))
- {
- result= "" + mul(val1,val2);
- }
- else if(methodName.equalsIgnoreCase("div"))
- {
- result= "" + div(val1,val2);
- }
- byte b1[]=result.getBytes();
- DatagramSocket ds1 = new DatagramSocket(); DatagramPacket dp1 = new
- DatagramPacket(b1,b1.length,InetAddress.getLocalHost(), 1300);
- System.out.println("result : "+result+"\n"); ds1.send(dp1);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public int add(int val1, int val2)
- {
- return val1+val2;
- }
- public int sub(int val3, int val4)
- {
- return val3-val4;
- }
- public int mul(int val3, int val4)
- {
- return val3*val4;
- }
- public int div(int val3, int val4)
- {
- return val3/val4;
- }
- public static void main(String[] args)
- {
- new RPCServer();
- }
- }
- 2. RPCClient.java
- import java.io.*; import java.net.*;
- class RPCClient
- {
- RPCClient()
- {
- 4\n");
- try
- {
- InetAddress ia = InetAddress.getLocalHost(); DatagramSocket ds = new DatagramSocket(); DatagramSocket ds1 = new DatagramSocket(1300); System.out.println("\nRPC Client\n");
- System.out.println("Enter method name and parameter like add 3
- while (true)
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str = br.readLine(); byte b[] = str.getBytes(); DatagramPacket dp = new
- DatagramPacket(b,b.length,ia,1200);
- ds.send(dp);
- dp = new DatagramPacket(b,b.length); ds1.receive(dp);
- String s = new String(dp.getData(),0,dp.getLength()); System.out.println("\nResult = " + s + "\n");
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void main(String[] args)
- {
- new RPCClient();
- }
- }
- 1. RPCNumServer.java
- import java.util.*; import java.net.*; import java.io.*; class RPCNumServer
- {
- DatagramSocket ds;
- DatagramPacket dp;
- String str,methodName,result; int val;
- RPCNumServer()
- {
- try
- {
- ds=new DatagramSocket(1200); byte b[]=new byte[4096]; while(true)
- {
- dp=new DatagramPacket(b,b.length); ds.receive(dp);
- str=new String(dp.getData(),0,dp.getLength()); if(str.equalsIgnoreCase("q")) {
- System.exit(1);
- }
- else
- {
- StringTokenizer st = new StringTokenizer(str," "); int i=0;
- while(st.hasMoreTokens())
- {
- String token=st.nextToken(); methodName=token;
- val = Integer.parseInt(st.nextToken());
- }
- }
- System.out.println(str);
- InetAddress ia = InetAddress.getLocalHost(); if(methodName.equalsIgnoreCase("square"))
- {
- result= "" + square(val);
- }
- else if(methodName.equalsIgnoreCase("squareroot"))
- {
- result= "" + squareroot(val);
- }
- else if(methodName.equalsIgnoreCase("cube"))
- {
- result= "" + cube(val);
- }
- else if(methodName.equalsIgnoreCase("cuberoot"))
- {
- result= "" + cuberoot(val);
- }
- byte b1[]=result.getBytes();
- DatagramSocket ds1 = new DatagramSocket();
- DatagramPacket dp1 = new DatagramPacket(b1,b1.length,InetAddress.getLocalHost(), 1300);
- System.out.println("result : "+result+"\n"); ds1.send(dp1);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public double square(int a) throws Exception
- {
- double ans; ans = a*a; return ans;
- }
- public double squareroot(int a) throws Exception
- {
- double ans;
- ans = Math.sqrt(a); return ans;
- }
- public double cube(int a) throws Exception
- {
- double ans; ans = a*a*a; return ans;
- }
- public double cuberoot(int a) throws Exception
- {
- double ans;
- ans = Math.cbrt(a); return ans;
- }
- public static void main(String[] args)
- {
- new RPCNumServer();
- }
- }
- 2. RPCNumClient.java
- import java.io.*; import java.net.*; class RPCNumClient
- {
- RPCNumClient()
- {
- try
- {
- InetAddress ia = InetAddress.getLocalHost(); DatagramSocket ds = new DatagramSocket(); DatagramSocket ds1 = new DatagramSocket(1300); System.out.println("\nRPC Client\n");
- System.out.println("1. Square of the number - square\n2. Square root of the number - squareroot\n3. Cube of the number - cube\n4. Cube root of the number - cuberoot");
- System.out.println("Enter method name and the number\n"); while (true)
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str = br.readLine(); byte b[] = str.getBytes(); DatagramPacket dp = new
- DatagramPacket(b,b.length,ia,1200);
- ds.send(dp);
- dp = new DatagramPacket(b,b.length); ds1.receive(dp);
- String s = new String(dp.getData(),0,dp.getLength()); System.out.println("\nResult = " + s + "\n");
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void main(String[] args)
- {
- new RPCNumClient();
- }
- }
- SQL WEBSERVICES
- Code Spec
- package webservice;
- import java.sql.*;
- import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.ejb.Stateless;
- @WebService() @Stateless()
- public class BookWS {
- /**
- * Web service operation
- */
- @WebMethod(operationName = "getBookDetails") public String getBookDetails(@WebParam(name = "isbn") String isbn) {
- //TODO write your implementation code here: Connection dbcon = null;
- Statement stmt = null; ResultSet rs = null; String query = null; try
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- 40
- Distributed Systems – Practical Manual – 2018-2019
- dbcon =
- DriverManager.getConnection("jdbc:mysql://localhost/bookshop","root","123"); stmt = dbcon.createStatement();
- query = "select * from books where isbn = '" +isbn+ "'"; rs = stmt.executeQuery(query); rs.next();
- String bookDetails = "<h1>The name of the book is <b>"
- +rs.getString("bookname") + "</b> and its cost is <b>" +rs.getString("bookprice") + "</b></h1>.";
- return bookDetails;
- }
- catch(Exception e)
- {
- System.out.println("Sorry failed to connect to the database.." + e.getMessage());
- }
- return null;
- }
- }
- MUTUAL TOKEN
- 1. TokenServer.java
- import java.io.*; import java.net.*;
- public class TokenServer
- {
- public static void main(String agrs[])throws Exception {
- while(true)
- {
- Server sr=new Server(); sr.recPort(8000); sr.recData();
- }
- }
- }
- class Server
- {
- boolean hasToken=false; boolean sendData=false; int recport;
- void recPort(int recport)
- {
- this.recport=recport;
- }
- void recData()throws Exception
- {
- byte buff[]=new byte[256];
- 49
- Distributed Systems – Practical Manual – 2018-2019
- DatagramSocket ds; DatagramPacket dp; String str;
- ds=new DatagramSocket(recport);
- dp=new DatagramPacket(buff,buff.length); ds.receive(dp);
- ds.close();
- str=new String(dp.getData(),0,dp.getLength()); System.out.println("The message is "+str);
- }
- }
- 2. TokenClient1.java
- import java.io.*; import java.net.*;
- public class TokenClient1
- {
- public static void main(String arg[]) throws Exception
- {
- InetAddress lclhost; BufferedReader br; String str=""; TokenClient12 tkcl,tkser; boolean hasToken; boolean setSendData;
- while(true)
- {
- lclhost=InetAddress.getLocalHost(); tkcl = new TokenClient12(lclhost); tkser = new TokenClient12(lclhost); tkcl.setSendPort(9004); tkcl.setRecPort(8002); lclhost=InetAddress.getLocalHost(); tkser.setSendPort(9000);
- if(tkcl.hasToken == true)
- {
- YES/NO");
- System.out.println("Do you want to enter the Data -->
- br=new BufferedReader(new InputStreamReader(System.in)); str=br.readLine();
- if(str.equalsIgnoreCase("yes"))
- {
- System.out.println("ready to send"); tkser.setSendData = true; tkser.sendData();
- tkser.setSendData = false;
- 50
- Distributed Systems – Practical Manual – 2018-2019
- }
- else if(str.equalsIgnoreCase("no"))
- {
- }
- }
- else
- {
- tkcl.sendData(); tkcl.recData();
- System.out.println("ENTERING RECEIVING MODE..."); tkcl.recData();
- }
- }
- }
- }
- class TokenClient12
- {
- InetAddress lclhost; int sendport,recport;
- boolean hasToken = true; boolean setSendData = false; TokenClient12 tkcl,tkser;
- TokenClient12(InetAddress lclhost)
- {
- this.lclhost = lclhost;
- }
- void setSendPort(int sendport)
- {
- this.sendport = sendport;
- }
- void setRecPort(int recport)
- {
- this.recport = recport;
- }
- void sendData() throws Exception
- {
- BufferedReader br; String str="Token"; DatagramSocket ds; DatagramPacket dp;
- if(setSendData == true)
- {
- System.out.println("sending "); System.out.println("Enter the Data");
- 51
- Distributed Systems – Practical Manual – 2018-2019
- br=new BufferedReader(new InputStreamReader(System.in)); str = "ClientOne....." + br.readLine(); System.out.println("now sending");
- }
- ds = new DatagramSocket(sendport);
- dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000); ds.send(dp);
- ds.close(); setSendData = false; hasToken = false;
- }
- void recData()throws Exception
- {
- String msgstr;
- byte buffer[] = new byte[256]; DatagramSocket ds; DatagramPacket dp;
- ds = new DatagramSocket(recport);
- dp = new DatagramPacket(buffer,buffer.length); ds.receive(dp);
- ds.close();
- msgstr = new String(dp.getData(),0,dp.getLength()); System.out.println("The data is "+msgstr);
- if(msgstr.equals("Token"))
- {
- hasToken = true;
- }
- }
- }
- 3. TokenClient2.java
- import java.io.*; import java.net.*;
- public class TokenClient2
- {
- static boolean setSendData ; static boolean hasToken ;
- public static void main(String arg[]) throws Exception
- {
- InetAddress lclhost; BufferedReader br; String str1; TokenClient21 tkcl; TokenClient21 ser;
- 52
- Distributed Systems – Practical Manual – 2018-2019
- while(true)
- {
- lclhost=InetAddress.getLocalHost(); tkcl = new TokenClient21(lclhost); tkcl.setRecPort(8004); tkcl.setSendPort(9002); lclhost=InetAddress.getLocalHost(); ser = new TokenClient21(lclhost); ser.setSendPort(9000);
- if(hasToken == true)
- {
- YES/NO");
- System.out.println("Do you want to enter the Data -->
- br=new BufferedReader(new InputStreamReader(System.in)); str1=br.readLine();
- if(str1.equalsIgnoreCase("yes"))
- {
- ser.setSendData = true; ser.sendData();
- }
- else if(str1.equalsIgnoreCase("no"))
- {
- }
- }
- else
- {
- tkcl.sendData(); hasToken=false;
- System.out.println("entering recieving mode"); tkcl.recData();
- hasToken=true;
- }
- }
- }
- }
- class TokenClient21
- {
- InetAddress lclhost; int sendport,recport;
- boolean setSendData = false; boolean hasToken = false; TokenClient21 tkcl; TokenClient21 ser;
- TokenClient21(InetAddress lclhost)
- {
- this.lclhost = lclhost;
- }
- 53
- Distributed Systems – Practical Manual – 2018-2019
- void setSendPort(int sendport)
- {
- this.sendport = sendport;
- }
- void setRecPort(int recport)
- {
- this.recport = recport;
- }
- void sendData() throws Exception
- {
- BufferedReader br; String str="Token"; DatagramSocket ds; DatagramPacket dp;
- if(setSendData == true)
- {
- System.out.println("Enter the Data");
- br=new BufferedReader(new InputStreamReader(System.in)); str = "ClientTwo....." + br.readLine();
- }
- ds = new DatagramSocket(sendport);
- dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000); ds.send(dp);
- ds.close(); System.out.println("Data Sent"); setSendData = false;
- hasToken = false;
- }
- void recData()throws Exception
- {
- String msgstr;
- byte buffer[] = new byte[256]; DatagramSocket ds;
- DatagramPacket dp; ds = new DatagramSocket(recport); dp = new DatagramPacket(buffer,buffer.length); ds.receive(dp);
- ds.close(); msgstr = new
- String(dp.getData(),0,dp.getLength()); System.out.println("The data is "+msgstr); if(msgstr.equals("Token")) {
- hasToken = true;
- }
- }
- }
- CLOCK SYCHRONIZATION
- 1. SCServer.java
- import java.io.*; import java.net.*; import java.sql.*;
- public class SCServer
- {
- public static void main(String args[])throws Exception {
- InetAddress lclhost; lclhost=InetAddress.getLocalHost();
- long maxtime,skewtime,datatime; String maxtimestr,skewtimestr; BufferedReader br;
- ClntServer ser=new ClntServer(lclhost);
- System.out.println("Enter the maximum time"); br = new BufferedReader(new
- InputStreamReader(System.in)); maxtimestr=br.readLine();
- 56
- Distributed Systems – Practical Manual – 2018-2019
- System.out.println("Enter the maximum skew time"); br = new BufferedReader(new
- InputStreamReader(System.in)); skewtimestr=br.readLine();
- maxtime=Long.parseLong(maxtimestr); skewtime=Long.parseLong(skewtimestr);
- while(true)
- {
- }
- }
- }
- datatime = System.currentTimeMillis(); long G = datatime-maxtime-skewtime; System.out.println("G ="+G); ser.setTimeStamp(new Timestamp(G)); ser.recPort(8001);
- ser.recData();
- class ClntServer
- {
- InetAddress lclhost; int recport; Timestamp obtmp;
- ClntServer(InetAddress lclhost)
- {
- this.lclhost = lclhost;
- }
- void recPort(int recport)
- {
- this.recport = recport;
- }
- void setTimeStamp(Timestamp obtmp)
- {
- this.obtmp = obtmp;
- }
- void recData()throws Exception
- {
- String msgstr=""; DatagramSocket ds; DatagramPacket dp; BufferedReader br;
- byte buf[] = new byte[256];
- ds = new DatagramSocket(recport);
- dp = new DatagramPacket(buf,buf.length);
- 57
- Distributed Systems – Practical Manual – 2018-2019
- ds.receive(dp); ds.close();
- msgstr = new String(dp.getData(),0,dp.getLength()); System.out.println(msgstr);
- Timestamp obtmp = new Timestamp(Long.parseLong(msgstr)); if(this.obtmp.before(obtmp) == true)
- {
- }
- else
- {
- }
- }
- }
- System.out.println("The Message is accepted");
- System.out.println("The Message is rejected");
- 2. SCClient.java
- import java.io.*; import java.net.*;
- public class SCClient
- {
- public static void main(String args[])throws Exception {
- InetAddress lclhost; lclhost=InetAddress.getLocalHost();
- while(true)
- {
- }
- }
- }
- Client cntl=new Client(lclhost); cntl.sendPort(9001); cntl.sendData();
- class Client
- {
- InetAddress lclhost; int senport;
- Client(InetAddress lclhost)
- {
- this.lclhost=lclhost;
- }
- 58
- Distributed Systems – Practical Manual – 2018-2019
- void sendPort(int senport)
- {
- this.senport=senport;
- }
- void sendData()throws Exception
- {
- DatagramPacket dp; DatagramSocket ds; BufferedReader br;
- br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the data"); String str=br.readLine();
- ds = new DatagramSocket(senport);
- dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,senport-1000); ds.send(dp);
- ds.close();
- }
- }
- Write a program to implement two phase commit protocol.
- Algorithm Used:
- A) Actions by the coordinator:
- Write START_2PC to local log;
- 60
- Distributed Systems – Practical Manual – 2018-2019
- Multicast VOTE_REQUEST to all participants; While not all votes have been collected {
- Wait for any incoming vote; If timeout
- {
- Write GLOBAL_ABORT to local log; Multicast GLOBAL_ABORT to all participants; Exit;
- }
- record vote;
- }
- If all participant sent VOTE_COMMIT and coordinator votes COMMIT
- {
- }
- else
- {
- }
- Write GLOBAL_COMMIT to local log; Multicast GLOBAL_COMMIT to all participants;
- Write GLOBAL_ABORT to local log; Multicast GLOBAL_ABORT to all participants;
- B) Actions by participants:
- Write INIT to local log;
- Wait for VOTE_REQUEST from coordinator; If timeout
- {
- write VOTE_ABORT to local log; exit;
- }
- if participant vote COMMIT
- {
- write VOTE_COMMIT to local log; send VOTE_COMMIT to coordinator; wait for DECISION from coordinator; if timeout
- {
- multicast DECISION_REQUEST to other participants; wait until DECISION is received;/*remain blocked*/ write DECISION to local log;
- }
- }
- else
- {
- if DECISION= = GLOBAL_COMMIT
- write GLOBAL_COMMIT to local log; else if DECISION= = GLOBAL_ABORT
- write GLOBAL_ABORT to local log;
- write VOTE_ABORT to local log;
- 61
- Distributed Systems – Practical Manual – 2018-2019
- send VOTE_ABORT to coordinator;
- }
- C) Action for handling decision request:
- While true
- {
- }
- Code:
- wait until any incoming DECISION_REQUEST is received;/*remain blocked*/ read most recently recorded STATES from the local log;
- if STATE= = GLOBAL_COMMIT
- send GLOBAL_COMMIT to requesting participants; else if STATE= =INIT or STATE= = GLOBAL_ABORT send
- GLOBAL_ABORT to requesting participants;
- else
- skip;/* participant remains blocked.*/
- 1. Server.java
- import java.io.*; import java.net.*; import java.util.*;
- public class Server
- {
- boolean closed=false,inputFromAll=false; List<clientThread> t;
- List<String> data;
- Server()
- {
- t = new ArrayList<clientThread>(); data= new ArrayList<String>();
- }
- public static void main(String args[])
- {
- Socket clientSocket = null; ServerSocket serverSocket = null; int port_number=1111;
- Server ser=new Server(); try
- {
- serverSocket = new ServerSocket(port_number);
- }
- catch (IOException e)
- {
- System.out.println(e);
- }
- while(!ser.closed)
- {
- 62
- Distributed Systems – Practical Manual – 2018-2019
- }
- try
- {
- try
- {
- }
- clientSocket = serverSocket.accept();
- clientThread th=new clientThread(ser,clientSocket); (ser.t).add(th);
- System.out.println("\nNow Total clients are : "+(ser.t).size()); (ser.data).add("NOT_SENT");
- th.start();
- catch (IOException e)
- {
- }
- serverSocket.close();
- }
- catch(Exception e1)
- {
- }
- }
- }
- class clientThread extends Thread
- {
- DataInputStream is = null; String line;
- String destClient=""; String name; PrintStream os = null;
- Socket clientSocket = null; String clientIdentity; Server ser;
- public clientThread(Server ser,Socket clientSocket)
- {
- this.clientSocket=clientSocket; this.ser=ser;
- }
- public void run()
- {
- try
- {
- is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); os.println("Enter your name."); name = is.readLine(); clientIdentity=name;
- 63
- Distributed Systems – Practical Manual – 2018-2019
- os.println("Welcome "+name+" to this 2 Phase Application.\nYou will receive a vote Request now...");
- os.println("VOTE_REQUEST\nPlease enter COMMIT or ABORT to
- proceed : ");
- for(int i=0; i<(ser.t).size(); i++)
- {
- if((ser.t).get(i)!=this)
- {
- ((ser.t).get(i)).os.println("---A new user
- "+name+" entered the Appilcation---");
- }
- }
- while (true)
- {
- line = is.readLine(); if(line.equalsIgnoreCase("ABORT"))
- {
- System.out.println("\nFrom '"+clientIdentity+"' :
- ABORT\n\nSince aborted we will not wait for inputs from other clients.");
- System.out.println("\nAborted....");
- for(int i=0; i<(ser.t).size(); i++)
- {
- ((ser.t).get(i)).os.println("GLOBAL_ABORT");
- ((ser.t).get(i)).os.close();
- ((ser.t).get(i)).is.close();
- }
- break;
- }
- if(line.equalsIgnoreCase("COMMIT"))
- {
- COMMIT");
- System.out.println("\nFrom '"+clientIdentity+"' :
- if((ser.t).contains(this))
- {
- (ser.data).set((ser.t).indexOf(this), "COMMIT"); for(int j=0;j<(ser.data).size();j++) {
- if(!(((ser.data).get(j)).equalsIgnoreCase("NOT_SENT")))
- {
- }
- else
- {
- ser.inputFromAll=true; continue;
- ser.inputFromAll=false; System.out.println("\nWaiting for inputs from other clients.");
- 64
- Distributed Systems – Practical Manual – 2018-2019
- break;
- }
- }
- if(ser.inputFromAll)
- {
- System.out.println("\n\nCommited....");
- for(int i=0; i<(ser.t).size(); i++)
- {
- ((ser.t).get(i)).os.println("GLOBAL_COMMIT");
- }
- }//if t.contains
- }//commit
- }//while
- ser.closed=true; clientSocket.close();
- }
- break;
- ((ser.t).get(i)).os.close();
- ((ser.t).get(i)).is.close();
- }
- catch(IOException e)
- {
- };
- }
- }
- 2. Client.java
- import java.io.*; import java.net.*;
- public class Client implements Runnable
- {
- static Socket clientSocket = null; static PrintStream os = null; static DataInputStream is = null;
- static BufferedReader inputLine = null; static boolean closed = false;
- public static void main(String[] args)
- {
- int port_number=1111; String host="localhost"; try
- {
- clientSocket = new Socket(host, port_number); inputLine = new BufferedReader(new
- InputStreamReader(System.in));
- 65
- Distributed Systems – Practical Manual – 2018-2019
- os = new PrintStream(clientSocket.getOutputStream());
- is = new DataInputStream(clientSocket.getInputStream());
- }
- catch (Exception e)
- {
- System.out.println("Exception occurred : "+e.getMessage());
- }
- if (clientSocket != null && os != null && is != null)
- {
- try
- {
- new Thread(new Client()).start();
- while (!closed)
- {
- os.println(inputLine.readLine());
- }
- os.close();
- is.close();
- clientSocket.close();
- }
- catch (IOException e)
- {
- System.err.println("IOException: " + e);
- }
- }
- }
- public void run()
- {
- String responseLine;
- try
- {
- while ((responseLine = is.readLine()) != null)
- {
- System.out.println("\n"+responseLine);
- if
- (responseLine.equalsIgnoreCase("GLOBAL_COMMIT")==true ||
- responseLine.equalsIgnoreCase("GLOBAL_ABORT")==true )
- {
- break;
- }
- }
- closed=true;
- }
- catch (IOException e)
- {
- System.err.println("IOException: " + e);
- }
- }
- }
- O/P- JAVA SERVER
- JAVA CLIENT(3TIMES)
- RPC EXECUTION
- JAVAC CLIENT
- JAVA CLIENT
- JAVA RPCServer
- 1)
- TCP(Transmission Control Protocol)
- Package used for Socket Programming Using TCP / UDP
- import java.io.*;
- import java.net.*;
- Connection Establishment Code.
- Server:
- try
- {
- int port = 8888;
- ServerSocket ss= new ServerSocket(port);
- System.out.println("Waiting for Client");
- Socket s = ss.accept();
- }
- catch (Exception e)
- {
- System.out.println("Error :" + e);
- }
- Client
- try
- {
- int port = 8888;
- InetAddress addr = InetAddress.getLocalHost();
- Socket s = new Socket(addr, port);
- System.out.println("Connection is established");
- }
- catch (Exception e)
- {
- System.out.println("Error :" + e);
- }
- User Input In TCP/UDP(BufferedReader with System.in):
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter The Number :");
- String req = br.readLine();
- Send Data to Socket In TCP
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
- pw.println(str);
- pw.flush();
- Receive Data from Socket in TCP(BufferedReader with getInputStream()).
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = br.readLine();
- System.out.println("Request From The Client :" + str);
- UDP(User Datagram Protocol)
- DatagramSocket Code.
- Server
- try
- {
- int port = 6666;
- DatagramSocket ds = new DatagramSocket(port);
- System.out.println("Waiting for Client");
- }
- catch (Exception e)
- {
- System.out.println("Error :" + e);
- }
- Client
- try
- {
- int port = 5555;
- DatagramSocket ds = new DatagramSocket(port);
- }
- catch (Exception e)
- {
- System.out.println("Error :" + e);
- }
- To Send Data In UDP
- byte b[] = new byte[1024];
- b = req.getBytes();
- DatagramPacket dp1 = new DatagramPacket(b,b.length,InetAddress.getLocalHost(),6666);
- ds.send(dp1);
- To Receive Data In Data
- byte b[] = new byte[1024];
- DatagramPacket dp1 = new DatagramPacket(b, b.length);
- ds.receive(dp1);
- String str = new String(dp1.getData(),0,dp1.getLength());
- RMI(Remote Method Invocation)
- Packages
- import java.io.*;
- import java.net.*;
- import java.rmi.*;
- import java.rmi.server.*;
- Step1 : Interface File(Mathoptr.java)
- import java.io.*;
- import java.net.*;
- import java.rmi.*;
- import java.rmi.server.*;
- public interface MathOptr extends Remote
- {
- public double square(int num) throws Exception;
- public double squareroot(int num) throws Exception;
- public double cube(int num) throws Exception;
- public double cuberoot(int num) throws Exception;
- }
- Step 2 : Implementation File(MathOptrImpl.java)
- import java.io.*;
- import java.net.*;
- import java.rmi.*;
- import java.rmi.server.*;
- public class MathOptrImpl extends UnicastRemoteObject implements MathOptr
- {
- public MathOptrImpl() throws Exception
- {
- super();
- }
- public double square(int num) throws Exception
- {
- return (num*num);
- }
- public double squareroot(int num) throws Exception
- {
- return(Math.sqrt(num));
- }
- public double cube(int num) throws Exception
- {
- return(num*num*num);
- }
- public double cuberoot(int num) throws Exception
- {
- return(Math.cbrt(num));
- }
- }
- Step 3 : Server File(MathOptrServer.java)
- import java.io.*;
- import java.net.*;
- import java.rmi.*;
- import java.rmi.server.*;
- public class MathOptrServer
- {
- public static void main(String args[]) throws Exception
- {
- MathOptrImpl obj = new MathOptrImpl();
- Naming.rebind("abc",obj);
- System.out.println("Waiting For Client");
- }
- }
- Step 4: Client File(MathOptrClient.java)
- import java.io.*;
- import java.net.*;
- import java.rmi.*;
- import java.rmi.server.*;
- public class MathOptrClient
- {
- public static void main(String args[]) throws Exception
- {
- MathOptr obj = (MathOptr) Naming.lookup("abc") ;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter The Number :");
- String req = br.readLine();
- int num = Integer.parseInt(req);
- System.out.println("Square :" + obj.square(num));
- System.out.println("Square Root :" + obj.squareroot(num));
- System.out.println("Cube :" + obj.cube(num));
- System.out.println("Cube Root :" + obj.cuberoot(num));
- }
- }
- Step 5: Compile all the files(javac *.java);
- Step6 : Start rmi registry(Start rmiregistry)
- Step7 : Generate Stub File(Rmic MathOptrImpl)
- Step8 : Run Server file(java MathOptrServer)
- Step 9: Run Client(java MathOptrClient)
- NFS Configuration
- We need 2 linux machine > Power ON the machines
- SERVER
- Right click > open terminal>
- Step 1: Set IP addr(vim /etc/sysconfig/network-scripts/ifcfg-eth0
- then press Enter
- After entering press > i
- DEVICE=eth0
- ONBOOT=yes
- BOOTPROTO=static
- IPADDR=192.168.1.10
- NETMASK=255.255.255.0
- (esc+shift+: wq)
- # service network restart
- # ifconfig(to check ipaddress changed)
- Step2: #service iptables stop
- # service iptables status
- Step 3: Install the req packages
- # cd /media/RHEl(press TAB)
- # rpm -ivh --force --nodeps nfs*
- Step 4: mkdir /nfsserver
- # cd /nfsserver/
- (nfsserver) # touch f1 f2 f3 f4
- # ls
- # chmod -R 777 /nfsserver/
- # ls (f1 f2 changs to green color)
- Step 4:# vim /etc/exports
- /nfsserver *(rw,sync)
- Esc+shift+: )wq
- # exportfs -a (actual export)
- # exportfs -v(to view the exported file system)
- Step 5: # service nfs restart(if failed run again command)
- Client
- Step1: Set IP addr(vim /etc/sysconfig/network-scripts/ifcfg-eth1 then press Enter
- After entering press > i
- DEVICE=eth1
- ONBOOT=yes
- BOOTPROTO=static
- IPADDR=192.168.1.11
- NETMASK=255.255.255.0
- (esc+shift+: wq)
- # service network restart
- # ifconfig(to check ipaddress changed)
- Step2: #service iptables stop
- # service iptables status
- Step 3: Install the req packages
- # cd /media/RHEl(press TAB)/P(TAB)
- # rpm -ivh --force --nodeps nfs*
- Step 3: mkdir /client
- # ls /client/(will be empty)
- # mount 192.168.1.10:/nfsserver /client
- # ls /client/(to check)
- To write contenrt
- # cat >> f1
- Hgfdiagfjkfghiusjkvsid
- Fact
- Int f=1;
- {f=f*I;}return f;
- Rev
- Int rev=0;
- While(num num me kola 0){Int r=num%10;
- Rev = rev*10+r;num=num/10;}return rev;
- Even odd
- {
- If(num%2==0)
- {return"the number is even";
- }
- else{ return "odd";
- }
- Prime
- {
- Boolean flag=false;
- for(Int I=2;iless num/2;++I)
- {
- If(num%==0)
- { flag=true}}if(!flag){return"prime"}
- Else{return "not prime";}}
- Vowels
- {
- If(name.equals('a') || name.equals('e') || name.equals('i') || name.equals('o') || name.equals('u') )
- {Return "vowels";}
- Else
- {Return"constants";
- }
- }
Add Comment
Please, Sign In to add comment