Advertisement
Guest User

ds

a guest
Jan 27th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 68.94 KB | None | 0 0
  1. Distributed Systems 2018-2019
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. M.Sc. I.T. Part I
  18.  
  19.  
  20.  
  21.  
  22.  
  23. Semester I
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. DISTRIBUTED SYSTEM
  41.  
  42.  
  43.  
  44.  
  45. PRACTICAL
  46. 2018 – 2019
  47.  
  48.  
  49.  
  50. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 1
  51.  
  52.  
  53.  
  54. Distributed Systems 2018-2019
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. Vidyalankar School of Information Technology
  69. Wadala (East), Mumbai – 400037
  70.  
  71.  
  72.  
  73.  
  74. CERTIFICATE
  75.  
  76.  
  77.  
  78. This is to certify that, Mr. MD Taz Saheb Seat Number ______________ studying in Master of Science in Information Technology Part-I (Semester-I) has satisfactorily completed the Practical in the subject of “DISTRIBUTED SYSTEM” as prescribed by University of Mumbai, during the academic year 2018-2019.
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. Subject Faculty Coordinator, M.Sc. (IT)
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. External Examiner
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 2
  106.  
  107.  
  108.  
  109. Distributed Systems 2018-2019
  110.  
  111. INDEX
  112.  
  113.  
  114.  
  115.  
  116.  
  117. Sr.
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. Title
  125.  
  126.  
  127.  
  128.  
  129. Date
  130.  
  131.  
  132. Page
  133. Signature
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. No.
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. No.
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. 1.
  229.  
  230.  
  231.  
  232.  
  233. Implement the concept for sharing
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. the resources using distributed
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325. system.
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365. 2.
  366.  
  367.  
  368.  
  369.  
  370. Write a program for implementing
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403. Client Server communication model.
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456. 3.
  457.  
  458.  
  459.  
  460.  
  461. Write a program to show the object
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494. communication using RMI.
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547. 4.
  548.  
  549.  
  550.  
  551.  
  552. Show the implementation of Remote
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585. Procedure Call.
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638. 5.
  639.  
  640.  
  641.  
  642.  
  643. Show the implementation of Web
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676. Services.
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729. 6.
  730.  
  731.  
  732.  
  733.  
  734. Write a program to execute any one
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767. mutual exclusion algorithm.
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820. 7.
  821.  
  822.  
  823.  
  824.  
  825. Write a program to implement any
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858. one election algorithm.
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915. 8.
  916.  
  917.  
  918.  
  919.  
  920. Show the implementation of any one
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985. clock synchronization algorithm.
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042. 9.
  1043.  
  1044.  
  1045.  
  1046.  
  1047. Implement the concept of distributed
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112. file system architecture.
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 3
  1186.  
  1187.  
  1188.  
  1189. Distributed Systems 2018-2019
  1190.  
  1191. ________________________________________________________
  1192.  
  1193. ________________________________________________________
  1194.  
  1195. ________________________________________________________
  1196.  
  1197. ________________________________________________________
  1198.  
  1199. ________________________________________________________
  1200.  
  1201. ________________________________________________________
  1202.  
  1203. ________________________________________________________
  1204.  
  1205. ________________________________________________________
  1206.  
  1207. ________________________________________________________
  1208.  
  1209. ________________________________________________________
  1210.  
  1211. ________________________________________________________
  1212.  
  1213. ________________________________________________________
  1214.  
  1215. ________________________________________________________
  1216.  
  1217. ________________________________________________________
  1218.  
  1219. ________________________________________________________
  1220.  
  1221. ________________________________________________________
  1222.  
  1223. ________________________________________________________
  1224.  
  1225. ________________________________________________________
  1226.  
  1227. ________________________________________________________
  1228.  
  1229. ________________________________________________________
  1230.  
  1231. ________________________________________________________
  1232.  
  1233. ________________________________________________________
  1234.  
  1235. ________________________________________________________
  1236.  
  1237. ________________________________________________________
  1238.  
  1239. ________________________________________________________
  1240.  
  1241. ________________________________________________________
  1242.  
  1243. ________________________________________________________
  1244.  
  1245. ________________________________________________________
  1246.  
  1247. ________________________________________________________
  1248.  
  1249. ________________________________________________________
  1250.  
  1251. _______________________________________
  1252.  
  1253. Distributed Systems 2018-2019
  1254.  
  1255. Practical No: 01
  1256.  
  1257. Aim: Implement the concept for sharing the resources using distributed system.
  1258.  
  1259. Create a workgroup myworkgroup on windows machine and share a folder.
  1260. If samba package is not installed on your linux machine then install it first.
  1261.  
  1262. #vi /etc/samba/smb.conf
  1263. Modify the file accordingly
  1264. Workgroup = MYGroup
  1265.  
  1266. Server String = Samba Server Version %v
  1267. Netbios name = MYSERVER
  1268. interface = lo eth0 192.154.2.10/24
  1269.  
  1270. host allow 127. 192.154.12.
  1271.  
  1272. Remove the semicolon at the start of the above lines if any.
  1273. Go to end and add
  1274. [common]
  1275.  
  1276. Comment=shared folder
  1277. Path = /common
  1278. Public =yes
  1279. Writable = yes
  1280. Browsable yes
  1281. Save the file
  1282. #useradd mona
  1283. #smbpasswd –a mona
  1284. Will ask for password
  1285. Enter the password
  1286. Retype the password
  1287. #/etc/init.d/smb restart
  1288. #chkconfig cmb on
  1289. #testparm
  1290. #setenforce 0(zero)
  1291. #setsebool samba_enable_home+dirs=1
  1292. #getsebool –a | less
  1293.  
  1294. The ip address in the following command is the ip address of windows machine. #smbclient //192.154.2.10/common –U mona
  1295.  
  1296. Enter password
  1297.  
  1298. Smb>mkdir ppbrdr
  1299. Will create ppbrdr directory in shared folder
  1300. Smb>get rdr.txt
  1301. Read the file rdr.txt
  1302. Smb>put ppb.txt
  1303. Will create a file named ppb.txt
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 5
  1312.  
  1313.  
  1314.  
  1315. Distributed Systems 2018-2019
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 6
  1386.  
  1387.  
  1388.  
  1389. Distributed Systems 2018-2019
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450. Conclusion: Resources are shared between Windows and Linux.
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 7
  1459.  
  1460.  
  1461.  
  1462. Distributed Systems 2018-2019
  1463.  
  1464. ________________________________________________________
  1465.  
  1466. ________________________________________________________
  1467.  
  1468. ________________________________________________________
  1469.  
  1470. ________________________________________________________
  1471.  
  1472. ________________________________________________________
  1473.  
  1474. ________________________________________________________
  1475.  
  1476. ________________________________________________________
  1477.  
  1478. ________________________________________________________
  1479.  
  1480. ________________________________________________________
  1481.  
  1482. ________________________________________________________
  1483.  
  1484. ________________________________________________________
  1485.  
  1486. ________________________________________________________
  1487.  
  1488. ________________________________________________________
  1489.  
  1490. ________________________________________________________
  1491.  
  1492. ________________________________________________________
  1493.  
  1494. ________________________________________________________
  1495.  
  1496. ________________________________________________________
  1497.  
  1498. ________________________________________________________
  1499. ________________________________________________________
  1500. ________________________________________________________
  1501.  
  1502. ________________________________________________________
  1503.  
  1504. ________________________________________________________
  1505. ________________________________________________________
  1506. ________________________________________________________
  1507.  
  1508. ________________________________________________________
  1509. ________________________________________________________
  1510. ________________________________________________________
  1511.  
  1512. ________________________________________________________
  1513. ________________________________________________________
  1514. ________________________________________________________
  1515.  
  1516. ________________________________________________________
  1517. ________________________________________________________
  1518.  
  1519.  
  1520.  
  1521. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 8
  1522.  
  1523.  
  1524.  
  1525. Distributed Systems 2018-2019
  1526.  
  1527. Practical No: 02
  1528.  
  1529. Aim: Write a program for implementing Client Server communication model.
  1530.  
  1531. A client server based program using TCP
  1532.  
  1533. TCPClient.java import java.net.*; import java.io.*; import java.util.*;
  1534.  
  1535. class TCPClient
  1536. {
  1537. public static void main(String args[ ] ) throws Exception
  1538. {
  1539.  
  1540. Socket s = new Socket( "127.0.0.1", 2010 ); Scanner kbin = new Scanner( System.in );
  1541.  
  1542. DataInputStream in = new DataInputStream(s.getInputStream( )); DataOutputStream out = new DataOutputStream(s.getOutputStream( ));
  1543.  
  1544. while( true )
  1545. {
  1546.  
  1547. out.writeUTF( kbin.next ( ) ); System.out.println( "Server: " +in.readUTF( ) );
  1548. }
  1549.  
  1550.  
  1551. }
  1552.  
  1553. }
  1554.  
  1555. TCPServer.java
  1556. import java.net.*;
  1557.  
  1558. import java.io.*;
  1559.  
  1560. class TCPServer
  1561. {
  1562.  
  1563. public static void main(String args[ ] ) throws Exception {
  1564.  
  1565. ServerSocket ss = new ServerSocket( 2010 );
  1566.  
  1567. while( true )
  1568. {
  1569. Socket s = ss.accept( );
  1570.  
  1571. DataInputStream in = new DataInputStream(s.getInputStream( )); DataOutputStream out = new DataOutputStream(s.getOutputStream( ));
  1572. String msg="";
  1573.  
  1574. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 9
  1575.  
  1576.  
  1577.  
  1578. Distributed Systems 2018-2019
  1579.  
  1580. do
  1581. {
  1582. msg = in.readUTF( );
  1583. System.out.println(msg);
  1584. out.writeUTF( msg );
  1585.  
  1586. }while( ! msg.equals("quit") );
  1587. }
  1588.  
  1589.  
  1590. }
  1591.  
  1592. }
  1593.  
  1594. Output:
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 10
  1643.  
  1644.  
  1645.  
  1646. Distributed Systems 2018-2019
  1647.  
  1648.  
  1649. B) A client server based program for TCP marshalling of objects.
  1650.  
  1651. Complex.java
  1652. import java.io.*;
  1653.  
  1654. class Complex implements Serializable
  1655. {
  1656. int a , b;
  1657.  
  1658. Complex( int x, int y )
  1659. {
  1660. a = x;
  1661.  
  1662. b = y;
  1663. }
  1664.  
  1665. public String toString( )
  1666. {
  1667. return "[ "+ a +" + i " + b + " ]";
  1668. }
  1669.  
  1670. }
  1671.  
  1672. MarshallingServer.java
  1673. import java.net.*;
  1674.  
  1675. import java.io.*;
  1676.  
  1677. class MarshallingServer
  1678. {
  1679.  
  1680. public static void main(String args[ ] ) throws Exception {
  1681.  
  1682. ServerSocket ss = new ServerSocket( 2010 );
  1683.  
  1684. while( true )
  1685. {
  1686. Socket s = ss.accept( );
  1687. System.out.println("Connection established...");
  1688.  
  1689. ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream( )); ObjectInputStream in = new ObjectInputStream(s.getInputStream( ));
  1690.  
  1691.  
  1692. Complex c1 = (Complex) in.readObject();
  1693. System.out.println( "Received object " +c1 );
  1694.  
  1695. Complex c2 = (Complex) in.readObject();
  1696. System.out.println( "Received object " +c2 );
  1697.  
  1698.  
  1699. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 11
  1700.  
  1701.  
  1702.  
  1703. Distributed Systems 2018-2019
  1704.  
  1705. Complex c3 = new Complex( c1.a+c2.a , c1.b+c2.b );
  1706.  
  1707. System.out.println( "Sending object " +c3 );
  1708. out.writeObject( c3 );
  1709. out.flush();
  1710.  
  1711. s.close();
  1712. }
  1713.  
  1714.  
  1715. }
  1716.  
  1717. }
  1718.  
  1719. MarshallingClient.java
  1720.  
  1721. import java.net.*;
  1722. import java.io.*;
  1723. import java.util.*;
  1724.  
  1725. class MarshallingClient
  1726. {
  1727.  
  1728. public static void main(String args[ ] ) throws Exception {
  1729.  
  1730. Socket s = new Socket( "127.0.0.1", 2010 );
  1731. System.out.println( "connected");
  1732.  
  1733. ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream( )); ObjectInputStream in = new ObjectInputStream(s.getInputStream( ));
  1734.  
  1735.  
  1736. Complex c1 = new Complex( 3, 5 );
  1737. Complex c2 = new Complex( -1, 4 );
  1738.  
  1739.  
  1740. out.writeObject( c1 );
  1741. out.flush();
  1742. out.writeObject( c2 );
  1743.  
  1744. out.flush();
  1745. System.out.println( "Sum is : " + (Complex) in.readObject( ) );
  1746.  
  1747.  
  1748.  
  1749.  
  1750. }
  1751.  
  1752. }
  1753.  
  1754.  
  1755.  
  1756. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 12
  1757.  
  1758.  
  1759.  
  1760. Distributed Systems 2018-2019
  1761.  
  1762. Output:
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.  
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794.  
  1795.  
  1796.  
  1797.  
  1798.  
  1799.  
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811. A client server based program using UDP
  1812.  
  1813. UDPSender.java
  1814.  
  1815. import java.net.*; class UDPSender
  1816. {
  1817. public static void main(String args[]) throws Exception
  1818. {
  1819.  
  1820. InetAddress ia = InetAddress.getByName( "vsit-y101-07" ); int port = 1099;
  1821.  
  1822. String msg = " This is a great subject"; byte []buf = msg.getBytes();
  1823.  
  1824. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 13
  1825.  
  1826.  
  1827.  
  1828. Distributed Systems 2018-2019
  1829.  
  1830. DatagramPacket dp = new DatagramPacket( buf , buf.length, ia , port );
  1831.  
  1832. DatagramSocket ds = new DatagramSocket( ) ;
  1833.  
  1834. ds.send( dp );
  1835.  
  1836. System.out.println ( msg + " sent to " + ia + ":" +port );
  1837.  
  1838. ds.receive( dp );
  1839.  
  1840. String msg2 = new String( dp.getData() );
  1841.  
  1842. System.out.println ("Echo Received "+msg2.trim());
  1843.  
  1844. }
  1845. }
  1846.  
  1847. UDPReceiver.java
  1848. import java.net.*;
  1849.  
  1850. import java.text.SimpleDateFormat;
  1851. import java.util.Date;
  1852.  
  1853. class UDPReceiver {
  1854. public static void main(String args[]) throws Exception
  1855. {
  1856.  
  1857. DatagramSocket ds = new DatagramSocket( 1099 ) ; while( true ){
  1858.  
  1859. byte []buf = new byte [1000];
  1860.  
  1861. DatagramPacket dp = new DatagramPacket( buf , buf.length );
  1862.  
  1863. ds.receive( dp );
  1864.  
  1865. String msg = new String( dp.getData() );
  1866.  
  1867. Date date = new Date();
  1868.  
  1869. System.out.println ( "Received : " + msg.trim() + " from " + dp.getAddress() + ":" + dp.getPort() + " at " + date );
  1870.  
  1871. ds.send(dp);
  1872. }
  1873. }
  1874. }
  1875.  
  1876.  
  1877.  
  1878.  
  1879.  
  1880.  
  1881.  
  1882. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 14
  1883.  
  1884.  
  1885.  
  1886. Distributed Systems 2018-2019
  1887.  
  1888. Output:
  1889.  
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.  
  1910. D) A client server based program for UDP echo chat.
  1911.  
  1912. UDPClient.java
  1913. import java.net.*;
  1914.  
  1915. class UDPSender {
  1916.  
  1917. public static void main(String args[]) throws Exception {
  1918.  
  1919. InetAddress ia = InetAddress.getByName( "LAPTOP-R8KE5PAE" ); int port = 1099;
  1920.  
  1921. String msg = " This is Aditya";
  1922. byte []buf = msg.getBytes();
  1923.  
  1924. DatagramPacket dp = new DatagramPacket( buf , buf.length, ia , port ); DatagramSocket ds = new DatagramSocket( ) ;
  1925.  
  1926. ds.send( dp );
  1927.  
  1928. System.out.println ( msg + " sent to " + ia + ":" +port ); ds.receive( dp );
  1929.  
  1930. String msg2 = new String( dp.getData() ); System.out.println ("Echo Received "+msg2.trim());
  1931.  
  1932. }
  1933. }
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 15
  1951.  
  1952.  
  1953.  
  1954. Distributed Systems 2018-2019
  1955.  
  1956. UDPServer.java
  1957.  
  1958. import java.net.*;
  1959. import java.text.SimpleDateFormat;
  1960. import java.util.Date;
  1961.  
  1962. class UDPReceiver {
  1963.  
  1964. public static void main(String args[]) throws Exception
  1965. {
  1966. DatagramSocket ds = new DatagramSocket( 1099 ) ;
  1967.  
  1968. while( true ){
  1969.  
  1970. byte []buf = new byte [1000];
  1971.  
  1972. DatagramPacket dp = new DatagramPacket( buf , buf.length ); ds.receive( dp );
  1973.  
  1974. String msg = new String( dp.getData() );
  1975.  
  1976. Date date = new Date();
  1977.  
  1978. System.out.println ( "Received : " + msg.trim() + " from " + dp.getAddress() + ":" + dp.getPort() + " at " + date );
  1979.  
  1980. ds.send(dp);
  1981. }
  1982. }
  1983. }
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990.  
  1991.  
  1992.  
  1993.  
  1994.  
  1995.  
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006.  
  2007.  
  2008.  
  2009.  
  2010.  
  2011. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 16
  2012.  
  2013.  
  2014.  
  2015. Distributed Systems 2018-2019
  2016.  
  2017.  
  2018. Output:
  2019.  
  2020.  
  2021.  
  2022.  
  2023.  
  2024.  
  2025.  
  2026.  
  2027.  
  2028.  
  2029.  
  2030.  
  2031.  
  2032.  
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057.  
  2058.  
  2059.  
  2060.  
  2061.  
  2062.  
  2063.  
  2064.  
  2065.  
  2066.  
  2067.  
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 17
  2085.  
  2086.  
  2087.  
  2088. Distributed Systems 2018-2019
  2089.  
  2090.  
  2091. E) A multicast Socket program
  2092.  
  2093. MulticastSender.java
  2094. import java.net.*;
  2095.  
  2096. class MulticastSender
  2097. {
  2098.  
  2099. public static void main( String args[] )throws Exception {
  2100.  
  2101. Which port should we send to int port = 5000;
  2102.  
  2103. Which address
  2104.  
  2105. Create the socket but we don't bind it as we are only going to send data MulticastSocket s = new MulticastSocket();
  2106.  
  2107. Note that we don't have to join the multicast group if we are only
  2108. sending data and not receiving
  2109. Fill the buffer with some data
  2110. byte buf[] = new java.util.Date().toString().getBytes();
  2111.  
  2112. // Create a DatagramPacket
  2113.  
  2114. DatagramPacket pack = new DatagramPacket(buf, buf.length, InetAddress.getByName(group), port);
  2115.  
  2116. Do a send. Note that send takes a byte for the ttl and not an int. s.send(pack);
  2117.  
  2118. And when we have finished sending data close the socket s.close();
  2119.  
  2120. }
  2121. }
  2122.  
  2123. MulticastReceiver.java
  2124.  
  2125. import java.net.*;
  2126. class MulticastReceiver
  2127. {
  2128.  
  2129. public static void main(String args[]) throws Exception {
  2130.  
  2131. Which port should we listen to int port = 5000;
  2132.  
  2133. Which address
  2134.  
  2135. Create the socket and bind it to port 'port'. MulticastSocket s = new MulticastSocket(port);
  2136.  
  2137. join the multicast group s.joinGroup(InetAddress.getByName(group)); int ctr=0;
  2138. while(true)
  2139. {
  2140.  
  2141. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 18
  2142.  
  2143.  
  2144.  
  2145. Distributed Systems 2018-2019
  2146.  
  2147. Now the socket is set up and we are ready to receive packets
  2148. Create a DatagramPacket and do a receive
  2149. byte buf[] = new byte[1024];
  2150.  
  2151. DatagramPacket pack = new DatagramPacket(buf, buf.length); s.receive(pack);
  2152.  
  2153. Finally, let us do something useful with the data we just received,
  2154. like print it on stdout :-)
  2155.  
  2156. System.out.println("Received data from: " + pack.getAddress() + ":" + pack.getPort() +"\t" );
  2157.  
  2158. System.out.println(new String(pack.getData()).trim()); System.out.println();
  2159.  
  2160. And when we have finished receiving data leave the multicast group if( ++ctr == 999) break;
  2161.  
  2162. }
  2163.  
  2164. close the socket s.leaveGroup(InetAddress.getByName(group)); s.close();
  2165. }
  2166. }
  2167.  
  2168. Output:
  2169.  
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190.  
  2191.  
  2192.  
  2193.  
  2194.  
  2195.  
  2196.  
  2197.  
  2198.  
  2199.  
  2200.  
  2201.  
  2202.  
  2203.  
  2204.  
  2205.  
  2206.  
  2207.  
  2208. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 19
  2209.  
  2210.  
  2211.  
  2212. Distributed Systems 2018-2019
  2213.  
  2214. ________________________________________________________
  2215.  
  2216. ________________________________________________________
  2217.  
  2218. ________________________________________________________
  2219.  
  2220. ________________________________________________________
  2221.  
  2222. ________________________________________________________
  2223.  
  2224. ________________________________________________________
  2225.  
  2226. ________________________________________________________
  2227.  
  2228. ________________________________________________________
  2229.  
  2230. ________________________________________________________
  2231.  
  2232. ________________________________________________________
  2233.  
  2234. ________________________________________________________
  2235.  
  2236. ________________________________________________________
  2237.  
  2238. ________________________________________________________
  2239.  
  2240. ________________________________________________________
  2241.  
  2242. ________________________________________________________
  2243.  
  2244. ________________________________________________________
  2245.  
  2246. ________________________________________________________
  2247.  
  2248. ________________________________________________________
  2249. ________________________________________________________
  2250. ________________________________________________________
  2251.  
  2252. ________________________________________________________
  2253.  
  2254. ________________________________________________________
  2255. ________________________________________________________
  2256. ________________________________________________________
  2257.  
  2258. ________________________________________________________
  2259. ________________________________________________________
  2260. ________________________________________________________
  2261.  
  2262. ________________________________________________________
  2263. ________________________________________________________
  2264. ________________________________________________________
  2265.  
  2266. ________________________________________________________
  2267. ________________________________________________________
  2268.  
  2269.  
  2270.  
  2271. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 20
  2272.  
  2273.  
  2274.  
  2275. Distributed Systems 2018-2019
  2276.  
  2277. Practical No: 03
  2278.  
  2279. Aim: Write a program to show the object communication using RMI.
  2280.  
  2281. Code:
  2282.  
  2283. Adder.java
  2284.  
  2285. import java.rmi.*;
  2286. public interface Adder extends Remote{
  2287.  
  2288. public int add(int x,int y)throws RemoteException;
  2289. }
  2290.  
  2291. AdderRemote.java
  2292.  
  2293. import java.rmi.*;
  2294. import java.rmi.server.*;
  2295.  
  2296. public class AdderRemote extends UnicastRemoteObject implements Adder {
  2297.  
  2298. public AdderRemote()throws RemoteException { super();
  2299.  
  2300. }
  2301.  
  2302. public int add(int x,int y) {
  2303. return x+y;
  2304. }
  2305.  
  2306. }
  2307.  
  2308. MyClient.java
  2309.  
  2310. import java.rmi.*;
  2311.  
  2312. public class MyClient{
  2313.  
  2314. public static void main(String args[]){
  2315. try{
  2316.  
  2317. Adder x=(Adder)Naming.lookup("rmi://localhost:5555/vsitadderservice"); System.out.println(x.add(34,4));
  2318.  
  2319. }catch(Exception e){System.out.println(e);}
  2320. }
  2321.  
  2322. }
  2323.  
  2324.  
  2325.  
  2326.  
  2327.  
  2328. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 21
  2329.  
  2330.  
  2331.  
  2332. Distributed Systems 2018-2019
  2333.  
  2334. MyServer.java
  2335. import java.rmi.*;
  2336.  
  2337. import java.rmi.registry.*;
  2338.  
  2339. public class MyServer{
  2340.  
  2341. public static void main(String args[]){
  2342. try{
  2343. variable will be of Interface i.e. a1 and Object will be created
  2344. of that class who has extended the interface i.e.AdderRemote()
  2345.  
  2346. Adder a1=new AdderRemote(); Naming.rebind("rmi://localhost:5555/vsitadderservice",a1);
  2347.  
  2348. }catch(Exception e){System.out.println(e);}
  2349. }
  2350.  
  2351. }
  2352.  
  2353. Output:
  2354.  
  2355.  
  2356.  
  2357.  
  2358.  
  2359.  
  2360.  
  2361.  
  2362.  
  2363.  
  2364.  
  2365.  
  2366.  
  2367.  
  2368.  
  2369.  
  2370.  
  2371.  
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.  
  2381.  
  2382.  
  2383.  
  2384.  
  2385.  
  2386.  
  2387.  
  2388.  
  2389.  
  2390.  
  2391.  
  2392.  
  2393.  
  2394.  
  2395. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 22
  2396.  
  2397.  
  2398.  
  2399. Distributed Systems 2018-2019
  2400.  
  2401.  
  2402.  
  2403.  
  2404.  
  2405.  
  2406.  
  2407.  
  2408.  
  2409.  
  2410.  
  2411.  
  2412.  
  2413.  
  2414.  
  2415.  
  2416.  
  2417.  
  2418.  
  2419.  
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425.  
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.  
  2433.  
  2434.  
  2435.  
  2436.  
  2437.  
  2438.  
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.  
  2448.  
  2449.  
  2450.  
  2451.  
  2452.  
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458.  
  2459.  
  2460.  
  2461.  
  2462.  
  2463.  
  2464.  
  2465.  
  2466.  
  2467.  
  2468.  
  2469. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 23
  2470.  
  2471.  
  2472.  
  2473. Distributed Systems 2018-2019
  2474.  
  2475. ________________________________________________________
  2476.  
  2477. ________________________________________________________
  2478.  
  2479. ________________________________________________________
  2480.  
  2481. ________________________________________________________
  2482.  
  2483. ________________________________________________________
  2484.  
  2485. ________________________________________________________
  2486.  
  2487. ________________________________________________________
  2488.  
  2489. ________________________________________________________
  2490.  
  2491. ________________________________________________________
  2492.  
  2493. ________________________________________________________
  2494.  
  2495. ________________________________________________________
  2496.  
  2497. ________________________________________________________
  2498.  
  2499. ________________________________________________________
  2500.  
  2501. ________________________________________________________
  2502.  
  2503. ________________________________________________________
  2504.  
  2505. ________________________________________________________
  2506.  
  2507. ________________________________________________________
  2508.  
  2509. ________________________________________________________
  2510. ________________________________________________________
  2511. ________________________________________________________
  2512.  
  2513. ________________________________________________________
  2514.  
  2515. ________________________________________________________
  2516. ________________________________________________________
  2517. ________________________________________________________
  2518.  
  2519. ________________________________________________________
  2520. ________________________________________________________
  2521. ________________________________________________________
  2522.  
  2523. ________________________________________________________
  2524. ________________________________________________________
  2525. ________________________________________________________
  2526.  
  2527. ________________________________________________________
  2528. ________________________________________________________
  2529.  
  2530.  
  2531.  
  2532. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 24
  2533.  
  2534.  
  2535.  
  2536. Distributed Systems 2018-2019
  2537.  
  2538.  
  2539. Practical No: 04
  2540.  
  2541. Aim: Show the implementation of Remote Procedure Call.
  2542.  
  2543. Code:
  2544.  
  2545. RPCClient.java
  2546.  
  2547. import java.io.*;
  2548. import java.net.*;
  2549. class RPCClient
  2550. {
  2551.  
  2552. public static void main(String[] args)
  2553. {
  2554. try
  2555.  
  2556. {InetAddress ia = InetAddress.getLocalHost(); DatagramSocket ds = new DatagramSocket(); DatagramSocket ds1 = new DatagramSocket(1300); System.out.println("\nRPC Client\n");
  2557.  
  2558. System.out.println("Enter method name and parameter (eg. add 3 4)\n");
  2559. while (true)
  2560. {
  2561.  
  2562. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  2563.  
  2564. String str = br.readLine();
  2565. byte b[] = str.getBytes();
  2566.  
  2567. DatagramPacket dp = new DatagramPacket(b,b.length,ia,1200); ds.send(dp);
  2568.  
  2569. dp = new DatagramPacket(b,b.length);
  2570. ds1.receive(dp);
  2571.  
  2572. String s = new String(dp.getData(),0,dp.getLength()); System.out.println("\nResult = " + s + "\n");
  2573. }
  2574. } catch (Exception e){e.printStackTrace();}
  2575. }
  2576. }
  2577.  
  2578. RPCServer.java
  2579.  
  2580. import java.util.*;
  2581. import java.net.*;
  2582. class RPCServer
  2583. {
  2584. DatagramSocket ds;
  2585. DatagramPacket dp;
  2586.  
  2587. RPCServer()
  2588. {
  2589.  
  2590. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 25
  2591.  
  2592.  
  2593.  
  2594. Distributed Systems 2018-2019
  2595.  
  2596. String str="",methodName="";
  2597. int val1=0,val2=0;
  2598. double result=0;
  2599.  
  2600. try{
  2601. ds=new DatagramSocket(1200);
  2602. while(true)
  2603. {
  2604. byte b[]=new byte[1000];
  2605. dp=new DatagramPacket(b,b.length);
  2606. ds.receive(dp);
  2607.  
  2608. str=new String(dp.getData(),0,dp.getLength());
  2609.  
  2610. if(str.equalsIgnoreCase("q"))
  2611. continue;
  2612.  
  2613. StringTokenizer st = new StringTokenizer(str," ");
  2614. int i=0;
  2615. while(st.hasMoreTokens())
  2616. {
  2617. methodName = st.nextToken();
  2618. val1 = Integer.parseInt(st.nextToken());
  2619. val2 = Integer.parseInt(st.nextToken());
  2620. }
  2621.  
  2622. System.out.println(str);
  2623. InetAddress ia = InetAddress.getLocalHost();
  2624.  
  2625. if(methodName.equals("add")) result = add(val1,val2); else if(methodName.equals("sub")) result = sub(val1,val2); else if(methodName.equals("mul")) result = mul(val1,val2); else if(methodName.equals("div")) result = div(val1,val2);
  2626.  
  2627. byte b1[]=new String(result+"").getBytes(); DatagramSocket ds1 = new DatagramSocket();
  2628.  
  2629. DatagramPacket dp1 = new DatagramPacket (
  2630.  
  2631. b1,b1.length,InetAddress.getLocalHost(), 1300); System.out.println("result : "+result+"\n"); ds1.send(dp1);
  2632.  
  2633. }
  2634. }catch (Exception e){ e.printStackTrace(); }
  2635. }
  2636.  
  2637. public double add(int val1, int val2)
  2638. {
  2639. return val1+val2;
  2640. }
  2641.  
  2642. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 26
  2643.  
  2644.  
  2645.  
  2646. Distributed Systems 2018-2019
  2647.  
  2648. public double sub(int val3, int val4)
  2649. {
  2650. return val3-val4;
  2651. }
  2652. public double mul(int val3, int val4)
  2653.  
  2654. {
  2655. return val3*val4;
  2656. }
  2657. public double div(int val3, int val4)
  2658. {
  2659. return val3 / (double)val4;
  2660. }
  2661.  
  2662. public static void main(String[] args)
  2663. {
  2664. new RPCServer();
  2665. }
  2666. }
  2667.  
  2668. Output:
  2669.  
  2670.  
  2671.  
  2672.  
  2673.  
  2674.  
  2675.  
  2676.  
  2677.  
  2678.  
  2679.  
  2680.  
  2681.  
  2682.  
  2683.  
  2684.  
  2685.  
  2686.  
  2687.  
  2688.  
  2689.  
  2690.  
  2691.  
  2692.  
  2693.  
  2694.  
  2695.  
  2696.  
  2697.  
  2698.  
  2699.  
  2700.  
  2701.  
  2702.  
  2703.  
  2704.  
  2705.  
  2706.  
  2707.  
  2708.  
  2709. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 27
  2710.  
  2711.  
  2712.  
  2713. Distributed Systems 2018-2019
  2714.  
  2715.  
  2716.  
  2717.  
  2718.  
  2719.  
  2720.  
  2721.  
  2722.  
  2723.  
  2724.  
  2725.  
  2726.  
  2727.  
  2728.  
  2729.  
  2730.  
  2731.  
  2732.  
  2733.  
  2734.  
  2735.  
  2736.  
  2737.  
  2738.  
  2739.  
  2740.  
  2741.  
  2742.  
  2743.  
  2744.  
  2745.  
  2746.  
  2747.  
  2748.  
  2749.  
  2750.  
  2751.  
  2752.  
  2753.  
  2754.  
  2755.  
  2756.  
  2757.  
  2758.  
  2759.  
  2760.  
  2761.  
  2762.  
  2763.  
  2764.  
  2765.  
  2766.  
  2767.  
  2768.  
  2769.  
  2770.  
  2771.  
  2772.  
  2773.  
  2774.  
  2775.  
  2776.  
  2777.  
  2778.  
  2779.  
  2780.  
  2781.  
  2782.  
  2783. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 28
  2784.  
  2785.  
  2786.  
  2787. Distributed Systems 2018-2019
  2788.  
  2789.  
  2790. ________________________________________________________
  2791.  
  2792. ________________________________________________________
  2793.  
  2794. ________________________________________________________
  2795.  
  2796. ________________________________________________________
  2797.  
  2798. ________________________________________________________
  2799.  
  2800. ________________________________________________________
  2801.  
  2802. ________________________________________________________
  2803.  
  2804. ________________________________________________________
  2805.  
  2806. ________________________________________________________
  2807.  
  2808. ________________________________________________________
  2809.  
  2810. ________________________________________________________
  2811.  
  2812. ________________________________________________________
  2813.  
  2814. ________________________________________________________
  2815.  
  2816. ________________________________________________________
  2817.  
  2818. ________________________________________________________
  2819.  
  2820. ________________________________________________________
  2821.  
  2822. ________________________________________________________
  2823.  
  2824. ________________________________________________________
  2825. ________________________________________________________
  2826. ________________________________________________________
  2827.  
  2828. ________________________________________________________
  2829.  
  2830. ________________________________________________________
  2831. ________________________________________________________
  2832. ________________________________________________________
  2833.  
  2834. ________________________________________________________
  2835. ________________________________________________________
  2836. ________________________________________________________
  2837.  
  2838. ________________________________________________________
  2839. ________________________________________________________
  2840. ________________________________________________________
  2841.  
  2842. ________________________________________________________
  2843. ________________________________________________________
  2844.  
  2845.  
  2846. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 29
  2847.  
  2848.  
  2849.  
  2850. Distributed Systems 2018-2019
  2851.  
  2852. Practical No. 05
  2853.  
  2854. Aim: Show the implementation of web services.
  2855.  
  2856. Implementing “Big” Web Service.
  2857.  
  2858. 1) Creating a Web Service
  2859.  
  2860. A. Choosing a Container:
  2861.  
  2862. 1. Choose File > New Project. Select Web Application from the Java Web.
  2863.  
  2864.  
  2865.  
  2866.  
  2867.  
  2868.  
  2869.  
  2870.  
  2871.  
  2872.  
  2873.  
  2874.  
  2875.  
  2876.  
  2877.  
  2878.  
  2879.  
  2880.  
  2881.  
  2882.  
  2883.  
  2884.  
  2885.  
  2886.  
  2887.  
  2888.  
  2889.  
  2890.  
  2891.  
  2892.  
  2893.  
  2894.  
  2895.  
  2896. 2. Name the project CalculatorWSApplication. Select a location for the project. Click Next.
  2897.  
  2898.  
  2899.  
  2900.  
  2901.  
  2902.  
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913.  
  2914.  
  2915.  
  2916. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 30
  2917.  
  2918.  
  2919.  
  2920. Distributed Systems 2018-2019
  2921.  
  2922.  
  2923.  
  2924.  
  2925.  
  2926.  
  2927.  
  2928.  
  2929.  
  2930.  
  2931.  
  2932.  
  2933.  
  2934.  
  2935.  
  2936.  
  2937.  
  2938.  
  2939.  
  2940.  
  2941.  
  2942.  
  2943.  
  2944.  
  2945.  
  2946.  
  2947.  
  2948.  
  2949.  
  2950.  
  2951. 3. Select your server and Java EE version and click Finish
  2952.  
  2953.  
  2954.  
  2955.  
  2956.  
  2957.  
  2958.  
  2959.  
  2960.  
  2961.  
  2962.  
  2963.  
  2964.  
  2965.  
  2966.  
  2967.  
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974.  
  2975.  
  2976.  
  2977.  
  2978.  
  2979.  
  2980.  
  2981.  
  2982.  
  2983.  
  2984.  
  2985.  
  2986.  
  2987.  
  2988.  
  2989.  
  2990. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 31
  2991.  
  2992.  
  2993.  
  2994. Distributed Systems 2018-2019
  2995.  
  2996.  
  2997. B. Creating a Web Service from a Java Class
  2998.  
  2999. 1. Right-click the CalculatorWSApplication node and choose New > Web Service.
  3000.  
  3001.  
  3002.  
  3003.  
  3004.  
  3005.  
  3006.  
  3007.  
  3008.  
  3009.  
  3010.  
  3011.  
  3012.  
  3013.  
  3014.  
  3015.  
  3016.  
  3017.  
  3018.  
  3019.  
  3020.  
  3021.  
  3022.  
  3023.  
  3024.  
  3025.  
  3026.  
  3027.  
  3028. Name the web service CalculatorWS and type org.me.calculator in Package. Leave Create Web Service from Scratch selected. If you are creating a Java EE 6 project on GlassFish or WebLogic, select Implement Web Service as a Stateless Session Bean.
  3029.  
  3030.  
  3031.  
  3032.  
  3033.  
  3034.  
  3035.  
  3036.  
  3037.  
  3038.  
  3039.  
  3040.  
  3041.  
  3042.  
  3043.  
  3044.  
  3045.  
  3046.  
  3047.  
  3048.  
  3049.  
  3050.  
  3051.  
  3052.  
  3053.  
  3054.  
  3055.  
  3056.  
  3057.  
  3058. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 32
  3059.  
  3060.  
  3061.  
  3062. Distributed Systems 2018-2019
  3063.  
  3064.  
  3065.  
  3066.  
  3067.  
  3068.  
  3069.  
  3070.  
  3071.  
  3072.  
  3073.  
  3074.  
  3075.  
  3076.  
  3077.  
  3078.  
  3079.  
  3080.  
  3081.  
  3082.  
  3083.  
  3084.  
  3085.  
  3086.  
  3087.  
  3088.  
  3089.  
  3090.  
  3091.  
  3092.  
  3093.  
  3094.  
  3095.  
  3096.  
  3097.  
  3098.  
  3099.  
  3100.  
  3101.  
  3102. Click Finish. The Projects window displays the structure of the new web service and the source code is shown in the editor area.
  3103.  
  3104.  
  3105. 2) Adding an Operation to the Web Service
  3106.  
  3107. The goal of this exercise is to add to the web service an operation that adds two numbers received from a client. The NetBeans IDE provides a dialog for adding an operation to a web service. You can open this dialog either in the web service visual designer or in the web service context menu.
  3108. A. To add an operation to the web service:
  3109.  
  3110. Change to the Design view in the editor.
  3111.  
  3112. Click Add Operation in either the visual designer or the context menu. The Add Opera-tion dialog opens.
  3113.  
  3114.  
  3115.  
  3116. In the upper part of the Add Operation dialog box, type add in Name and type int in the Return Type drop-down list.
  3117.  
  3118. In the lower part of the Add Operation dialog box, click Add and create a parameter of type int named i.
  3119.  
  3120. Click Add again and create a parameter of type int called j. You now see the following:
  3121.  
  3122. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 33
  3123.  
  3124.  
  3125.  
  3126. Distributed Systems 2018-2019
  3127.  
  3128.  
  3129.  
  3130.  
  3131.  
  3132.  
  3133.  
  3134.  
  3135.  
  3136.  
  3137.  
  3138.  
  3139.  
  3140.  
  3141.  
  3142.  
  3143.  
  3144.  
  3145.  
  3146.  
  3147.  
  3148.  
  3149.  
  3150.  
  3151.  
  3152.  
  3153.  
  3154.  
  3155.  
  3156.  
  3157.  
  3158.  
  3159.  
  3160.  
  3161.  
  3162. Click OK at the bottom of the Add Operation dialog box. You return to the editor.
  3163.  
  3164. The visual designer now displays the following:
  3165. Click Source. And code the following.
  3166.  
  3167. @WebMethod(operationName = "add")
  3168. public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j)
  3169. {
  3170. int k = i + j;
  3171. return k;
  3172.  
  3173. }
  3174.  
  3175.  
  3176.  
  3177.  
  3178.  
  3179.  
  3180.  
  3181.  
  3182.  
  3183.  
  3184.  
  3185.  
  3186.  
  3187.  
  3188.  
  3189.  
  3190.  
  3191.  
  3192.  
  3193.  
  3194. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 34
  3195.  
  3196.  
  3197.  
  3198. Distributed Systems 2018-2019
  3199.  
  3200.  
  3201.  
  3202.  
  3203.  
  3204.  
  3205.  
  3206.  
  3207.  
  3208.  
  3209.  
  3210.  
  3211.  
  3212.  
  3213.  
  3214.  
  3215.  
  3216.  
  3217.  
  3218.  
  3219.  
  3220.  
  3221.  
  3222.  
  3223.  
  3224.  
  3225.  
  3226.  
  3227. 3) Deploying and Testing the Web Service
  3228.  
  3229. After you deploy a web service to a server, you can use the IDE to open the server's test client, if the server has a test client. The GlassFish and WebLogic servers provide test clients.
  3230.  
  3231. A. To test successful deployment to a GlassFish or WebLogic server:
  3232.  
  3233. Right-click the project and choose Deploy. The IDE starts the application server, builds the application, and deploys the application to the server.
  3234.  
  3235. In the IDE's Projects tab, expand the Web Services node of the CalculatorWSApplication project. Right-click the CalculatorWS node, and choose Test Web Service.
  3236.  
  3237.  
  3238.  
  3239.  
  3240.  
  3241.  
  3242.  
  3243.  
  3244.  
  3245.  
  3246.  
  3247.  
  3248.  
  3249.  
  3250.  
  3251.  
  3252.  
  3253.  
  3254.  
  3255.  
  3256.  
  3257.  
  3258.  
  3259.  
  3260.  
  3261.  
  3262.  
  3263.  
  3264. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 35
  3265.  
  3266.  
  3267.  
  3268. Distributed Systems 2018-2019
  3269.  
  3270. The IDE opens the tester page in your browser, if you deployed a web application to the GlassFish server.
  3271.  
  3272. If you deployed to the GlassFish server, type two numbers in the tester page, as shown below
  3273.  
  3274. The sum of the two numbers is displayed:
  3275.  
  3276.  
  3277.  
  3278.  
  3279.  
  3280.  
  3281.  
  3282.  
  3283.  
  3284.  
  3285.  
  3286.  
  3287.  
  3288.  
  3289.  
  3290.  
  3291.  
  3292.  
  3293.  
  3294.  
  3295.  
  3296.  
  3297.  
  3298.  
  3299.  
  3300.  
  3301.  
  3302.  
  3303.  
  3304.  
  3305.  
  3306.  
  3307.  
  3308.  
  3309.  
  3310.  
  3311.  
  3312.  
  3313.  
  3314.  
  3315.  
  3316.  
  3317.  
  3318.  
  3319.  
  3320.  
  3321.  
  3322.  
  3323.  
  3324.  
  3325.  
  3326.  
  3327.  
  3328.  
  3329.  
  3330.  
  3331.  
  3332.  
  3333.  
  3334.  
  3335.  
  3336. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 36
  3337.  
  3338.  
  3339.  
  3340. Distributed Systems 2018-2019
  3341.  
  3342.  
  3343. 4) Consuming the Web Service
  3344.  
  3345. Now that you have deployed the web service, you need to create a client to make use of the web service's add method.
  3346.  
  3347. Client: Java Class in Java SE Application
  3348.  
  3349. Choose File > New Project. Select Java Application from the Java category. Name the project CalculatorWS_Client_Application. Leave Create Main Class selected and accept all other default settings. Click Finish.
  3350.  
  3351.  
  3352.  
  3353.  
  3354.  
  3355.  
  3356.  
  3357.  
  3358.  
  3359.  
  3360.  
  3361.  
  3362.  
  3363.  
  3364.  
  3365.  
  3366.  
  3367.  
  3368.  
  3369.  
  3370.  
  3371.  
  3372.  
  3373.  
  3374.  
  3375.  
  3376.  
  3377.  
  3378.  
  3379.  
  3380.  
  3381.  
  3382.  
  3383.  
  3384.  
  3385.  
  3386. Right-click the CalculatorWS_Client_Application node and choose New > Web Service Client. The New Web Service Client wizard opens.
  3387.  
  3388.  
  3389.  
  3390.  
  3391.  
  3392.  
  3393.  
  3394.  
  3395.  
  3396.  
  3397.  
  3398.  
  3399.  
  3400.  
  3401.  
  3402.  
  3403. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 37
  3404.  
  3405.  
  3406.  
  3407. Distributed Systems 2018-2019
  3408.  
  3409.  
  3410.  
  3411.  
  3412.  
  3413.  
  3414.  
  3415.  
  3416.  
  3417.  
  3418.  
  3419.  
  3420.  
  3421.  
  3422.  
  3423.  
  3424.  
  3425.  
  3426.  
  3427.  
  3428.  
  3429.  
  3430.  
  3431.  
  3432.  
  3433.  
  3434.  
  3435.  
  3436.  
  3437. Select Project as the WSDL source. Click Browse. Browse to the CalculatorWS web ser-vice in the CalculatorWSApplication project. When you have selected the web service, click OK.
  3438.  
  3439. Do not select a package name. Leave this field empty.
  3440.  
  3441. Leave the other settings at default and click Finish. The Projects window displays the new web service client, with a node for the add method that you created:
  3442.  
  3443.  
  3444.  
  3445.  
  3446.  
  3447.  
  3448.  
  3449.  
  3450.  
  3451.  
  3452.  
  3453.  
  3454.  
  3455.  
  3456.  
  3457.  
  3458.  
  3459.  
  3460.  
  3461.  
  3462.  
  3463.  
  3464.  
  3465.  
  3466.  
  3467.  
  3468.  
  3469.  
  3470.  
  3471.  
  3472.  
  3473.  
  3474.  
  3475. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 38
  3476.  
  3477.  
  3478.  
  3479. Distributed Systems 2018-2019
  3480.  
  3481. Double-click your main class so that it opens in the Source Editor. Drag the add node be-low the main() method.
  3482.  
  3483.  
  3484.  
  3485.  
  3486.  
  3487.  
  3488.  
  3489.  
  3490.  
  3491.  
  3492.  
  3493.  
  3494.  
  3495.  
  3496.  
  3497.  
  3498.  
  3499.  
  3500.  
  3501.  
  3502.  
  3503.  
  3504.  
  3505.  
  3506.  
  3507.  
  3508.  
  3509. You now see the following:
  3510. public static void main(String[] args)
  3511. {
  3512. TODO code application logic here
  3513.  
  3514. }
  3515. private static int add(int i, int j)
  3516. {
  3517.  
  3518. org.me.calculator.CalculatorWS_Service service = new org.me.calculator.CalculatorWS_Service(); org.me.calculator.CalculatorWS port = service.getCalculatorWSPort(); return port.add(i, j);
  3519.  
  3520. }
  3521.  
  3522. 7. In the main() method body, replace the TODO comment with code that initializes values for i and j, calls add(), and prints the result.
  3523.  
  3524. Distributed Systems – Practical Manual – 2013-2014 M.Sc. – I.T. – Semester I Page 37
  3525.  
  3526.  
  3527.  
  3528.  
  3529.  
  3530.  
  3531.  
  3532.  
  3533.  
  3534.  
  3535.  
  3536.  
  3537.  
  3538.  
  3539.  
  3540.  
  3541. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 39
  3542.  
  3543.  
  3544.  
  3545. Distributed Systems 2018-2019
  3546.  
  3547. public static void main(String[] args)
  3548. { int i = 3; int j = 4; int result = add(i, j); System.out.println("Result = " + result);
  3549.  
  3550. }
  3551. 8. Surround the main() method code with a try/catch block that prints an exception.
  3552.  
  3553. public static void main(String[] args)
  3554. { try
  3555.  
  3556. { int i = 3; int j = 4; int result = add(i, j); System.out.println("Result = " + result); } catch (Excep-tion ex) { System.out.println("Exception: " + ex); }
  3557.  
  3558. }
  3559. Right-click the project node and choose Run.
  3560.  
  3561.  
  3562.  
  3563.  
  3564.  
  3565.  
  3566.  
  3567.  
  3568.  
  3569.  
  3570.  
  3571.  
  3572.  
  3573.  
  3574.  
  3575.  
  3576.  
  3577.  
  3578.  
  3579.  
  3580.  
  3581.  
  3582.  
  3583.  
  3584.  
  3585.  
  3586.  
  3587. The Output window now shows the sum:
  3588. compile:
  3589. run:
  3590. Result = 7
  3591. BUILD SUCCESSFUL (total time: 1 second)
  3592.  
  3593. Practical 5B: Implementing Web Service that connects to MySQL database.
  3594.  
  3595.  
  3596. Creating MySQL DB Table create database bookshop;
  3597. use bookshop;
  3598. ✓ Create a table named Books that will store valid books information
  3599.  
  3600. create table books(isbn varchar(20) primary key, bookname varchar(100), bookprice var-char(10));
  3601. ✓ Insert valid records in the Books table
  3602.  
  3603. insert into books values("111-222-333","Learn My SQL","250");
  3604.  
  3605. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 40
  3606.  
  3607.  
  3608.  
  3609. Distributed Systems 2018-2019
  3610.  
  3611. insert into books values("111-222-444","Java EE 6 for Beginners","850"); insert into books values("111-222-555","Programming with Android","500"); insert into books values("111-222-666","Oracle Database for you","400");
  3612. insert into books values("111-222-777","Asp.Net for advanced programmers","1250");
  3613.  
  3614. Creating a web service i. Choosing a container
  3615.  
  3616.  
  3617.  
  3618. in a Web application.
  3619.  
  3620. ii. Creating a web application
  3621.  
  3622.  
  3623. - New Project.
  3624.  
  3625.  
  3626. Web Application available under the Projects section. Click Next.
  3627.  
  3628.  
  3629. Name textbox and select the option Use Dedicated Folder for Storing Libraries.
  3630.  
  3631. ∙ Click Next. Server and Settings section of the New Web Application dialog box appears. Choose the default i.e. GlassFish v3 Domain as the Web server, the Java EE 6 Web as the Java
  3632.  
  3633. version and the Context Path. -Finish
  3634.  
  3635.  
  3636.  
  3637. iii. Creating a web service
  3638.  
  3639.  
  3640. -click the BookWS project and select New -> Web Service as shown in diagram.
  3641.  
  3642.  
  3643.  
  3644.  
  3645.  
  3646.  
  3647.  
  3648.  
  3649.  
  3650.  
  3651.  
  3652.  
  3653.  
  3654.  
  3655.  
  3656. textbox, webservice in the Package textbox, select the option Create Web Service from scratch and also select the option implement web service as a stateless session bean as shown in the dia-gram.
  3657.  
  3658.  
  3659.  
  3660.  
  3661.  
  3662.  
  3663.  
  3664.  
  3665.  
  3666. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 41
  3667.  
  3668.  
  3669.  
  3670. Distributed Systems 2018-2019
  3671.  
  3672.  
  3673.  
  3674.  
  3675.  
  3676.  
  3677.  
  3678.  
  3679.  
  3680.  
  3681.  
  3682.  
  3683.  
  3684.  
  3685.  
  3686.  
  3687.  
  3688.  
  3689.  
  3690.  
  3691.  
  3692.  
  3693.  
  3694.  
  3695. 3) Designing the web service
  3696.  
  3697. Now add an operation which will accept the ISBN number from the client to the web service. i. Adding an operation to the web service
  3698.  
  3699.  
  3700.  
  3701. below the name of the BookWS.java tab.
  3702.  
  3703.  
  3704.  
  3705.  
  3706.  
  3707.  
  3708.  
  3709.  
  3710.  
  3711.  
  3712.  
  3713.  
  3714.  
  3715.  
  3716.  
  3717.  
  3718.  
  3719.  
  3720.  
  3721.  
  3722.  
  3723.  
  3724.  
  3725.  
  3726.  
  3727.  
  3728.  
  3729.  
  3730. ∙ Click Add Operation available in the design view of the web service.
  3731.  
  3732.  
  3733. a-
  3734. va.lang.String in the Return Type textbox as shown in the diagram.
  3735.  
  3736.  
  3737. as shown in the diagram.
  3738.  
  3739. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 42
  3740.  
  3741.  
  3742.  
  3743. Distributed Systems 2018-2019
  3744.  
  3745.  
  3746.  
  3747.  
  3748.  
  3749.  
  3750.  
  3751.  
  3752.  
  3753.  
  3754.  
  3755.  
  3756.  
  3757.  
  3758.  
  3759.  
  3760.  
  3761.  
  3762.  
  3763.  
  3764.  
  3765.  
  3766.  
  3767.  
  3768.  
  3769.  
  3770.  
  3771.  
  3772.  
  3773.  
  3774.  
  3775.  
  3776.  
  3777.  
  3778.  
  3779.  
  3780.  
  3781.  
  3782.  
  3783.  
  3784.  
  3785.  
  3786.  
  3787. The code spec expands due to the operation added to the web service as shown
  3788. in the diagram.
  3789.  
  3790.  
  3791.  
  3792.  
  3793.  
  3794.  
  3795.  
  3796.  
  3797.  
  3798.  
  3799.  
  3800.  
  3801.  
  3802.  
  3803.  
  3804.  
  3805.  
  3806.  
  3807.  
  3808.  
  3809.  
  3810.  
  3811. ∙ Modify the code spec of the web service BookWS.java.
  3812.  
  3813. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 43
  3814.  
  3815.  
  3816.  
  3817. Distributed Systems 2018-2019
  3818.  
  3819. Code Spec
  3820. package webservice;
  3821.  
  3822. import java.sql.*;
  3823. import javax.jws.WebMethod;
  3824. import javax.jws.WebParam;
  3825. import javax.jws.WebService;
  3826. import javax.ejb.Stateless;
  3827.  
  3828. @WebService()
  3829. @Stateless()
  3830. public class BookWS {
  3831. /**
  3832.  
  3833. Web service operation */
  3834. @WebMethod(operationName = "getBookDetails")
  3835.  
  3836. public String getBookDetails(@WebParam(name = "isbn") String isbn) {
  3837.  
  3838. //TODO write your implementation code here: Connection dbcon = null;
  3839.  
  3840. Statement stmt = null; ResultSet rs = null; String query = null; try
  3841.  
  3842. {
  3843.  
  3844. Class.forName("com.mysql.jdbc.Driver").newInstance();
  3845.  
  3846. dbcon = DriverManager.getConnection("jdbc:mysql://localhost/bookshop","root","123"); stmt = dbcon.createStatement();
  3847.  
  3848. query = "select * from books where isbn = '" +isbn+ "'"; rs = stmt.executeQuery(query);
  3849.  
  3850. rs.next();
  3851.  
  3852. String bookDetails = "<h1>The name of the book is <b>" +rs.getString("bookname") + "</b> and its cost is <b>" +rs.getString("bookprice") + "</b></h1>.";
  3853.  
  3854. return bookDetails;
  3855. }
  3856.  
  3857. catch(Exception e)
  3858. {
  3859. System.out.println("Sorry failed to connect to the database.." + e.getMessage());
  3860. }
  3861. return null;
  3862.  
  3863. }
  3864. }
  3865.  
  3866. Explanation
  3867.  
  3868. n-
  3869.  
  3870. tered by the user, the associated book name and price is retrieved and returned.
  3871.  
  3872. 4) Adding the MySQL connector
  3873.  
  3874.  
  3875. need to add a reference of MySQL connector to our web service. It is via this connector that our web service will be able to communicate with the database.
  3876.  
  3877.  
  3878.  
  3879. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 44
  3880.  
  3881.  
  3882.  
  3883. Distributed Systems 2018-2019
  3884.  
  3885.  
  3886.  
  3887.  
  3888.  
  3889.  
  3890.  
  3891.  
  3892.  
  3893.  
  3894.  
  3895.  
  3896.  
  3897.  
  3898.  
  3899.  
  3900.  
  3901.  
  3902.  
  3903.  
  3904. the location where mysql-coonector-java-5.1.10-bin is located, select it and click on open as shown.
  3905.  
  3906.  
  3907.  
  3908.  
  3909.  
  3910.  
  3911.  
  3912.  
  3913.  
  3914.  
  3915.  
  3916.  
  3917.  
  3918.  
  3919.  
  3920.  
  3921.  
  3922.  
  3923.  
  3924.  
  3925.  
  3926.  
  3927.  
  3928.  
  3929.  
  3930.  
  3931. 5) Deploying and testing the web service
  3932.  
  3933.  
  3934. a web service is deployed to a web container, the IDE allows testing the web service to see if it functions as expected.
  3935.  
  3936.  
  3937. tester application provided by GlassFish, is integrated into the IDE for this purpose as it allows the developer to enter values and test them.
  3938.  
  3939.  
  3940. e BookWS application, right click the BookWS project and select Deploy as shown in the diagram.
  3941.  
  3942.  
  3943.  
  3944.  
  3945.  
  3946.  
  3947.  
  3948.  
  3949. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 45
  3950.  
  3951.  
  3952.  
  3953. Distributed Systems 2018-2019
  3954.  
  3955.  
  3956.  
  3957.  
  3958.  
  3959.  
  3960.  
  3961.  
  3962.  
  3963.  
  3964.  
  3965.  
  3966.  
  3967.  
  3968.  
  3969.  
  3970.  
  3971.  
  3972.  
  3973.  
  3974.  
  3975.  
  3976. -deploy) and GlassFish v3 Domain
  3977. tabs in the Output view.
  3978.  
  3979.  
  3980. -click the BookWS Web
  3981. service and select Test web service as shown in the diagram.
  3982.  
  3983.  
  3984.  
  3985.  
  3986.  
  3987.  
  3988.  
  3989.  
  3990.  
  3991.  
  3992.  
  3993.  
  3994.  
  3995.  
  3996.  
  3997.  
  3998.  
  3999.  
  4000.  
  4001.  
  4002.  
  4003.  
  4004.  
  4005.  
  4006.  
  4007. GlassFish server as shown in the figure.
  4008.  
  4009.  
  4010.  
  4011.  
  4012.  
  4013.  
  4014.  
  4015.  
  4016.  
  4017.  
  4018.  
  4019.  
  4020.  
  4021.  
  4022.  
  4023.  
  4024. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 46
  4025.  
  4026.  
  4027.  
  4028. Distributed Systems 2018-2019
  4029.  
  4030.  
  4031.  
  4032.  
  4033.  
  4034.  
  4035.  
  4036.  
  4037.  
  4038.  
  4039.  
  4040.  
  4041.  
  4042.  
  4043.  
  4044.  
  4045.  
  4046.  
  4047.  
  4048.  
  4049.  
  4050.  
  4051.  
  4052.  
  4053.  
  4054.  
  4055. ∙ Enter the ISBN number as shown in the diagram.
  4056.  
  4057.  
  4058. The book name and its cost are displayed as shown in the diagram.
  4059.  
  4060.  
  4061.  
  4062.  
  4063.  
  4064.  
  4065.  
  4066.  
  4067.  
  4068.  
  4069.  
  4070.  
  4071.  
  4072.  
  4073.  
  4074.  
  4075.  
  4076.  
  4077.  
  4078.  
  4079.  
  4080.  
  4081.  
  4082.  
  4083.  
  4084.  
  4085.  
  4086.  
  4087.  
  4088.  
  4089.  
  4090.  
  4091.  
  4092.  
  4093.  
  4094.  
  4095.  
  4096.  
  4097.  
  4098.  
  4099.  
  4100.  
  4101. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 47
  4102.  
  4103.  
  4104.  
  4105. Distributed Systems 2018-2019
  4106.  
  4107.  
  4108.  
  4109.  
  4110. 6) Consuming the web service
  4111.  
  4112.  
  4113.  
  4114. the web service’s getBookDetails() method.
  4115.  
  4116. i. Creating a web application
  4117.  
  4118.  
  4119. -> New Project.
  4120.  
  4121.  
  4122. application available under the projects section. Click Finish.
  4123.  
  4124.  
  4125. New web application dialog box appears. Enter BookWSServletClient as the project name in the Project Name textbox and select the option Use Dedicated Folder for Storing Libraries.
  4126.  
  4127.  
  4128. g box appears. Choose
  4129.  
  4130. the default i.e. GlassFish v3 Domain as the web serevr, the Java EE 6 web as the Java EE version
  4131. and the context path.
  4132.  
  4133.  
  4134.  
  4135.  
  4136.  
  4137. ii. Adding the web service to the client application
  4138.  
  4139.  
  4140. -click the BookWSServletClient project and select New -> Web Service Client as shown in the diagram.
  4141.  
  4142.  
  4143.  
  4144.  
  4145.  
  4146.  
  4147.  
  4148.  
  4149.  
  4150.  
  4151.  
  4152.  
  4153.  
  4154.  
  4155.  
  4156. through the web service which needs to be consumed. Click ok. The name of the web service ap-pears in the New Web Service Client as shown in the diagram.
  4157.  
  4158.  
  4159.  
  4160.  
  4161.  
  4162.  
  4163.  
  4164.  
  4165.  
  4166.  
  4167.  
  4168.  
  4169.  
  4170.  
  4171.  
  4172.  
  4173.  
  4174. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 48
  4175.  
  4176.  
  4177.  
  4178. Distributed Systems 2018-2019
  4179.  
  4180.  
  4181.  
  4182.  
  4183.  
  4184.  
  4185.  
  4186.  
  4187.  
  4188.  
  4189.  
  4190.  
  4191.  
  4192.  
  4193.  
  4194.  
  4195.  
  4196.  
  4197.  
  4198.  
  4199.  
  4200.  
  4201.  
  4202.  
  4203.  
  4204.  
  4205.  
  4206.  
  4207.  
  4208.  
  4209.  
  4210.  
  4211.  
  4212.  
  4213.  
  4214.  
  4215.  
  4216.  
  4217.  
  4218. The Web Service Reference directory is added to the BookWSServletClient application as shown in the diagram. It displays the structure of the newly created client including the getBookDetails() method created earlier.
  4219.  
  4220.  
  4221.  
  4222.  
  4223.  
  4224.  
  4225.  
  4226.  
  4227.  
  4228.  
  4229.  
  4230.  
  4231.  
  4232.  
  4233.  
  4234.  
  4235.  
  4236.  
  4237. iii. Creating a servlet
  4238.  
  4239.  
  4240.  
  4241. -> Servlet.
  4242.  
  4243.  
  4244. n-
  4245. ter servlet in the package textbox.
  4246.  
  4247. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 49
  4248.  
  4249.  
  4250.  
  4251. Distributed Systems 2018-2019
  4252.  
  4253.  
  4254.  
  4255. Keep the defaults.
  4256.  
  4257.  
  4258.  
  4259.  
  4260. eBookDetails.java is available with the default skeleton created by the NetBeans IDE which needs to be modified to hold the application logic.
  4261.  
  4262.  
  4263. body of the processRequest() method.
  4264. /*TODO output your page here*/
  4265.  
  4266.  
  4267.  
  4268. out.println("<h1>Servlet retreiveBookDetails at " + request.getContextPath () + "</h1>"); With the code spec of the getBookDetails() operation of the web service by dragging and drop-ping the getBookDetails operation as shown in the diagram.
  4269.  
  4270.  
  4271.  
  4272.  
  4273.  
  4274.  
  4275.  
  4276.  
  4277.  
  4278.  
  4279.  
  4280.  
  4281.  
  4282.  
  4283.  
  4284.  
  4285.  
  4286.  
  4287.  
  4288.  
  4289.  
  4290.  
  4291.  
  4292.  
  4293.  
  4294.  
  4295.  
  4296.  
  4297.  
  4298.  
  4299.  
  4300.  
  4301.  
  4302.  
  4303.  
  4304.  
  4305.  
  4306.  
  4307.  
  4308.  
  4309.  
  4310.  
  4311.  
  4312.  
  4313.  
  4314. change the following code spec:
  4315.  
  4316. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 50
  4317.  
  4318.  
  4319.  
  4320. Distributed Systems 2018-2019
  4321.  
  4322. java.lang.String isbn = “”;
  4323. to
  4324.  
  4325. java.lang.String isbn = request.getParameter(“isbn”);
  4326.  
  4327. iv. Creating an HTML form
  4328.  
  4329.  
  4330. the web service is added and the servlet is created, the form to accept ISBN from the user needs to be coded.
  4331.  
  4332.  
  4333. n-
  4334. dex.jsp file. Modify it to hold the following code spec.
  4335.  
  4336. <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
  4337.  
  4338. <head>
  4339.  
  4340. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>SOAP Cleint - Get Book Details</title> </head>
  4341.  
  4342. <body bgcolor="pink">
  4343.  
  4344. <form name="frmgetBookDetails" method="post" action="retreiveBookDetails"> <h1>
  4345.  
  4346. ISBN : <input type="text" name="isbn"/><br><br> </h1>
  4347.  
  4348. <input type="submit" value="Submit"/>
  4349.  
  4350. </form>
  4351. </body>
  4352. </html>
  4353.  
  4354. v. Building the Web Application
  4355.  
  4356.  
  4357.  
  4358.  
  4359.  
  4360. the Build menu item is clicked the details about the compilation and building of the BookWSServletClient Web application appears in the output – BookWSServletClient (dist) win-dow.
  4361.  
  4362. vi. Running the Application
  4363.  
  4364.  
  4365. web application is done run the application.
  4366.  
  4367.  
  4368.  
  4369. and the BookWSServletCleint application is executed as shown in the diagram.
  4370.  
  4371.  
  4372.  
  4373.  
  4374.  
  4375.  
  4376.  
  4377.  
  4378.  
  4379. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 51
  4380.  
  4381.  
  4382.  
  4383. Distributed Systems 2018-2019
  4384.  
  4385.  
  4386.  
  4387.  
  4388.  
  4389.  
  4390.  
  4391.  
  4392.  
  4393.  
  4394.  
  4395.  
  4396.  
  4397.  
  4398.  
  4399.  
  4400.  
  4401.  
  4402.  
  4403.  
  4404.  
  4405.  
  4406.  
  4407.  
  4408.  
  4409.  
  4410.  
  4411.  
  4412.  
  4413.  
  4414.  
  4415.  
  4416.  
  4417.  
  4418.  
  4419.  
  4420.  
  4421.  
  4422.  
  4423.  
  4424.  
  4425.  
  4426.  
  4427.  
  4428.  
  4429.  
  4430.  
  4431.  
  4432.  
  4433.  
  4434.  
  4435.  
  4436.  
  4437.  
  4438.  
  4439.  
  4440.  
  4441.  
  4442.  
  4443.  
  4444.  
  4445.  
  4446.  
  4447.  
  4448.  
  4449.  
  4450.  
  4451.  
  4452.  
  4453. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 52
  4454.  
  4455.  
  4456.  
  4457. Distributed Systems 2018-2019
  4458.  
  4459. ________________________________________________________
  4460.  
  4461. ________________________________________________________
  4462.  
  4463. ________________________________________________________
  4464.  
  4465. ________________________________________________________
  4466.  
  4467. ________________________________________________________
  4468.  
  4469. ________________________________________________________
  4470.  
  4471. ________________________________________________________
  4472.  
  4473. ________________________________________________________
  4474.  
  4475. ________________________________________________________
  4476.  
  4477. ________________________________________________________
  4478.  
  4479. ________________________________________________________
  4480.  
  4481. ________________________________________________________
  4482.  
  4483. ________________________________________________________
  4484.  
  4485. ________________________________________________________
  4486.  
  4487. ________________________________________________________
  4488.  
  4489. ________________________________________________________
  4490.  
  4491. ________________________________________________________
  4492.  
  4493. ________________________________________________________
  4494. ________________________________________________________
  4495. ________________________________________________________
  4496.  
  4497. ________________________________________________________
  4498.  
  4499. ________________________________________________________
  4500. ________________________________________________________
  4501. ________________________________________________________
  4502.  
  4503. ________________________________________________________
  4504. ________________________________________________________
  4505. ________________________________________________________
  4506.  
  4507. ________________________________________________________
  4508. ________________________________________________________
  4509. ________________________________________________________
  4510.  
  4511. ________________________________________________________
  4512. ________________________________________________________
  4513.  
  4514.  
  4515.  
  4516. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 53
  4517.  
  4518.  
  4519.  
  4520. Distributed Systems 2018-2019
  4521.  
  4522.  
  4523. Practical No: 06
  4524.  
  4525. Aim: Write a program to execute any one mutual exclusion algorithm.(Token Ring)
  4526.  
  4527. Code:
  4528.  
  4529. TokenClient1.java
  4530.  
  4531. import java.io.*;
  4532. import java.net.*;
  4533. public class TokenClient1 {
  4534. public static void main(String arg[]) throws Exception
  4535. {
  4536.  
  4537. InetAddress lclhost;
  4538. BufferedReader br;
  4539. String str="";
  4540. TokenClient12 tkcl,tkser;
  4541. //boolean hasToken;
  4542. //boolean setSendData;
  4543. while(true)
  4544. {
  4545. lclhost=InetAddress.getLocalHost();
  4546. tkcl = new TokenClient12(lclhost);
  4547. tkser = new TokenClient12(lclhost);
  4548. //tkcl.setSendPort(9001);
  4549. tkcl.setSendPort(9004);
  4550. tkcl.setRecPort(8002);
  4551. lclhost=InetAddress.getLocalHost();
  4552. tkser.setSendPort(9000);
  4553. if(tkcl.hasToken == true)
  4554. {
  4555. System.out.println("Do you want to enter the Data --> YES/NO");
  4556. br=new BufferedReader(new InputStreamReader(System.in));
  4557. str=br.readLine();
  4558. if(str.equalsIgnoreCase("yes"))
  4559. {
  4560. System.out.println("ready to send");
  4561. tkser.setSendData = true;
  4562.  
  4563. tkser.sendData();
  4564. tkser.setSendData = false;
  4565. }
  4566. else if(str.equalsIgnoreCase("no")) {
  4567. System.out.println("i m in else");
  4568. //tkcl.hasToken=false;
  4569. tkcl.sendData();
  4570. tkcl.recData();
  4571. System.out.println("i m leaving else");
  4572. }
  4573.  
  4574. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 54
  4575.  
  4576.  
  4577.  
  4578. Distributed Systems 2018-2019
  4579.  
  4580. }
  4581. else {
  4582.  
  4583. System.out.println("ENTERING RECEIVING MODE..."); tkcl.recData();
  4584.  
  4585.  
  4586.  
  4587. } } } }
  4588. class TokenClient12 {
  4589. InetAddress lclhost;
  4590. int sendport,recport;
  4591. boolean hasToken = true;
  4592. boolean setSendData = false;
  4593. TokenClient12 tkcl,tkser;
  4594. TokenClient12(InetAddress lclhost)
  4595. this.lclhost = lclhost;
  4596.  
  4597. }
  4598. void setSendPort(int sendport) {
  4599. this.sendport = sendport;
  4600. } void setRecPort(int recport)
  4601. this.recport = recport;
  4602. }
  4603.  
  4604. void sendData() throws Exception
  4605. BufferedReader br;
  4606. String str="Token";
  4607. DatagramSocket ds;
  4608. DatagramPacket dp;
  4609. if(setSendData == true) {
  4610.  
  4611.  
  4612.  
  4613.  
  4614.  
  4615.  
  4616.  
  4617.  
  4618.  
  4619. {
  4620.  
  4621.  
  4622.  
  4623.  
  4624.  
  4625. {
  4626.  
  4627.  
  4628.  
  4629.  
  4630.  
  4631.  
  4632.  
  4633.  
  4634.  
  4635.  
  4636.  
  4637.  
  4638.  
  4639.  
  4640.  
  4641.  
  4642.  
  4643.  
  4644.  
  4645.  
  4646. {
  4647.  
  4648.  
  4649. System.out.println("sending ");
  4650. System.out.println("Enter the Data");
  4651.  
  4652. br=new BufferedReader(new InputStreamReader(System.in)); str = "ClientOne....." + br.readLine(); System.out.println("now sending");
  4653.  
  4654. }
  4655. ds = new DatagramSocket(sendport);
  4656. dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000);
  4657. ds.send(dp);
  4658. ds.close();
  4659. setSendData = false;
  4660. hasToken = false;
  4661. }
  4662.  
  4663. void recData()throws Exception
  4664. {
  4665. String msgstr;
  4666. byte buffer[] = new byte[256];
  4667. DatagramSocket ds;
  4668. DatagramPacket dp;
  4669. ds = new DatagramSocket(recport);
  4670. dp = new DatagramPacket(buffer,buffer.length);
  4671. ds.receive(dp);
  4672. ds.close();
  4673.  
  4674. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 55
  4675.  
  4676.  
  4677.  
  4678. Distributed Systems 2018-2019
  4679.  
  4680. msgstr = new String(dp.getData(),0,dp.getLength()); System.out.println("The data is "+msgstr);
  4681. if(msgstr.equals("Token")) {
  4682. hasToken = true;
  4683. } } }
  4684.  
  4685. TokenClient2.java
  4686.  
  4687. import java.io.*;
  4688. import java.net.*;
  4689. public class TokenClient2 {
  4690. static boolean setSendData ;
  4691. static boolean hasToken ;
  4692.  
  4693. public static void main(String arg[]) throws Exception {
  4694. InetAddress lclhost;
  4695. BufferedReader br;
  4696. String str1;
  4697. TokenClient21 tkcl;
  4698. TokenClient21 ser;
  4699. while(true) {
  4700. lclhost=InetAddress.getLocalHost();
  4701. tkcl = new TokenClient21(lclhost);
  4702. tkcl.setRecPort(8004);
  4703. tkcl.setSendPort(9002);
  4704. lclhost=InetAddress.getLocalHost();
  4705. ser = new TokenClient21(lclhost);
  4706. ser.setSendPort(9000);
  4707. System.out.println("entering if");
  4708. if(hasToken == true) {
  4709.  
  4710. System.out.println("Do you want to enter the Data --> YES/NO");
  4711. br=new BufferedReader(new InputStreamReader(System.in));
  4712. str1=br.readLine();
  4713. if(str1.equalsIgnoreCase("yes")) {
  4714. System.out.println("ignorecase");
  4715. ser.setSendData = true;
  4716. ser.sendData(); }
  4717. else if(str1.equalsIgnoreCase("no")) {
  4718. tkcl.sendData();
  4719.  
  4720. hasToken=false;
  4721. }
  4722. }
  4723. else {
  4724. System.out.println("entering recieving mode");
  4725. tkcl.recData();
  4726. hasToken=true;
  4727. }
  4728. }
  4729. }
  4730.  
  4731.  
  4732.  
  4733. M.Sc. IT PART-1
  4734.  
  4735.  
  4736. Roll No: 18306A1035
  4737.  
  4738.  
  4739. Page | 56
  4740.  
  4741.  
  4742. Distributed Systems 2018-2019
  4743.  
  4744. }
  4745. class TokenClient21 {
  4746. InetAddress lclhost;
  4747. int sendport,recport;
  4748. boolean setSendData = false;
  4749. boolean hasToken = false;
  4750. TokenClient21 tkcl;
  4751. TokenClient21 ser;
  4752. TokenClient21(InetAddress lclhost) {
  4753. this.lclhost = lclhost; }
  4754. void setSendPort(int sendport) {
  4755. this.sendport = sendport;
  4756. }
  4757.  
  4758. void setRecPort(int recport) {
  4759. this.recport = recport;
  4760. }
  4761. void sendData() throws Exception {
  4762. System.out.println("case");
  4763. BufferedReader br;
  4764. String str="Token";
  4765. DatagramSocket ds;
  4766. DatagramPacket dp;
  4767. if(setSendData == true) {
  4768. System.out.println("Enter the Data");
  4769. br=new BufferedReader(new InputStreamReader(System.in));
  4770. str = "ClientTwo....." + br.readLine(); }
  4771. ds = new DatagramSocket(sendport);
  4772.  
  4773. dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000);
  4774. ds.send(dp);
  4775. ds.close();
  4776. System.out.println("Data Sent");
  4777. setSendData = false;
  4778. hasToken = false;
  4779. }
  4780. void recData()throws Exception {
  4781. String msgstr;
  4782. byte buffer[] = new byte[256];
  4783. DatagramSocket ds;
  4784.  
  4785. DatagramPacket dp;
  4786. ds = new DatagramSocket(recport);
  4787. //ds = new DatagramSocket(4000);
  4788. dp = new DatagramPacket(buffer,buffer.length);
  4789. ds.receive(dp);
  4790. ds.close();
  4791. msgstr = new String(dp.getData(),0,dp.getLength());
  4792. System.out.println("The data is "+msgstr);
  4793.  
  4794.  
  4795.  
  4796. if(msgstr.equals("Token"))
  4797.  
  4798. {
  4799.  
  4800.  
  4801.  
  4802. M.Sc. IT PART-1
  4803.  
  4804.  
  4805. Roll No: 18306A1035
  4806.  
  4807.  
  4808. Page | 57
  4809.  
  4810.  
  4811. Distributed Systems 2018-2019
  4812.  
  4813. hasToken = true;
  4814. }
  4815. }
  4816. }
  4817.  
  4818.  
  4819.  
  4820. TokenServer.java
  4821. import java.net.*;
  4822.  
  4823. public class TokenServer {
  4824.  
  4825. public static void main(String agrs[])throws Exception
  4826. while(true) {
  4827.  
  4828.  
  4829.  
  4830.  
  4831.  
  4832. {
  4833.  
  4834.  
  4835. Server sr=new Server();
  4836. sr.recPort(8000);
  4837. sr.recData();
  4838.  
  4839. }
  4840. }
  4841. }
  4842. class Server {
  4843. boolean hasToken=false;
  4844. boolean sendData=false;
  4845. int recport;
  4846. void recPort(int recport) {
  4847. this.recport=recport;
  4848. }
  4849.  
  4850. void recData()throws Exception {
  4851. byte buff[]=new byte[256];
  4852. DatagramSocket ds;
  4853. DatagramPacket dp;
  4854. String str;
  4855. ds=new DatagramSocket(recport);
  4856. dp=new DatagramPacket(buff,buff.length);
  4857. ds.receive(dp);
  4858. ds.close();
  4859.  
  4860. str=new String(dp.getData(),0,dp.getLength()); System.out.println("The message is "+str); }
  4861.  
  4862. }
  4863.  
  4864.  
  4865.  
  4866.  
  4867.  
  4868.  
  4869.  
  4870.  
  4871.  
  4872.  
  4873.  
  4874.  
  4875.  
  4876.  
  4877.  
  4878.  
  4879. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 58
  4880.  
  4881.  
  4882.  
  4883. Distributed Systems 2018-2019
  4884.  
  4885. Output:
  4886.  
  4887.  
  4888.  
  4889.  
  4890.  
  4891.  
  4892.  
  4893.  
  4894.  
  4895.  
  4896.  
  4897.  
  4898.  
  4899.  
  4900.  
  4901.  
  4902.  
  4903.  
  4904.  
  4905.  
  4906.  
  4907.  
  4908.  
  4909.  
  4910.  
  4911.  
  4912.  
  4913.  
  4914.  
  4915.  
  4916.  
  4917.  
  4918.  
  4919.  
  4920.  
  4921.  
  4922.  
  4923.  
  4924.  
  4925.  
  4926.  
  4927.  
  4928.  
  4929.  
  4930.  
  4931.  
  4932.  
  4933.  
  4934.  
  4935.  
  4936.  
  4937.  
  4938.  
  4939.  
  4940.  
  4941.  
  4942.  
  4943.  
  4944.  
  4945.  
  4946.  
  4947.  
  4948.  
  4949.  
  4950.  
  4951.  
  4952. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 59
  4953.  
  4954.  
  4955.  
  4956. Distributed Systems 2018-2019
  4957.  
  4958.  
  4959.  
  4960.  
  4961.  
  4962.  
  4963.  
  4964.  
  4965.  
  4966.  
  4967.  
  4968.  
  4969.  
  4970.  
  4971.  
  4972.  
  4973.  
  4974.  
  4975.  
  4976.  
  4977.  
  4978.  
  4979.  
  4980.  
  4981.  
  4982.  
  4983.  
  4984.  
  4985.  
  4986.  
  4987.  
  4988.  
  4989.  
  4990.  
  4991.  
  4992.  
  4993.  
  4994.  
  4995.  
  4996.  
  4997.  
  4998.  
  4999.  
  5000.  
  5001.  
  5002.  
  5003.  
  5004.  
  5005.  
  5006.  
  5007.  
  5008.  
  5009.  
  5010.  
  5011.  
  5012.  
  5013.  
  5014.  
  5015.  
  5016.  
  5017.  
  5018.  
  5019.  
  5020.  
  5021.  
  5022.  
  5023.  
  5024.  
  5025.  
  5026. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 60
  5027.  
  5028.  
  5029.  
  5030. Distributed Systems 2018-2019
  5031.  
  5032. ________________________________________________________
  5033.  
  5034. ________________________________________________________
  5035.  
  5036. ________________________________________________________
  5037.  
  5038. ________________________________________________________
  5039.  
  5040. ________________________________________________________
  5041.  
  5042. ________________________________________________________
  5043.  
  5044. ________________________________________________________
  5045.  
  5046. ________________________________________________________
  5047.  
  5048. ________________________________________________________
  5049.  
  5050. ________________________________________________________
  5051.  
  5052. ________________________________________________________
  5053.  
  5054. ________________________________________________________
  5055.  
  5056. ________________________________________________________
  5057.  
  5058. ________________________________________________________
  5059.  
  5060. ________________________________________________________
  5061.  
  5062. ________________________________________________________
  5063.  
  5064. ________________________________________________________
  5065.  
  5066. ________________________________________________________
  5067. ________________________________________________________
  5068. ________________________________________________________
  5069.  
  5070. ________________________________________________________
  5071.  
  5072. ________________________________________________________
  5073. ________________________________________________________
  5074. ________________________________________________________
  5075.  
  5076. ________________________________________________________
  5077. ________________________________________________________
  5078. ________________________________________________________
  5079.  
  5080. ________________________________________________________
  5081. ________________________________________________________
  5082. ________________________________________________________
  5083.  
  5084. ________________________________________________________
  5085.  
  5086.  
  5087.  
  5088.  
  5089.  
  5090. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 61
  5091.  
  5092.  
  5093.  
  5094. Distributed Systems 2018-2019
  5095.  
  5096. Practical No: 07
  5097.  
  5098. Aim: Write a program to implement any one election algorithm.
  5099.  
  5100. Code:
  5101.  
  5102.  
  5103. Election.c
  5104. #include<stdio.h>
  5105. #include<conio.h>
  5106. #include<process.h>
  5107.  
  5108. struct proc
  5109. {
  5110. int live;
  5111. int identifier;
  5112.  
  5113. }
  5114. process[10];
  5115. int n,cordinator=1;
  5116. /******************* DISPLAY PROCESSES **********************/
  5117. void display()
  5118.  
  5119. {
  5120. int i;
  5121. printf("\n PROCESSES ARE\n\n");
  5122. printf("Processes ");
  5123. for(i=1;i<=n;i++)
  5124.  
  5125. {
  5126. printf("P%d\t",i);
  5127. }
  5128. printf("\nlive ");
  5129. for(i=1;i<=n;i++)
  5130.  
  5131. {
  5132. printf("%d\t",process[i].live);
  5133. }
  5134. printf("\nidentifier ");
  5135. for(i=1;i<=n;i++)
  5136.  
  5137. {
  5138. printf("%d\t",process[i].identifier);
  5139. }
  5140. }
  5141. /************ BULLY ALGORITHM ****************************/
  5142.  
  5143. void bully()
  5144. {
  5145. int ch,c,id,i=0,cordinator,init,max=-99;
  5146. cordinator=i;
  5147. for(i=1;i<=n;i++)
  5148.  
  5149. {
  5150. if(process[cordinator].identifier<process[i].identifier&& process[i].live==1)
  5151. cordinator=i;
  5152. }
  5153.  
  5154. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 62
  5155.  
  5156.  
  5157.  
  5158. Distributed Systems 2018-2019
  5159.  
  5160. printf("\n\n CURRENT CO-ORDINATOR IS=P%d",cordinator);
  5161. while(ch!=4)
  5162.  
  5163. {
  5164. printf("\n\n\n *** BULLY ALGORITHM ***");
  5165. printf("\n1.Crash a Process\n2.Activate Process\n3.Display\n4.Exit");
  5166. printf("\nENTER UR CHOICE");
  5167. scanf("%d",&ch);
  5168.  
  5169. switch(ch)
  5170. {
  5171. case 1:
  5172. printf("\n Enter the process id to crash");
  5173. scanf("%d",&id);
  5174.  
  5175. if(process[id].live==0)
  5176. {
  5177. printf("\n Already crashed process");
  5178. }
  5179. else
  5180.  
  5181. {
  5182. process[id].live=0;
  5183. printf("\n process P%d is crashed",id);
  5184. if(id==cordinator)
  5185. {
  5186. while(1)
  5187.  
  5188. {
  5189. printf("\n Enter process id who intiates election");
  5190. scanf("%d",&init);
  5191. if(process[init].live==0)
  5192. {
  5193.  
  5194. printf("\n the selected process is crashed");
  5195. }
  5196. else
  5197. {
  5198. for(i=1;i<=n;i++)
  5199.  
  5200. {
  5201. if(i!=init&& process[i].identifier>process[init].identifier)
  5202. printf("\n Election MSG sent from %d to %d",init,i);
  5203. }
  5204. for(i=1;i<=n;i++)
  5205.  
  5206. {
  5207. if(i!=init)
  5208. {
  5209. if(process[i].identifier>process[init].identifier&&process[i].live!=0)
  5210. {
  5211.  
  5212. printf("\n OK from %d to %d",i,init);
  5213. }
  5214. }
  5215. }
  5216. for(i=1;i<=n;i++)
  5217.  
  5218. {
  5219. if(max<process[i].identifier && process[i].live!=0)
  5220.  
  5221. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 63
  5222.  
  5223.  
  5224.  
  5225. Distributed Systems 2018-2019
  5226.  
  5227. {
  5228. cordinator=i;
  5229.  
  5230. max=process[i].identifier;
  5231. }
  5232. }
  5233. printf("\n\n NEW CO-ORDINATOR IS=P%d",cordinator);
  5234.  
  5235. break;
  5236. }
  5237. }
  5238. }
  5239. }
  5240.  
  5241. break;
  5242. case 2:
  5243. printf("\n Enter process id to activate");
  5244. scanf("%d",&id);
  5245. if(process[id].live==1)
  5246.  
  5247. {
  5248. printf("\n Process %d is already active",id);
  5249. }
  5250. else
  5251. {
  5252. process[id].live=1;
  5253.  
  5254. printf("\n Process %d activated",id);
  5255. }
  5256. if(process[id].identifier>process[cordinator].identifier)
  5257. {
  5258. cordinator=id;
  5259.  
  5260. printf("\n NEW CO-ORDINATOR IS=P%d\n\n",id);
  5261. }
  5262. break;
  5263. case 3:
  5264. display();
  5265.  
  5266. break;
  5267. case 4:
  5268. break;
  5269. }
  5270. }
  5271.  
  5272. }
  5273. /************ RING ALGORITHM ****************************/
  5274. void ring()
  5275. {
  5276. int ch,c,id,i=0,init,max=-99,last;
  5277.  
  5278. for(i=1;i<=n;i++)
  5279. {
  5280. if(process[cordinator].identifier<process[i].identifier&&process[i].live==1)
  5281. cordinator=i;
  5282. }
  5283.  
  5284. printf("\n\n CURRENT CO-ORDINATOR IS=P%d",cordinator); while(ch!=4)
  5285.  
  5286. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 64
  5287.  
  5288.  
  5289.  
  5290. Distributed Systems 2018-2019
  5291.  
  5292. {
  5293. printf("\n\n\n *** RING ALGORITHM ***");
  5294.  
  5295. printf("\n1.Crash a Process\n2.Activate Process\n3.Display\n4.Exit");
  5296. printf("\nENTER UR CHOICE");
  5297. scanf("%d",&ch);
  5298. switch(ch)
  5299. {
  5300.  
  5301. case 1:
  5302. printf("\n Enter the process id to crash");
  5303. scanf("%d",&id);
  5304. if(process[id].live==0)
  5305. {
  5306.  
  5307. printf("\n Already crashed process");
  5308. }
  5309. else
  5310. {
  5311. process[id].live=0;
  5312.  
  5313. printf("\n process P%d is crashed",id);
  5314. if(id==cordinator)
  5315. {
  5316. while(1)
  5317. {
  5318. printf("\n Enter process id who intiates election");
  5319.  
  5320. scanf("%d",&init);
  5321. if(process[init].live==0)
  5322. {
  5323. printf("\n the selected process is crashed");
  5324. }
  5325.  
  5326. else
  5327. {
  5328. last=init;
  5329. printf("\nElection MSG sent from =%d",last);
  5330. for(i=init+1;i<=n;i++)
  5331.  
  5332. {
  5333. if(i!=init)
  5334. printf(" ->%d",i);
  5335. }
  5336. for(i=1;i<init;i++)
  5337.  
  5338. {
  5339. if(i!=init)
  5340. printf("->%d",i);
  5341. last=i;
  5342. }
  5343.  
  5344. for(i=init+1;i<=n;i++)
  5345. {
  5346. if(max<process[i].identifier && process[i].live==1)
  5347. {
  5348. cordinator=i;
  5349.  
  5350. max=process[i].identifier;
  5351. }
  5352.  
  5353. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 65
  5354.  
  5355.  
  5356.  
  5357. Distributed Systems 2018-2019
  5358.  
  5359. }
  5360. for(i=1;i<=init;i++)
  5361.  
  5362. {
  5363. if(max<process[i].identifier && process[i].live==1)
  5364. {
  5365. cordinator=i;
  5366. max=process[i].identifier;
  5367.  
  5368. }
  5369. }
  5370. printf("\n\n NEW COORDINATOR
  5371. IS=P%d",cordinator);
  5372. break;
  5373.  
  5374. }
  5375. }
  5376. }
  5377. }
  5378. break;
  5379.  
  5380. case 2:
  5381. printf("\n Enter process id to activate");
  5382. scanf("%d",&id);
  5383. if(process[id].live==1)
  5384. {
  5385. printf("\n Process %d is already active",id);
  5386.  
  5387. }
  5388. else
  5389. {
  5390. process[id].live=1;
  5391.  
  5392. printf("\n Process %d activated",id); if(process[id].identifier>process[cordinator].identifier) {
  5393.  
  5394. printf("\n NEW CO-ORDINATOR
  5395. IS=P%d\n\n",id);
  5396. cordinator=id;
  5397.  
  5398. }
  5399. }
  5400. break;
  5401. case 3:
  5402. display();
  5403.  
  5404. break;
  5405. case 4:
  5406. break;
  5407. }
  5408. }
  5409.  
  5410. }
  5411. void main()
  5412. {
  5413. int ch,i,c;
  5414. clrscr();
  5415.  
  5416. printf("\n ENTER NO. OF PROCESSES");
  5417. scanf("%d",&n);
  5418.  
  5419. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 66
  5420.  
  5421.  
  5422.  
  5423. Distributed Systems 2018-2019
  5424.  
  5425. for(i=1;i<=n;i++)
  5426. {
  5427.  
  5428. printf("\nEnter P%d process live or not(0/1)",i);
  5429. scanf("%d",&process[i].live);
  5430. printf("\nEnter P%d process identifier",i);
  5431. scanf("%d",&process[i].identifier);
  5432. }
  5433.  
  5434. display();
  5435. while(1)
  5436. {
  5437.  
  5438. printf("\n\n\n**** ELECTION ALGORITHM ****"); printf("\n1.BULLY ALGORITHM\n2.RING ALGORITHM\n3.EXIT"); printf("\n\n ENTER UR CHOICE"); scanf("%d",&ch);
  5439.  
  5440. switch(ch)
  5441. {
  5442. case 1:
  5443.  
  5444. bully();
  5445. break;
  5446. case 2:
  5447. ring();
  5448. break;
  5449. case 3:
  5450.  
  5451. exit(0);
  5452. }
  5453. }
  5454. }
  5455.  
  5456. Output:
  5457.  
  5458.  
  5459.  
  5460.  
  5461.  
  5462.  
  5463.  
  5464.  
  5465.  
  5466.  
  5467.  
  5468.  
  5469.  
  5470.  
  5471.  
  5472.  
  5473.  
  5474.  
  5475.  
  5476.  
  5477.  
  5478.  
  5479.  
  5480.  
  5481.  
  5482.  
  5483.  
  5484.  
  5485.  
  5486. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 67
  5487.  
  5488.  
  5489.  
  5490. Distributed Systems 2018-2019
  5491.  
  5492. ________________________________________________________
  5493.  
  5494. ________________________________________________________
  5495.  
  5496. ________________________________________________________
  5497.  
  5498. ________________________________________________________
  5499.  
  5500. ________________________________________________________
  5501.  
  5502. ________________________________________________________
  5503.  
  5504. ________________________________________________________
  5505.  
  5506. ________________________________________________________
  5507.  
  5508. ________________________________________________________
  5509.  
  5510. ________________________________________________________
  5511.  
  5512. ________________________________________________________
  5513.  
  5514. ________________________________________________________
  5515.  
  5516. ________________________________________________________
  5517.  
  5518. ________________________________________________________
  5519.  
  5520. ________________________________________________________
  5521.  
  5522. ________________________________________________________
  5523.  
  5524. ________________________________________________________
  5525.  
  5526. ________________________________________________________
  5527. ________________________________________________________
  5528. ________________________________________________________
  5529.  
  5530. ________________________________________________________
  5531.  
  5532. ________________________________________________________
  5533. ________________________________________________________
  5534. ________________________________________________________
  5535.  
  5536. ________________________________________________________
  5537. ________________________________________________________
  5538. ________________________________________________________
  5539.  
  5540. ________________________________________________________
  5541. ________________________________________________________
  5542. ________________________________________________________
  5543.  
  5544. ________________________________________________________
  5545. ________________________________________________________
  5546.  
  5547.  
  5548.  
  5549. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 68
  5550.  
  5551.  
  5552.  
  5553. Distributed Systems 2018-2019
  5554.  
  5555. Practical No: 08
  5556.  
  5557. Aim: Show the implementation of any one clock synchronization algorithm.
  5558.  
  5559. Code:
  5560.  
  5561. SCClient.java
  5562.  
  5563. import java.io.*;
  5564. import java.net.*;
  5565. public class SCClient
  5566. {
  5567. public static void main(String args[])throws Exception
  5568. {
  5569.  
  5570. InetAddress lclhost;
  5571. lclhost=InetAddress.getLocalHost();
  5572. while(true)
  5573. {
  5574. Client cntl=new Client(lclhost);
  5575. cntl.sendPort(9001);
  5576. cntl.sendData();
  5577. }}}
  5578. class Client
  5579. {
  5580. InetAddress lclhost;
  5581. int senport;
  5582. Client(InetAddress lclhost)
  5583. {
  5584. this.lclhost=lclhost;
  5585. }
  5586. void sendPort(int senport)
  5587. {
  5588. this.senport=senport;
  5589. }
  5590. void sendData()throws Exception
  5591. {
  5592. DatagramPacket dp;
  5593. DatagramSocket ds;
  5594. BufferedReader br;
  5595.  
  5596. br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the data"); String str=br.readLine();
  5597.  
  5598. ds = new DatagramSocket(senport);
  5599. dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,senport-1000);
  5600. ds.send(dp);
  5601. ds.close();
  5602. }}
  5603.  
  5604.  
  5605.  
  5606. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 69
  5607.  
  5608.  
  5609.  
  5610. Distributed Systems 2018-2019
  5611.  
  5612.  
  5613. SCServer.java
  5614.  
  5615. import java.io.*;
  5616. import java.net.*;
  5617. import java.sql.*;
  5618. public class SCServer
  5619. {
  5620. public static void main(String args[])throws Exception
  5621. {
  5622. InetAddress lclhost;
  5623. lclhost=InetAddress.getLocalHost();
  5624. long maxtime,skewtime,datatime;
  5625. String maxtimestr,skewtimestr;
  5626.  
  5627. BufferedReader br;
  5628.  
  5629. ClntServer ser=new ClntServer(lclhost); System.out.println("Enter the maximum time");
  5630.  
  5631. br = new BufferedReader(new InputStreamReader(System.in)); maxtimestr=br.readLine();
  5632.  
  5633. System.out.println("Enter the maximum skew time");
  5634. br = new BufferedReader(new InputStreamReader(System.in));
  5635. skewtimestr=br.readLine();
  5636. maxtime=Long.parseLong(maxtimestr);
  5637. skewtime=Long.parseLong(skewtimestr);
  5638. while(true)
  5639. {
  5640. datatime = System.currentTimeMillis();
  5641. long G = datatime-maxtime-skewtime;
  5642. System.out.println("G ="+G);
  5643. ser.setTimeStamp(new Timestamp(G));
  5644. ser.recPort(8001);
  5645. ser.recData();
  5646. }}}
  5647. class ClntServer
  5648. {
  5649. InetAddress lclhost;
  5650. int recport;
  5651. Timestamp obtmp;
  5652.  
  5653. ClntServer(InetAddress lclhost)
  5654. {
  5655. this.lclhost = lclhost;
  5656. }
  5657. void recPort(int recport)
  5658. {
  5659. this.recport = recport;
  5660. }
  5661. void setTimeStamp(Timestamp obtmp)
  5662. {
  5663.  
  5664. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 70
  5665.  
  5666.  
  5667.  
  5668. Distributed Systems 2018-2019
  5669.  
  5670. this.obtmp = obtmp;
  5671. }
  5672. void recData()throws Exception
  5673. {
  5674. String msgstr="";
  5675. DatagramSocket ds;
  5676. DatagramPacket dp;
  5677. BufferedReader br;
  5678. byte buf[] = new byte[256];
  5679. ds = new DatagramSocket(recport);
  5680. dp = new DatagramPacket(buf,buf.length);
  5681. ds.receive(dp);
  5682. ds.close();
  5683.  
  5684. msgstr = new String(dp.getData(),0,dp.getLength()); System.out.println(msgstr);
  5685.  
  5686. Timestamp obtmp = new Timestamp(Long.parseLong(msgstr)); if(this.obtmp.before(obtmp) == true) {
  5687.  
  5688. System.out.println("The Message is accepted");
  5689. }
  5690. else
  5691. {
  5692. System.out.println("The Message is rejected");
  5693. }}}
  5694.  
  5695.  
  5696.  
  5697.  
  5698.  
  5699.  
  5700.  
  5701.  
  5702.  
  5703.  
  5704.  
  5705.  
  5706.  
  5707.  
  5708.  
  5709.  
  5710.  
  5711.  
  5712.  
  5713.  
  5714.  
  5715.  
  5716.  
  5717.  
  5718.  
  5719.  
  5720.  
  5721.  
  5722.  
  5723.  
  5724.  
  5725.  
  5726.  
  5727.  
  5728. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 71
  5729.  
  5730.  
  5731.  
  5732. Distributed Systems 2018-2019
  5733.  
  5734. Output:
  5735.  
  5736.  
  5737.  
  5738.  
  5739.  
  5740.  
  5741.  
  5742.  
  5743.  
  5744.  
  5745.  
  5746.  
  5747.  
  5748.  
  5749.  
  5750.  
  5751.  
  5752.  
  5753.  
  5754.  
  5755.  
  5756.  
  5757.  
  5758.  
  5759.  
  5760.  
  5761.  
  5762.  
  5763.  
  5764.  
  5765.  
  5766.  
  5767.  
  5768.  
  5769.  
  5770.  
  5771.  
  5772.  
  5773.  
  5774.  
  5775.  
  5776.  
  5777.  
  5778.  
  5779.  
  5780.  
  5781.  
  5782.  
  5783.  
  5784.  
  5785.  
  5786.  
  5787.  
  5788.  
  5789.  
  5790.  
  5791.  
  5792.  
  5793.  
  5794.  
  5795.  
  5796.  
  5797.  
  5798.  
  5799.  
  5800.  
  5801. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 72
  5802.  
  5803.  
  5804.  
  5805. Distributed Systems 2018-2019
  5806.  
  5807. ________________________________________________________
  5808.  
  5809. ________________________________________________________
  5810.  
  5811. ________________________________________________________
  5812.  
  5813. ________________________________________________________
  5814.  
  5815. ________________________________________________________
  5816.  
  5817. ________________________________________________________
  5818.  
  5819. ________________________________________________________
  5820.  
  5821. ________________________________________________________
  5822.  
  5823. ________________________________________________________
  5824.  
  5825. ________________________________________________________
  5826.  
  5827. ________________________________________________________
  5828.  
  5829. ________________________________________________________
  5830.  
  5831. ________________________________________________________
  5832.  
  5833. ________________________________________________________
  5834.  
  5835. ________________________________________________________
  5836.  
  5837. ________________________________________________________
  5838.  
  5839. ________________________________________________________
  5840.  
  5841. ________________________________________________________
  5842. ________________________________________________________
  5843. ________________________________________________________
  5844.  
  5845. ________________________________________________________
  5846.  
  5847. ________________________________________________________
  5848. ________________________________________________________
  5849. ________________________________________________________
  5850.  
  5851. ________________________________________________________
  5852. ________________________________________________________
  5853. ________________________________________________________
  5854.  
  5855. ________________________________________________________
  5856. ________________________________________________________
  5857. ________________________________________________________
  5858.  
  5859. ________________________________________________________
  5860.  
  5861.  
  5862.  
  5863.  
  5864.  
  5865. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 73
  5866.  
  5867.  
  5868.  
  5869. Distributed Systems 2018-2019
  5870.  
  5871. Practical No: 9
  5872.  
  5873. Aim: Implement the concept of distributed file system architecture.
  5874.  
  5875. Network File System:
  5876.  
  5877. Goto root mode.
  5878. $ su
  5879. password – fedora
  5880.  
  5881. Install nfs-utils
  5882. #yum install nfs-utils
  5883.  
  5884. Specify what we want to share
  5885. #/etc/exports
  5886.  
  5887. Start the Services
  5888. #systemctl start rpcbind.service
  5889. #systemctl start nfs-lock.service
  5890. #systemctl start nfs-service.service
  5891. #systemctl start nfs-idmap.service
  5892. #systemctl start mountd.service
  5893.  
  5894. Enable the Services
  5895. #systemctl enable rpcbind.service
  5896. #systemctl enable nfs-lock.service
  5897. #systemctl enable nfs-service.service
  5898. #systemctl enable nfs-idmap.service
  5899. #systemctl enable mountd.service
  5900.  
  5901. Type the message
  5902. cat>>vaibhav
  5903. Hello World!
  5904.  
  5905. Cat>>/nfstest/vaibhav
  5906. Hello Universe!
  5907.  
  5908. mkdir vsit
  5909.  
  5910. mount –t nfs 127.0.0.1:/nfstest vsit
  5911.  
  5912.  
  5913.  
  5914.  
  5915.  
  5916.  
  5917.  
  5918.  
  5919.  
  5920.  
  5921.  
  5922.  
  5923.  
  5924. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 74
  5925.  
  5926.  
  5927.  
  5928. Distributed Systems 2018-2019
  5929.  
  5930. Output:
  5931.  
  5932.  
  5933.  
  5934.  
  5935.  
  5936.  
  5937.  
  5938.  
  5939.  
  5940.  
  5941.  
  5942.  
  5943.  
  5944.  
  5945.  
  5946.  
  5947.  
  5948.  
  5949.  
  5950.  
  5951.  
  5952.  
  5953.  
  5954.  
  5955.  
  5956.  
  5957.  
  5958.  
  5959.  
  5960.  
  5961.  
  5962.  
  5963.  
  5964.  
  5965.  
  5966.  
  5967.  
  5968.  
  5969.  
  5970.  
  5971.  
  5972.  
  5973.  
  5974.  
  5975.  
  5976.  
  5977.  
  5978.  
  5979.  
  5980.  
  5981.  
  5982.  
  5983.  
  5984.  
  5985.  
  5986.  
  5987.  
  5988.  
  5989.  
  5990.  
  5991.  
  5992.  
  5993.  
  5994.  
  5995.  
  5996.  
  5997. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 75
  5998.  
  5999.  
  6000.  
  6001. Distributed Systems 2018-2019
  6002.  
  6003.  
  6004.  
  6005.  
  6006.  
  6007.  
  6008.  
  6009.  
  6010.  
  6011.  
  6012.  
  6013.  
  6014.  
  6015.  
  6016.  
  6017.  
  6018.  
  6019.  
  6020.  
  6021.  
  6022.  
  6023.  
  6024.  
  6025.  
  6026.  
  6027.  
  6028.  
  6029.  
  6030.  
  6031.  
  6032.  
  6033.  
  6034.  
  6035.  
  6036.  
  6037.  
  6038.  
  6039.  
  6040.  
  6041.  
  6042.  
  6043.  
  6044.  
  6045.  
  6046.  
  6047.  
  6048.  
  6049.  
  6050.  
  6051.  
  6052.  
  6053.  
  6054.  
  6055.  
  6056.  
  6057.  
  6058.  
  6059.  
  6060.  
  6061.  
  6062.  
  6063.  
  6064.  
  6065.  
  6066.  
  6067.  
  6068.  
  6069.  
  6070.  
  6071. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 76
  6072.  
  6073.  
  6074.  
  6075. Distributed Systems 2018-2019
  6076.  
  6077.  
  6078.  
  6079.  
  6080.  
  6081.  
  6082.  
  6083.  
  6084.  
  6085.  
  6086.  
  6087.  
  6088.  
  6089.  
  6090.  
  6091.  
  6092.  
  6093.  
  6094.  
  6095.  
  6096.  
  6097.  
  6098.  
  6099.  
  6100.  
  6101.  
  6102.  
  6103.  
  6104.  
  6105.  
  6106.  
  6107.  
  6108.  
  6109.  
  6110.  
  6111.  
  6112.  
  6113.  
  6114.  
  6115.  
  6116.  
  6117.  
  6118.  
  6119.  
  6120.  
  6121.  
  6122.  
  6123.  
  6124.  
  6125.  
  6126.  
  6127.  
  6128.  
  6129.  
  6130.  
  6131.  
  6132.  
  6133.  
  6134.  
  6135.  
  6136.  
  6137.  
  6138.  
  6139.  
  6140.  
  6141.  
  6142.  
  6143.  
  6144.  
  6145. M.Sc. IT PART-1 Roll No: 18306A1035 Page | 77
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement