Thuc hanh HPT

  • Published on
    04-Dec-2014

  • View
    215

  • Download
    1

Embed Size (px)

Transcript

<p>I. MC TIU THC HNH</p> <p>Gii thiu cho sinh vin cc kin thc cn bn v k thut RMI, y l k thut c s dng ph bin pht trin cc ng dng phn tn trong java. Pht trin cc ng dng phn tn khc nhau trn k thut RMI II. HC LIU Hc liu :- a USB - Phn mm (JDK1.5.0 hoc tr ln, Jcreator 4.0,NetBean 6.x )</p> <p>- Bi tp thc hnh ca ging vin cung cp Dng c : - Projector, mn chiu - My tnh - Cc phn mm dy hc - Bng, phn - H thng my tnh c kt ni mng III. NI DUNG Gm 6 Lab vi cc ni dung sau: - LAB_01 : X l dng v File ( Bui 01) - LAB_02 : Lp trnh Socket v Multi Socket ( Bui 02) - LAB_03 : Lp trnh Threat v Multi Threat (Bui 03) - LAB_04 : Lp trnh UDP ( Bui 04) - LAB_05 : Lp trnh TCP ( Bui 05) - LAB_06 : Lp trnh Databsae( Bui 06) - LAB_07 : Lp trnh RMI (Bui 07,08) - LAB_08 : X l cc bi ton ng phn tn c bn bng k thut RMI (Bui 09) - Kim tra thc hnh VI.TI LIU THAM KHO Ti liu chnh [1]. Tp bi ging H phn tn , ca ging vin Nguyn Minh Nht [2]. Jie Wu, "Distributed Systems Design", Addison-Wesley, 2004 Ti liu tham kho [3]. S. Mullender ed., "Distributed Systems", 2nd ed., Addison-Wesley, 1993 [4]. G. Coulouris, J. Dollimore, T. Kinberg, "Distributed systems : Concept and Design" 1</p> <p>[5]. Spiegel, A. (1998). Objects by value: Evaluating the trade-off. In Proceedings Int. Conf. on Parallel and Distributed Computing and Networks (PDCN), pages 542548, Brisbane, Australia. IASTED, ACTA Press. []. van Steen, M., Homburg, P., and Tanenbaum, A. (1999). Globe: A Wide-Area Distributed System. IEEE Concurrency, pages 70-78. [6]. Waldo, J., Wyant, G., Wollrath, A., and Kendall, S. (1997). A Note on Distributed Computing. In Vitek, J. and Tschudin, C., editors, Mobile Object Systems: Towards the Programmable Internet, volume 1222 of Lecture Notes in Computer Science, pages 49-64. Springer-Verlag. Ti liu Internet [5]. http://java.sun.com [6]. http://icp.org [7]. http://www.comp.dit.ie/coleary/teaching/dt2494ds/notes/11/index.html [8]. http://en.wikibooks.org/wiki/Distributed_Systems#Synchronization</p> <p>2</p> <p>TRNG I HC DUY TN KHOA CNG NGH THNG TIN B MN K THUT MNG</p> <p>H PHN TN (Distributed System)</p> <p>BI THC HNH Bi s : 01 S gi : 03gi GVHD : ThS.Nguyn Minh Nht</p> <p>LAB 01 STREAMS &amp; FILESI.MC TIU Cng c mt s kin thc lm c s cho thit k v lp trnh phn tn vi java, gm cc vn nh : - Dng v FileX l c, ghi trn dng v File</p> <p>II. NI DUNG A. L THUYT Lung : Lung byte, lung k t a.Lung Byte : + Lp tru tng : InputStream v OutputStream + Cc phng thc h tr Lp lung byte BufferedInputStream BufferedOutputStream ByteArrayInputStream ByteArrayOutputStream DataInputStream DataOutputStream FileInputStream FileOutputStream FilterInputStream FilterOutputStream InputStream OutputStream PipedInputStream ngha Lung vo trn Bufered Lung ra trn Bufered Input stream c d liu t mt mng byte Output Stream ghi d liu t mt mng Lung nhp c nhng phng thc c nhng kiu d liu chun trong java Lung xut c nhng phng thc ghi nhng kiu d liu chun trong java Lung nhp cho php c d liu t file Lung xut cho php ghi d liu xung file Hin thc lp tru tng InputStream Hin thc lp tru tng OutputStream Lp tru tng, l lp cha ca tt c cc lp lung nhp kiu Byte Lp tru tng, l lp cha ca tt c cc lp xut nhp kiu Byte Lung nhp byte kiu ng (piped) thng phi c 3</p> <p>gn vi mt lung xut kiu ng. Lung nhp byte kiu ng (piped) thng phi c gn vi mt lung nhp kiu ng to nn mt kt ni trao i d liu kiu ng. PrintStream Lung xut c cha phng thc print() v prinln() PushbackInputStream L mt lung nhp kiu Byte m h tr thao tc tr li (push back) v phc hi thao tc c mt byte (unread) RandomAccessFile H tr cc thao tc c, ghi i vi file truy cp ngu nhin. SequenceInputStream L mt lung nhp c to nn bng cch ni kt logic cc lung nhp khc. Nhng phng thc nh ngha trong lp InputStream v OutputStream PipedOutputStream Phng thcInputStream int available( ) void close( )</p> <p> ngha- Tr v s lung bytes c th c c t lung nhp - ng lung nhp v gii phng ti nguyn h thng gn vi lung. Khng thnh cng s nm ra mt li IOException - nh du v tr hin ti trong lung nhp - Kim tra xem lung nhp c h tr phng thc mark() v reset() khng. c byte tip theo t lung nhp - c buffer.length bytes v lu vo trong vng nh buffer. Kt qu tr v s bytes tht s c c - c numBytes bytes bt u t a ch offset v lu vo trong vng nh buffer. Kt qu tr v s bytes tht s c c - Nhy con tr n v tr c xc nh bi vic gi hm mark() ln sau cng. - Nhy qua numBytes d liu t lung nhp - ng lung xut v gii phng ti nguyn h thng gn vi lung. Khng thnh cng s nm ra mt li IOException - p d liu t b m phi ghi ngay xung lung</p> <p>void mark(int numBytes) boolean markSupported( ) int read( ) int read(byte buffer[ ])</p> <p>int read(byte buffer[ ], int offset, int numBytes) void reset( ) long skip(long numBytes) Output Stream void close( )</p> <p>4</p> <p>void flush( ) void write(int b) void write(byte buffer[ ]) void write(byte buffer[ ], int offset, int numBytes)</p> <p>(nu c) - Ghi byte d liu ch nh xung lung - Ghi buffer.length bytes d liu t mng ch nh xung lung - Ghi numBytes bytes d liu t v tr offset ca mng ch nh buffer xung lung</p> <p>b.Lung k t (Character Streams) + Lp tru tng : Reader v Writer + Cc phng thc h tr Lp lung k tBufferedReader BufferedWriter CharArrayReader CharArrayWriter FileReader FileWriter FilterReader FilterWriter InputStreamReader LineNumberReader OutputStreamWriter PipedReader PipedWriter PrintWriter PushbackReader Reader StringReader StringWriter Writer</p> <p> ngha</p> <p>Lung nhp k t c d liu vo mt vng m Lung xut k t ghi d liu ti mt vng m Lung nhp c d liu t mt mng k t Lung xut ghi d liu ti mt mng k t Lung nhp k t c d liu t file Lung xut k t ghi d liu n file Lp c d liu trung gian(lp tru tng) Lp xut trung gian tru tng Lung nhp chuyn bytes thnh cc k t (*) Lung nhp m dng Lung xut chuyn nhng k t thnh cc bytes(*) Lung c d liu bng c ch ng ng Lung ghi d liu bng c ch ng ng Lung ghi vn bn ra thit b xut (cha phng thc print() v println() ) Lung nhp cho php c v khi phc li d liu Lp nhp d liu tru tng Lung nhp c d liu t chui Lung xut ghi d liu ra chui Lp ghi d liu tru tng</p> <p>B.BI TP 1.1. Hy c mt mng byte t System.in, sau khi nhp t bn phm. 1.2. S dng phng thc System.out.write() xut k t X ra Console 1.3. Hy c cc k t t console s dng BufferedReader, cho n khi gp du chm th ngng li. 1.4. Hy c cc chui t console s dng BufferedReader, nhng n ch stop th ngng li. 1.5. Hy to mt file text s dng BufferedReader</p> <p>5</p> <p>1.6. Hy xut ra Console dng lung k t c kiu thay i 1.7. Hin th ni dung ca mt file tn test.txt lu ti D:\test.txt 1.8. Copy ni dung mt file text n mt file text khc 1.9. Dng DataOutputStream v DataInputStream ghi v c nhng kiu d liu khc nhau trn file. 1.10. Ghi 6 s kiu double xung file, ri c ln theo th t ngu nhin. 1.11. 1.12.c nhng dng vn bn nhp t bn phm v ghi chng xung file tn l test.txt. Vic c v ghi kt thc khi ngi dng nhp vo chui stop. HNG DN 1.1. S dng phng thc read c ni dung t system.inimport java.io.*; class ReadBytes { public static void main(String args[]) throws IOException { byte data[] = new byte[100]; System.out.print("Ban hay nhap mot so ky tu "); System.in.read(data); // Doc chuoi byte da nhap vao data System.out.print(" Cac ky tu cua ban da nhap: "); for(int i=0; i &lt; data.length; i++) System.out.print((char) data[i]); } }</p> <p>1.3. Nhp Console dng lung k t Thng th vic nhp d liu t Console dng lung k t th thun li hn dng lung byte. Lp tt nht c d liu nhp t Console l lp BufferedReader. Tuy nhin chng ta khng th xy dng mt lp BufferedReader trc tip t System.in. Thay vo chng ta phi chuyn n thnh mt lung k t. lm iu ny chng ta dng InputStreamReader chuyn bytes thnh k t. c c mt i tng InputStreamReader gn vi System.in ta dng constructor ca InputStreamReader :InputStreamReader(InputStream inputStream)</p> <p>Tip theo dng i tng InputStreamReader to ra to ra mt BufferedReader dng constructor BufferedReader. BufferedReader(Reader inputReader) V d: To mt BufferedReader gn vi KeyboardBufferedReader br = new BufferedReader(new InputStreamReader(System.in));</p> <p>Sau khi thc hin cu lnh trn, br l mt lung k t gn vi Console thng qua System.in. 6</p> <p>1.4</p> <p>import java.io.*; class ReadChars { public static void main(String args[]) throws IOException { char c; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Nhap chuoi ky tu, gioi han dau cham."); // Doc chuoi ky tu do { c = (char)br.read(); System.out.println(c); } while(c != '.'); } } hng dn : BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); import java.io.*; class ReadChars { public static void main(String args[]) throws IOException { char c; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Nhap chuoi ky tu, gioi han stop "); // Doc chuoi ky tu do Readline : c theo hng ngang { c = br.readline(); System.out.println(c); } while(!c.readline(stop)); } }</p> <p>1.5</p> <p>:</p> <p>hng dn : ging 1.6,1.7</p> <p>1.6. Trong ngn ng java, bn cnh vic dng System.out xut d liu ra Console (thng dng debug chng trnh), chng ta c th dng lung PrintWriter i vi cc chng trnh chuyn nghip. PrintWriter l mt trong nhng lp lung k t.</p> <p>7</p> <p>Vic dng cc lp lung k t xut d liu ra Console thng c a chung hn. xut d liu ra Console dng PrintWriter cn thit phi ch nh System.out cho lung xut. V d: To i tng PrintWriter xut d liu ra Console PrintWriter pw = new PrintWriter(System.out, true); 1.7. c d liu t file M mt file c d liu FileInputStream(String fileName) FileNotFoundException Nu file khng tn ti: th nm ra FileNotFoundException throws</p> <p> c d liu: dng phng thc read() int read( ) throws IOException: c tng byte t file v tr v gi tr ca byte c c. Tr v -1 khi ht file, v nm ra IOException khi c li c. ng file: dng phng thc close() void close( ) throws IOException: sau khi lm vic xong cn ng file gii phng ti nguyn h thng cp pht cho file. 1.8. Ghi d liu xung file M mt file ghi d liu FileOutputStream(String fileName) throws FileNotFoundException Nu file khng to c: th nm ra FileNotFoundException Ghi d liu xung: dng phng thc write() void write(int byteval) throws IOException: ghi mt byte xc nh bi tham s byteval xung file, v nm ra IOException khi c li ghi. ng file: dng phng thc close() void close( ) throws IOException: sau khi lm vic xong cn ng file gii phng ti nguyn h thng cp pht cho file. 1.9.c v ghi d liu nh phn c v ghi nhng gi tr nh phn ca cc kiu d liu trong java, chng ta s dng DataInputStream v DataOutputStream. DataOutputStream: hin thc interface DataOuput. Interface DataOutput c cc phng thc cho php ghi tt c nhng kiu d liu c s ca java n lung (theo nh dng nh phn). Phng thc ngha void writeBoolean (boolean val) Ghi xung lung mt gi tr boolean c xc nh bi val. void writeByte (int val) Ghi xung lung mt byte c xc nh bi val. 8</p> <p>void writeChar (int val) void writeDouble (double val) void writeFloat (float val) void writeInt (int val) void writeLong (long val) void writeShort (int val)</p> <p>Ghi xung lung mt Char c xc nh bi val. Ghi xung lung mt gi tr Double c xc nh bi val. Ghi xung lung mt gi tr float c xc nh bi val. Ghi xung lung mt gi tr int c xc nh bi val. Ghi xung lung mt gi tr long c xc nh bi val. Ghi xung lung mt gi tr short c xc nh bi val.</p> <p>Contructor: DataOutputStream(OutputStream outputStream) OutputStream: l lung xut d liu. ghi d liu ra file th i tng outputStream c th l FileOutputStream. DataInputStream: hin thc interface DataInput. Interface DataInput c cc phng thc cho php c tt c nhng kiu d liu c s ca java (theo nh dng nh phn). Phng thc boolean readBoolean( ) Byte readByte( ) char readChar( ) double readDouble( ) float readFloat( ) int readInt( ) Long readLong( ) short readShort( ) ngha c mt gi tr boolean c mt byte c mt Char c mt gi tr Double c mt gi tr float c mt gi tr int c mt gi tr long c mt gi tr short</p> <p>Contructor: DataInputStream(InputStream inputStream) InputStream: l lung nhp d liu. d liu t file th i tng InputStream c th l FileInputStream. 1.10. Bn cnh vic x l xut nhp trn file theo kiu tun t thng qua cc lung, java cng h tr truy cp ngu nhin ni dung ca mt file no dng RandomAccessFile. RandomAccessFile khng dn xut t InputStream hay OutputStream m n hin thc cc interface DataInput, DataOutput (c nh ngha cc phng thc I/O c bn). RandomAccessFile h tr vn nh v con tr file bn trong mt file dng phng thc seek(long newPos). 1.12. c/ghi File dng lung k t Thng thng c/ghi file ngi ta thng dng lung byte, nhng i vi lung k t chng ta cng c th thc hin c. u im ca vic dng lung k t l chng thao tc trc tip trn cc k t Unicode. V vy lung k t l chn la tt nht khi cn lu nhng vn bn Unicode. 9</p> <p>Hai lp lung thng dng cho vic c/ghi d liu k t xung file l FileReader v FileWriter.</p> <p>---o0o---</p> <p>Mi s thnh cng ca ngy hm sau l s n lc t ngy hm nay.</p> <p>TRNG I HC DUY TN KHOA CNG NGH THNG TIN B MN K THUT MNG</p> <p>H PHN TN(Distributed System)LAB s : S gi :</p> <p>BI THC HNH 02 06 ( 2 bui )</p> <p>GVHD :ThS.Nguyn Minh Nht</p> <p>LAB 02LP TRNH SOCKET, MULTI SOCKETI.MC TIU Thc hin trao i thng ip gia cc my tnh qua Socket. II. NI DUNG A.L THUYT 10</p> <p>1.nh ngha Socket l mt phng php thit lp kt ni truyn thng gia mt chng trnh yu cu dch v ( client) v mt chng trnh cung cp dch v (server) trn mng LAN, WAN hay Internet v i lc l gia nhng qu trnh ngay bn trong my tnh. Mi socket c th c xem nh mt im cui trong mt kt ni. Mt socket trn my yu cu dch v c a ch mng c cp sn gi mt socket trn my cung cp dch v. Mt khi socket c thit lp ph hp, hai my tnh c th trao i dch v v d liu. Nhng my tnh c Socket server m bo tnh trng m ca cng truyn thng, sn sng nhn bt k cuc gi n no d khng nh trc. Nhng my yu cu dch v thng xc nh s hiu cng ca server mong mun bng cch tm n trong c s d liu v Domain Name System Nhng my tnh c Socket server m bo tnh trng m ca cng truyn thng, sn sng nhn bt k cuc gi n no d khng nh trc. Nhng my yu cu dch v thng xc nh s hiu cng ca server mong mun bng cch tm n trong c s d liu v Domain Name System Vi s pht trin ca Web, socket vn tip tc ng vai tr quan trng trong vic duy tr cc lung truyn thng trn Internet. Cc ng dng c lin quan n Internet u vit lp bn trn socket, v d socket tch hp mt s phn ca a ch Website, trnh duyt web v cng ngh bo mt Scure Socket Layer. Trong lp trnh Web hin nay, Socket thc s khng cn thit i d dng Java, serlet, hay CGI,PHP,v khng bao gi m c cng mt cch tng minh. Cc socket vn tn ti kt ni ngi dng vi ng dng Web, nhng cc chi tit ca socket c n trong nhng lp su hn mi ngi khng phi ng chm n. Cc lp trnh vin c th trnh c nhng kh khn ca vic to socket nh th vin lp cc th h mi, chng hn Microsoft Foundation Clas Csocket v CsocketFile. Lp trnh vin Uinix c th dng Socket++ v.v Trong java, h tr mt sn lp v socket l : Java.net.Socket, n c dng rng ri trong vic to ra cc socket pha yu cu dch v c lp h thng, trong khi java.net.ServerSocket c th xy dng mt socket sn sng cho vic nhn cc yu cu t my yu cu dch v. Vi nhng cng c ny, cc nh pht trin c th nhanh chng to ra cc socket m khng cn phi sa ly trong cc chi tit lp trnh. 2. Xy dng chng trnh Socket trong java Mt chng trnh Socket bng java c thc hin nh sau: a/ Server: - Lng nghe v chp nhn kt ni t cng 9999. - Cho php nhiu client kt ni n cng mt lc. - Khi client gi n 1 chui th :</p> <p>11</p> <p>+ Nu chui l "quit" th ngt kt ni vi client. + Tin hnh o chui. + Gi chui c o cho client. b/ Client: - Kt ni ti Server qua cng 9999. - Nhp chui t bn phm. - Gi chui ti server. - Hin...</p>