Guest User

Untitled

a guest
Jan 28th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.02 KB | None | 0 0
  1. Practical No.1
  2. Aim: Write a program that implements the concept for sharing resources using distributed system
  3.  
  4. Output:
  5.  
  6. 1.Checking if Samba is installed:
  7. 2.Change to home directory:
  8. 3.Configuration file:
  9. 4.Starting Samba:
  10. 5.Starting on boot time:
  11. 6.Stopping and checking status of firewall:
  12. 7.Setting and getting settings:
  13. 8.Enabling:
  14. 9.Adding user and setting password:
  15. 10.Setting password for user:
  16. 11.Start Samba:
  17. 12.Restarting Samba:
  18.  
  19.  
  20.  
  21. Practical No.2
  22.  
  23. Aim: Write a program for implementing Client Server communication model.
  24.  
  25. Practical 2A: A client server based program using TCP to find factorial of the number.
  26.  
  27. Code:
  28. 1.Factorial Server
  29.  
  30. import java.io.*;
  31. import java.net.*;
  32. importjava.util.*;
  33.  
  34. classFactorialServer
  35. {
  36. public static void main(String[] args)
  37. {
  38. System.out.println ("TCP Factorial Server"); try
  39. {
  40. ServerSocketss=new ServerSocket(4000);
  41. Socket s= ss.accept();
  42. DataInputStream dis =new DataInputStream (s.getInputStream());
  43. DataOutputStream dos =new DataOutputStream (s.getOutputStream());
  44. intnum= dis.readInt();
  45. int fact = 1;
  46. for (int i =1; i<=num ; i++)
  47. {
  48. fact=fact *i;
  49. }
  50. dos.writeInt(fact); s.close();
  51. }
  52. catch (Exception e)
  53. {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58.  
  59.  
  60. Output:
  61.  
  62.  
  63.  
  64. 2.FactorialCllient
  65.  
  66. import java.io.*;
  67. import java.net.*;
  68.  
  69. classFactorialClient
  70. {
  71. public static void main(String[] a)
  72. {
  73. System.out.println ("TCP Factorial Client") String host = "localhost";
  74. int port =4000;
  75. try
  76. {
  77. System.out.println("Enter the number whose factorial is to be found");
  78. DataInputStream in= new DataInputStream(System.in);
  79. intnum =Integer.parseInt(in.readLine());
  80. Socket s = new Socket (host , port);
  81. DataInputStream dis = new DataInputStream(s.getInputStream());
  82. DataOutputStream dos= new
  83. DataOutputStream(s.getOutputStream());
  84. dos.writeInt (num);
  85. int i = dis.readInt();
  86. System.out.println ("Factorial of the number :"+i); s.close();
  87. }
  88. catch (Exception e)
  89. {
  90. e.printStackTrace();
  91. }
  92. }
  93. }
  94.  
  95. Output:
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. Practical 2B: A client server based program using TCP to add given numbers.
  104. 1.TCPAddServer Code:
  105. import java.io.*;
  106. import java.net.*;
  107. importjava.util.*;
  108. classTCPAddServer
  109. {
  110. public static void main(String[] a)
  111. {
  112. System.out.println ("TCP Factorial Server")
  113. try
  114. {
  115. ServerSocketss=new ServerSocket(4000);
  116. Socket s= ss.accept();
  117. DataInputStream dis1 =new DataInputStream (s.getInputStream());
  118. DataInputStream dis2 =new DataInputStream (s.getInputStream());
  119. int num1= dis1.readInt();
  120. int num2= dis2.readInt();
  121. int add = num1 + num2;
  122. DataOutputStream dos =new DataOutputStream (s.getOutputStream());
  123. dos.writeInt(add);
  124. s.close();
  125. }
  126. catch (Exception e)
  127. {
  128. e.printStackTrace();
  129. }
  130. }
  131. }
  132.  
  133. Output
  134.  
  135.  
  136.  
  137. 2.TCPAddClient
  138.  
  139. import java.io.*;
  140. import java.net.*;
  141.  
  142. classTCPAddClient
  143. {
  144. public static void main(String[] a)
  145. {
  146. System.out.println ("Addition of two numbers Client") String host = "localhost";
  147. int port = 4000; try
  148. {System.out.println("Enter the two numbers");
  149. DataInputStream in1= new DataInputStream(System.in);
  150. DataInputStream in2= new DataInputStream(System.in);
  151. Socket s = new Socket (host , port);
  152. int num1 =Integer.parseInt(in1.readLine());
  153. int num2 =Integer.parseInt(in2.readLine());
  154. DataOutputStream dos1= new DataOutputStream (s.getOutputStream());
  155. dos1.writeInt (num1);
  156. DataOutputStream dos2= new DataOutputStream (s.getOutputStream());
  157. dos2.writeInt (num2);
  158. DataInputStream dis = new DataInputStream(s.getInputStream());
  159. int i = dis.readInt();
  160. System.out.println ("Addition of the two numbers :" +i);
  161. s.close();
  162. }
  163. catch (Exception e)
  164. {
  165. e.printStackTrace();
  166. }
  167. }
  168. }
  169.  
  170. Output
  171.  
  172.  
  173.  
  174. Practical 2C: A client server based program using UDP to find the square of the given number.
  175.  
  176. Code:
  177. 1.UDPServerSquare.java
  178.  
  179. import java.io.*;
  180. import java.net.*;
  181. importjava.util.*;
  182.  
  183. public class UDPServerSquare
  184. {
  185. public static void main(String args[]) throws Exception
  186. {
  187. DatagramSocket ds =new DatagramSocket(1111);
  188. byte[] rec= new byte[1024];
  189. byte[] send = new byte[1024];
  190. while (true)
  191. {
  192. DatagramPacketrecdp = new DatagramPacket (rec, rec.length);
  193. ds.receive (recdp);
  194. String str= new String (recdp.getData());
  195. intnum = Integer.parseInt(str.trim());
  196. System.out.println ("From Client :" + num);
  197. int result = num *num;
  198. InetAddressip =recdp.getAddress();
  199. int port = recdp.getPort();
  200. String data1= String.valueOf(result);
  201. send = data1.getBytes();
  202. DatagramPacketsenddp=new DatagramPacket (send,send.length,ip,port);
  203. ds.send(senddp);
  204. }
  205. }
  206. }
  207.  
  208.  
  209. Output:
  210.  
  211.  
  212.  
  213. 2.UDPClientSquare Code:
  214. import java.io.*;
  215. import java.net.*;
  216. importjava.util.*;
  217.  
  218. public class UDPClientSquare
  219. {
  220. public static void main(String args[]) throws Exception
  221. {
  222. DatagramSocket ds =new DatagramSocket();
  223. byte[] rec= new byte[1024];
  224. byte[] send = new byte[1024];
  225. System.out.println ("Enter the number to be squared");
  226. BufferedReaderbr = new BufferedReader (new InputStreamReader (System.in));
  227. intnum = Integer.parseInt(br.readLine());
  228. String data = String.valueOf (num);
  229. send = br.readLine().getBytes();
  230. InetAddressip = InetAddress.getLocalHost();
  231. DatagramPacketsenddp = new DatagramPacket (send ,send.length, ip ,1111);
  232. ds.send (senddp);
  233. DatagramPacketrecdp = new DatagramPacket (rec, rec.length);
  234. ds.receive (recdp);
  235.  
  236.  
  237. String str= new String (recdp.getData());
  238. int result = Integer.parseInt (str.trim());
  239. System.out.println ("Square of the number is :" + str); ds.close();
  240.  
  241. }
  242. }
  243.  
  244.  
  245. Output:
  246.  
  247. Practical 2D: A Client Server based program to convert the entered string to Uppercase UDPServer.java
  248. Code:
  249.  
  250. import java.io.*;
  251. import java.net.*;
  252. importjava.util.*;
  253.  
  254. public class UDPServerUcase
  255. {
  256. public static void main(String args[]) throws Exception {
  257. DatagramSocket ds =new DatagramSocket(1111); byte[] rec= new byte[1024];
  258. byte[] send = new byte[1024]; while (true)
  259. {
  260. DatagramPacketrecdp = new DatagramPacket (rec, rec.length);
  261. ds.receive (recdp);
  262. String str= new String (recdp.getData());
  263. System.out.println ("From Client :" + str);
  264. String str1 = (str.toUpperCase().trim());
  265. InetAddressip =recdp.getAddress();
  266. int port = recdp.getPort();
  267. send = str1.getBytes();
  268. DatagramPacketsenddp=new DatagramPacket (send,send.length,ip,port);
  269. ds.send(senddp);
  270. }
  271. }
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. 2. UDPClient.java Code:
  280. import java.io.*;
  281. import java.net.*;
  282. importjava.util.*;
  283. public class UDPClientUcase
  284. {
  285. public static void main(String args[]) throws Exception {
  286. DatagramSocket ds =new DatagramSocket(); byte[] rec= new byte[1024];
  287. byte[] send = new byte[1024];
  288. System.out.println ("Enter the string to be converted");
  289. BufferedReaderbr = new BufferedReader (new InputStreamReader(System.in));
  290.  
  291. send = br.readLine().getBytes();
  292. InetAddressip = InetAddress.getLocalHost();
  293. DatagramPacketsenddp = new DatagramPacket (send ,send.length, ip,1111);
  294.  
  295. ds.send (senddp);
  296. DatagramPacketrecdp = new DatagramPacket (rec, rec.length);
  297. ds.receive (recdp);
  298. String str= new String (recdp.getData());
  299. System.out.println ("String in uppercase is :" + str.trim());
  300. ds.close();
  301. }
  302. }
  303.  
  304. Output
  305.  
  306.  
  307.  
  308. Practical 2E:A Multicast Socket Example
  309.  
  310. Code:
  311. 1. BroadcastServer.java
  312. import java.io.*; import java.net.*; importjava.util.*;
  313. public class BroadcastServer
  314. {
  315. public static final int PORT=1234;
  316. public static void main(String args[])throws Exception
  317. {
  318. MulticastSocket socket; DatagramPacket packet; InetAddressaddr;
  319. addr=InetAddress.getByName("224.0.0.1"); socket=new MulticastSocket(); socket.joinGroup(addr);
  320. byte[] data=null;
  321. for(;;)
  322.  
  323. {
  324. Thread.sleep(1000);
  325. System.out.println("Sending");
  326. String str=("This is Sagar Calling");
  327. data=str.getBytes();
  328. packet=new DatagramPacket(data,str.length(),addr,PORT);
  329. socket.send(packet);
  330.  
  331. }
  332. }
  333. }
  334.  
  335. 2. Broadcast Client
  336.  
  337. Code:
  338. import java.io.*; import java.net.*; importjava.util.*;
  339. public class BroadcastClient
  340. {
  341. public static final int PORT=1234;
  342. public static void main(String args[])throws Exception
  343. {
  344. MulticastSocket socket; DatagramPacket packet;
  345.  
  346.  
  347. InetAddressaddr; addr=InetAddress.getByName("224.0.0.1"); socket=new MulticastSocket(PORT); socket.joinGroup(addr);
  348. byte[] data=new byte[100];
  349. packet=new DatagramPacket(data,data.length); for(;;)
  350.  
  351.  
  352.  
  353.  
  354. {
  355.  
  356.  
  357.  
  358. }
  359. }
  360.  
  361.  
  362. socket.receive(packet);
  363. String str=new String(packet.getData()); System.out.println("Message " + packet.getAddress() + "Messageis:"+str);
  364.  
  365. }
  366.  
  367. Output
  368.  
  369.  
  370.  
  371. Practical No.3
  372. Aim: Write a program to show the object communication using RMI.
  373.  
  374. Practical 3A: A RMI based application program to display current date and time.
  375.  
  376. Code:Prac2i.java
  377.  
  378. importjava.rmi.*;
  379. public interface prac2i extends Remote
  380. {
  381. public String showdate() throws RemoteException;
  382. public String showtime() throws RemoteException;
  383. }
  384.  
  385. Code:Prac2s.java
  386.  
  387. importjava.rmi.*;
  388. importjava.rmi.server.*;
  389. importjava.util.*;
  390. public class prac2s extends UnicastRemoteObject implements prac2i {
  391. public prac2s() throws RemoteException
  392. {
  393. System.out.println ("Sever Created");
  394. }
  395. public String showdate() throws RemoteException {
  396. Date d = new Date(); returnd.toString();
  397. }
  398. public String showtime() throws RemoteException
  399. {
  400. Date d = new Date();
  401. String s = d.getHours() +":" + d.getMinutes() + ":" + d.getSeconds(); return s;
  402. }
  403. public static void main (String args[])
  404. {
  405. try
  406. {
  407. prac2sps = new prac2s(); Naming.rebind("xyz", ps);
  408. }
  409. catch (Exception e)
  410. {}
  411. }
  412. }
  413.  
  414.  
  415. Prac2c.java
  416. importjava.rmi.*;
  417.  
  418. public class prac2c
  419. {
  420. public static void main(String args[])
  421. {
  422. try
  423. {
  424. prac2i p1=(prac2i) Naming.lookup("xyz");
  425. System.out.println (p1.showdate());
  426. System.out.println(p1.showtime());
  427. }
  428. catch (Exception e)
  429. {
  430.  
  431. e.printStackTrace();
  432. }
  433. }
  434. }
  435.  
  436.  
  437. Output:
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447. Practical 3B: A RMI based application program that illustrates calculator
  448.  
  449. Code: cali.java
  450.  
  451. import java.rmi.*;
  452.  
  453. public interface cali extends Remote
  454. {
  455. publicint sum (int a, int b) throws RemoteException;
  456. publicint sub (int a , int b ) throws RemoteException;
  457. publicintmul (int a , int b) throws RemoteException;
  458. }
  459.  
  460. cals.java
  461.  
  462. import java.rmi.*;
  463. import java.rmi.server.*;
  464.  
  465. public class cals extends UnicastRemoteObject implements cali
  466. {
  467. public cals() throws RemoteException
  468. {
  469. System.out.println ("Server is Initiated");
  470. }
  471. public int sum (int first , int second) throws RemoteException
  472. {
  473. return first + second ;
  474. }
  475. public int sub (int first , int second) throws RemoteException
  476. {
  477. return first - second ;
  478. }
  479. public int mul (int first , int second) throws RemoteException
  480. {
  481. return first * second ;
  482. }
  483. public static void main( String args[])
  484. {
  485. try
  486. {
  487. cals cs = new cals(); Naming.rebind ("abc", cs);
  488. }
  489.  
  490.  
  491. catch (Exception e)
  492. {
  493. System.out.println("Exception occured :" + e.getMessage());
  494. }
  495. }
  496. }
  497.  
  498. calc.java
  499.  
  500. importjava.rmi.*;
  501. import java.io.*;
  502.  
  503. public class calc
  504. {
  505. public static void main(String args[])
  506. {
  507.  
  508. try
  509. {
  510.  
  511.  
  512.  
  513. cali c = (cali) Naming.lookup("abc"); BufferedReaderbr = new BufferedReader(new
  514.  
  515. InputStreamReader(System.in));
  516. System.out.println(" Enter the first number");
  517. String str1=br.readLine();
  518. System.out.println(" Enter the second number");
  519. String str2=br.readLine();
  520. System.out.println ("Sum of two numbers is : " +
  521. c.sum(Integer.parseInt(str1) , Integer.parseInt(str2)));
  522. System.out.println ("Difference of two numbers is : " +
  523. c.sub(Integer.parseInt(str1) ,Integer.parseInt(str2)));
  524. System.out.println ("Multiplication of two numbers is : " + c.mul(Integer.parseInt(str1) , Integer.parseInt(str2)));
  525. }
  526. catch (Exception e)
  527. {
  528. System.out.println("Exception occured : " + e.getMessage());
  529. }
  530. }
  531. }
  532.  
  533.  
  534. Output:
  535.  
  536.  
  537.  
  538. Practical No.4
  539.  
  540. Aim: Show the implementation of Remote Procedure Call.
  541.  
  542. Practical 4A: A program to implement simple calculator operations like addition, subtraction, multiplication and division.
  543.  
  544. Code:
  545.  
  546. 1.RPCServer.java
  547. importjava.util.*;
  548. import java.net.*;
  549. import java.io.*;
  550.  
  551. classRPCServer
  552. {
  553. DatagramSocket ds;
  554. DatagramPacketdp;
  555. String str, methodname , result; int val1 , val2;
  556. RPCServer()
  557. {
  558. try
  559. {
  560. ds= new DatagramSocket(1200);
  561. byte b[]= new byte[1024];
  562. while (true)
  563. {
  564. dp = new DatagramPacket (b , b.length);
  565. ds.receive (dp);
  566. str = new String (dp.getData(),0,dp.getLength());
  567. if (str.equalsIgnoreCase("q"))
  568. {
  569. System.exit (1);
  570.  
  571. }
  572. else
  573. {
  574.  
  575.  
  576.  
  577.  
  578.  
  579. StringTokenizerst = new StringTokenizer(str," ");
  580.  
  581. int i =0;
  582. while (st.hasMoreTokens())
  583. {
  584. String token = st.nextToken();
  585. methodname =token;
  586. val1 = Integer.parseInt (st.nextToken());
  587. val2 = Integer.parseInt (st.nextToken());
  588. }//while token
  589.  
  590.  
  591. } //else System.out.println(str);
  592. InetAddressia = InetAddress.getLocalHost();
  593. if (methodname.equalsIgnoreCase("add"))
  594. {
  595. result = "" + add(val1,val2);
  596. }
  597.  
  598. else if (methodname.equalsIgnoreCase("sub"))
  599. {
  600. result = "" + sub(val1,val2);
  601. }
  602. else if (methodname.equalsIgnoreCase("mul"))
  603. {
  604. result = "" + mul(val1,val2);
  605. }
  606. else if (methodname.equalsIgnoreCase("div"))
  607. {
  608. result = "" + div(val1,val2);
  609. }
  610. byte b1[]=result.getBytes();
  611. DatagramSocket ds1= new DatagramSocket();
  612. DatagramPacket dp1 = new DatagramPacket (b1,b1.length,InetAddress.getLocalHost(),1300);
  613. System.out.println("result: "+ result +"\n"); ds1.send (dp1);
  614. } //while
  615. } //try
  616. catch (Exception e)
  617. {
  618. e.printStackTrace();
  619. }
  620. } //constructor
  621.  
  622. publicint add (int val1 , int val2)
  623. {
  624. return val1+val2;
  625. }
  626.  
  627. publicint sub(int val1 ,int val2)
  628. {
  629. return val1-val2;
  630. }
  631.  
  632. publicintmul (int val1 ,int val2)
  633. {
  634.  
  635.  
  636. return val1*val2;
  637. }
  638.  
  639. publicint div (int val1 ,int val2)
  640. {
  641. return val1/val2;
  642. }
  643.  
  644. public static void main (String[] args)
  645. {
  646. newRPCServer();
  647. }
  648. } //class
  649.  
  650. 2. RPCClient.java
  651.  
  652. import java.io.*; import java.net.*;
  653.  
  654. classRPCClient
  655. {
  656. RPCClient()
  657. {
  658. try
  659. {
  660. InetAddressia=InetAddress.getLocalHost();
  661. DatagramSocket ds= new DatagramSocket();
  662. DatagramSocket ds1=new DatagramSocket(1300);
  663. System.out.println("\n RPC Client \n");
  664. System.out.println ("Enter method name and parameters ");
  665. while (true)
  666.  
  667. {
  668. BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
  669. String str=br.readLine();
  670. byte b[]= str.getBytes();
  671. DatagramPacketdp = new DatagramPacket (b ,b.length , ia, 1200);
  672. ds.send(dp);
  673. dp= new DatagramPacket (b, b.length);
  674. ds1.receive(dp);
  675. String s = new String (dp.getData(),0,dp.getLength());
  676. System.out.println("\n Result = " + s + "\n");
  677. } //while
  678. }//try
  679. catch (Exception e)
  680. {
  681. e.printStackTrace();
  682.  
  683.  
  684. } //catch
  685. }//constructor
  686. public static void main( String args[])
  687. {
  688. newRPCClient();
  689. }
  690. }
  691. Output:
  692.  
  693.  
  694.  
  695.  
  696.  
  697. Practical No.5
  698.  
  699. Aim: Write a program to execute any one mutual exclusion algorithm.
  700.  
  701.  
  702. Code:
  703.  
  704.  
  705. EchoClientOne.java
  706.  
  707. import java.io.*;
  708. import java.net.*;
  709.  
  710. public class EchoClientOne
  711. {
  712.  
  713. public static void main(String ards[]) throws IOException
  714. {
  715. Socket s = new Socket("localhost",7000);
  716. PrintStream out = new PrintStream (s.getOutputStream());
  717.  
  718. ServerSocketss = new ServerSocket(7001); Socket s1 = ss.accept();
  719. BufferedReader in1= new BufferedReader(new InputStreamReader(s1.getInputStream()));
  720. PrintStream out1 = new PrintStream (s1.getOutputStream());
  721. BufferedReaderbr = new BufferedReader (new InputStreamReader(System.in));
  722. String str = "Token";
  723. while (true)
  724. {
  725. if (str.equalsIgnoreCase("Token"))
  726. {
  727. System.out.println("Do you want to send some data");
  728. System.out.println("Enter yes or no");
  729. str = br.readLine();
  730. if (str.equalsIgnoreCase("Yes"))
  731. {
  732. System.out.println("Enter the data");
  733. str = br.readLine();
  734. out.println(str);
  735. }
  736. out1.println ("Token");
  737. }
  738. System.out.println("Waiting for token");
  739. str = in1.readLine();
  740. }
  741. }
  742. }
  743.  
  744.  
  745.  
  746. EchoClientTwo.java
  747.  
  748. import java.io.*;
  749. import java.net.*;
  750.  
  751. public class EchoClientTwo
  752. {
  753. public static void main(String ards[]) throws IOException
  754. {
  755. Socket s = new Socket("localhost",7000);
  756. PrintStream out = new PrintStream (s.getOutputStream());
  757. Socket s2 = new Socket("localhost",7001);
  758. BufferedReader in2= new BufferedReader(new InputStreamReader(s2.getInputStream()));
  759. PrintStream out2= new PrintStream (s2.getOutputStream());
  760.  
  761. BufferedReaderbr = new BufferedReader (new InputStreamReader(System.in));
  762. String str="Token";
  763.  
  764. while (true)
  765. {
  766. System.out.println("Waiting for token");
  767. }
  768. str = in2.readLine();
  769. if (str.equalsIgnoreCase("Token"))
  770. {
  771. System.out.println("Do you want to send some data");
  772. System.out.println("Enter yes or no");
  773. str = br.readLine();
  774.  
  775. if (str.equalsIgnoreCase("Yes"))
  776. {
  777. System.out.println("Enter the data"); str = br.readLine();
  778. out.println(str);
  779. }
  780. out2.println ("Token");
  781. }
  782. }
  783. }
  784. }
  785.  
  786.  
  787.  
  788. EchoServer.java
  789.  
  790. import java.io.*;
  791. import java.net.*;
  792.  
  793. public class EchoServer implements Runnable
  794. {
  795. Socket socket = null;
  796. staticServerSocketss;
  797. EchoServer( SocketnewSocket)
  798. {
  799. this.socket = newSocket;
  800. }
  801.  
  802. public static void main (String args[]) throws IOException
  803. {
  804. ss = new ServerSocket(7000);
  805. System.out.println ("Server Started");
  806. while (true)
  807. {
  808. Socket s=ss.accept();
  809.  
  810. EchoServeres = new EchoServer(s);
  811. Thread t = new Thread(es);
  812. t.start();
  813. }
  814. }
  815.  
  816. public void run()
  817. {
  818. try
  819. {
  820. BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  821. while(true)
  822. {
  823. System.out.println (in.readLine());
  824. }
  825. }
  826. catch (Exception e)
  827. {}
  828. }
  829. }
  830.  
  831.  
  832. Output:
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839. Practical No.6
  840. Aim: Show the implementation of any one clock synchronization algorithm.
  841.  
  842. Code:
  843.  
  844. 1. SCServer.java
  845.  
  846. import java.io.*;
  847. import java.net.*;
  848. importjava.sql.*;
  849. public class SCServer
  850. {
  851. public static void main(String args[])throws Exception
  852. {
  853. InetAddresslclhost;
  854. loclhost=InetAddress.getLocalHost();
  855. longmaxtime,skewtime,datatime;
  856. String maxtimestr,skewtimestr;
  857. BufferedReaderbr;
  858. ClntServerser=new ClntServer(lclhost);
  859. System.out.println("Enter the maximum time");
  860. br = new BufferedReader(new
  861. InputStreamReader(System.in));
  862. maxtimestr=br.readLine();
  863.  
  864. System.out.println("Enter the maximum skew time");
  865. br = new BufferedReader(new InputStreamReader(System.in));
  866. skewtimestr=br.readLine();
  867. maxtime=Long.parseLong(maxtimestr); s
  868. kewtime=Long.parseLong(skewtimestr);
  869. while(true)
  870. {
  871. datatime =System.currentTimeMillis();
  872. long G = datatime-maxtime-skewtime;
  873. System.out.println("G ="+G);
  874. ser.setTimeStamp(new Timestamp(G));
  875. ser.recPort(8001);
  876. ser.recData();
  877. }
  878. }
  879. }
  880. classClntServer
  881. {
  882. InetAddresslclhost;
  883. intrecport;
  884. Timestamp obtmp;
  885.  
  886.  
  887. ClntServer(InetAddresslclhost)
  888. {
  889. this.lclhost = lclhost;
  890. }
  891. voidrecPort(intrecport)
  892. {
  893. this.recport = recport;
  894. }
  895. voidsetTimeStamp(Timestamp obtmp)
  896. {
  897. this.obtmp = obtmp;
  898. }
  899. voidrecData()throws Exception
  900. {
  901. String msgstr="";
  902. DatagramSocket ds;
  903. DatagramPacketdp;
  904. BufferedReaderbr;
  905. bytebuf[] = new byte[256];
  906. ds = new DatagramSocket(recport);
  907. dp = new DatagramPacket(buf,buf.length);
  908.  
  909. ds.receive(dp);
  910. ds.close();
  911. msgstr = new String(dp.getData(),0,dp.getLength());
  912. System.out.println(msgstr);
  913. Timestamp obtmp = new Timestamp(Long.parseLong(msgstr));
  914. if(this.obtmp.before(obtmp) == true)
  915. {
  916. System.out.println("The Message is accepted");
  917. }
  918. else
  919. {
  920. System.out.println("The Message is rejected");
  921. }
  922. }
  923. }
  924.  
  925.  
  926. 3. SCClient.java
  927.  
  928. import java.io.*;
  929. import java.net.*;
  930. public class SCClient
  931. {
  932. public static void main(String args[]) throws Exception
  933. {
  934. InetAddresslclhost;
  935. loclhost = InetAddress.getLocalHost();
  936.  
  937. while (true)
  938. {
  939. Client cntl = new Client(lclhost);
  940. cntl.sendPort(9001);
  941. cntl.sendData();
  942. }
  943. }
  944. }
  945. class Client
  946. {
  947. InetAddresslclhost; intsenport;
  948. Client (InetAddresslclhost)
  949. {
  950. this.lclhost = loclhost;
  951. }
  952. voidsendPort (intsenport)
  953. {
  954. this.senport = senport;
  955. }
  956. voidsendData() throws Exception
  957. {
  958. DatagramPacketdp;
  959. DatagramSocket ds;
  960. BufferedReaderbr;
  961. br = new BufferedReader (new InputStreamReader(System.in));
  962. System.out.println ("Enter The Data");
  963. String str = br.readLine();
  964. ds = new DatagramSocket (senport);
  965. dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,senport-1000);
  966. ds.send(dp);
  967. ds.close();
  968. }
  969. }
  970.  
  971.  
  972. Output:
  973.  
  974.  
  975.  
  976. Practical No.7
  977. Aim: Write a program to implement two phase commit protocol.
  978.  
  979. Code:
  980.  
  981. 1. Server.java
  982.  
  983. import java.io.*;
  984. import java.net.*;
  985. importjava.util.*;
  986.  
  987. public class Server
  988. {
  989. boolean closed=false,inputFromAll=false;
  990. List<clientThread> t;
  991. List<String> data;
  992.  
  993. Server()
  994. {
  995. t= new ArrayList<clientThread>();
  996. data= new ArrayList<String>();
  997. }
  998. public static void main(String args[])
  999. {
  1000. Socket clientSocket = null;
  1001. ServerSocketserverSocket = null;
  1002. intport_number=1111;
  1003.  
  1004. Server ser=new Server();
  1005.  
  1006.  
  1007. try
  1008. {
  1009. serverSocket = new ServerSocket(port_number);
  1010. }
  1011. catch (IOException e)
  1012. {
  1013. System.out.println(e);
  1014. }
  1015. while(!ser.closed)
  1016. {
  1017. try
  1018. {
  1019. clientSocket = serverSocket.accept();
  1020. clientThreadth=new clientThread(ser,clientSocket);
  1021. (ser.t).add(th);
  1022. System.out.println("\nNow Total clients are : "+(ser.t).size());
  1023. (ser.data).add("NOT_SENT");
  1024. th.start();
  1025. }
  1026.  
  1027. catch (IOException e) {}
  1028. }
  1029.  
  1030.  
  1031. try
  1032. {
  1033. serverSocket.close();
  1034. }
  1035. catch(Exception e1){}
  1036. }
  1037. }
  1038. classclientThread extends Thread
  1039. {
  1040. DataInputStream is = null;
  1041. String line;
  1042. String destClient="";
  1043. String name;
  1044. PrintStreamos = null;
  1045. Socket clientSocket = null;
  1046. String clientIdentity;
  1047. Server ser;
  1048.  
  1049. publicclientThread(Server ser,SocketclientSocket)
  1050. {
  1051. this.clientSocket=clientSocket;
  1052. this.ser=ser;
  1053. }
  1054. public void run()
  1055. {
  1056. try
  1057. {
  1058. is = new DataInputStream(clientSocket.getInputStream());
  1059. os = new PrintStream(clientSocket.getOutputStream());
  1060.  
  1061. os.println("Enter your name.");
  1062. name = is.readLine();
  1063. clientIdentity=name;
  1064.  
  1065. os.println("Welcome "+name+" to this 2 Phase Application.\nYou will receive a vote Request now...");
  1066. os.println("VOTE_REQUEST\nPlease enter COMMIT or ABORT to proceed : ");
  1067.  
  1068. for(int i=0; i<(ser.t).size(); i++)
  1069. {
  1070. if((ser.t).get(i)!=this)
  1071. {
  1072. ((ser.t).get(i)).os.println("---A new user "+name+" entered the Appilcation---");
  1073. }
  1074. }
  1075.  
  1076. while (true)
  1077. {
  1078. line = is.readLine();
  1079.  
  1080. if(line.equalsIgnoreCase("ABORT"))
  1081. {
  1082. System.out.println("\nFrom '"+clientIdentity+"' : ABORT\n\nSince aborted we will not wait for inputs from other clients.");
  1083. System.out.println("\nAborted....");
  1084.  
  1085. for(int i=0; i<(ser.t).size(); i++)
  1086. {
  1087. ((ser.t).get(i)).os.println("GLOBAL_ABORT");
  1088. ((ser.t).get(i)).os.close();
  1089. ((ser.t).get(i)).is.close();
  1090. }
  1091. break;
  1092. }
  1093.  
  1094.  
  1095. if(line.equalsIgnoreCase("COMMIT"))
  1096. {
  1097. System.out.println("\nFrom '"+clientIdentity+"' : COMMIT");
  1098.  
  1099. if((ser.t).contains(this))
  1100. {
  1101. (ser.data).set((ser.t).indexOf(this), "COMMIT"); for(int j=0;j<(ser.data).size();j++)
  1102. {
  1103. if(!(((ser.data).get(j)).equalsIgnoreCase("NOT_SENT")))
  1104. {
  1105. ser.inputFromAll=true;
  1106. continue;
  1107. }
  1108. else
  1109.  
  1110. {
  1111.  
  1112.  
  1113. ser.inputFromAll=false;
  1114. System.out.println("\nWaiting for inputs from other clients."); break;
  1115.  
  1116. }
  1117. }
  1118.  
  1119. if(ser.inputFromAll)
  1120. {
  1121.  
  1122.  
  1123.  
  1124. System.out.println("\n\nCommited....");
  1125.  
  1126. for(int i=0; i<(ser.t).size(); i++)
  1127. {
  1128. ((ser.t).get(i)).os.println("GLOBAL_COMMIT");
  1129. ((ser.t).get(i)).os.close();
  1130. ((ser.t).get(i)).is.close();
  1131. }
  1132. break;
  1133. }
  1134.  
  1135. }//if t.contains
  1136. }//commit
  1137.  
  1138. }//while
  1139.  
  1140. ser.closed=true;
  1141. clientSocket.close();
  1142. }
  1143.  
  1144. catch(IOException e){};
  1145. }
  1146. }
  1147.  
  1148.  
  1149. 2. Client.java
  1150.  
  1151. import java.io.*;
  1152. import java.net.*;
  1153.  
  1154. public class Client implements Runnable
  1155. {
  1156. static Socket clientSocket = null;
  1157. staticPrintStreamos = null;
  1158. staticDataInputStream is = null;
  1159. staticBufferedReaderinputLine = null;
  1160.  
  1161.  
  1162. staticboolean closed = false;
  1163.  
  1164.  
  1165. public static void main(String[] args)
  1166. {
  1167. intport_number=1111;
  1168. String host="localhost";
  1169.  
  1170. try
  1171. {
  1172. clientSocket = new Socket(host, port_number);
  1173. inputLine = new BufferedReader(new InputStreamReader(System.in));
  1174. os= new PrintStream(clientSocket.getOutputStream());
  1175. is = new DataInputStream(clientSocket.getInputStream());
  1176. }
  1177. catch (Exception e)
  1178. {
  1179. System.out.println("Exception occurred : "+e.getMessage());
  1180. }
  1181.  
  1182. if (clientSocket != null &&os != null && is != null)
  1183. {
  1184. try
  1185. {
  1186. new Thread(new Client()).start();
  1187. while (!closed)
  1188. {
  1189. os.println(inputLine.readLine());
  1190. }
  1191.  
  1192. os.close();
  1193. is.close();
  1194. clientSocket.close();
  1195.  
  1196. }
  1197.  
  1198. catch (IOException e)
  1199. {
  1200. System.err.println("IOException: " + e);
  1201. }
  1202. }
  1203. }
  1204.  
  1205. public void run()
  1206. {
  1207. String responseLine;
  1208. try
  1209. {
  1210. while ((responseLine = is.readLine()) != null)
  1211. {
  1212.  
  1213. System.out.println("\n"+responseLine);
  1214.  
  1215. if (responseLine.equalsIgnoreCase("GLOBAL_COMMIT")==true
  1216. || responseLine.equalsIgnoreCase("GLOBAL_ABORT")==true )
  1217. {
  1218. break;
  1219. }
  1220. }
  1221.  
  1222. closed=true;
  1223. }
  1224.  
  1225. catch (IOException e)
  1226. {
  1227. System.err.println("IOException: " + e);
  1228. }
  1229. }
  1230. }
  1231.  
  1232. OUTPUT:
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239. Practical No.8
  1240. Aim: Implement the concept of distributed file system architecture.
  1241.  
  1242. Network File System: Server Side Configuration
  1243. Set ip address of server 192.159.1.27
  1244. Check if nfs is installed on your machine.
  1245.  
  1246. rpm –qa | grep nfs
  1247. #set iptables off
  1248. #set firewall off
  1249. #set vsftpd off
  1250. #set imapd off
  1251. #service nfs start
  1252. #pwd
  1253. /root
  1254.  
  1255. #mkdir mscitp1PPb
  1256. #cd mscitp1PPb #pwd
  1257. /root/mscitp1ppb #vi ved
  1258. Type something and save
  1259. #vi monu
  1260.  
  1261. Type something and save
  1262. #vi lochu
  1263.  
  1264. Type something and save
  1265. #ls
  1266.  
  1267. ved monu lochu
  1268. #vi /etc/exports
  1269.  
  1270.  
  1271.  
  1272. Add the following
  1273. /root/mscitp1ppb(press atab key) 192.159.1.28(rw,sync)
  1274.  
  1275. Save and exit
  1276. #showmount -e
  1277. #service nfs restart
  1278.  
  1279.  
  1280. Client Side Configuration:
  1281.  
  1282. Set ip address of server 192.159.1.28
  1283.  
  1284. #service iptables stop
  1285. #service ftpd stop #pwd
  1286. /root
  1287.  
  1288. #mkdir ppbstuff
  1289.  
  1290. #mount –t nfs 192.159.1.27:/root/mscitp1ppb /root/ppbstuff
  1291.  
  1292. #cd ppbstuff
  1293. #cat ved
  1294. #cat monu
  1295. #cat lochu
  1296.  
  1297. Conclusion:
  1298. Distributed file system is implemented.
Add Comment
Please, Sign In to add comment