cn_exp5

Published on Aug 13, 2026

cn_exp5

cn_exp5 - PDF to Flipbook

Published on Aug 13, 2026

Description:

Nam shaikh ibahim Rll N 4AI51 lass TE- AIM EXPERIMENT- 5 Aim To Perform TP lient and Server Soket Programming using Java. bjetive • ndstand th npt f lint-sv mmniatin. • an hw TP skts stablish a liabl nntin. • Exhang mssags btwn a lint and a sv sing Java. Apparatus (Software) mmand Pmpt Tminal, Java JK 8 lat VS d / Elips / IntlliJ IEA / NtBans Theory Java Skt pgamming is sd f mmniatin btwn th appliatins nning n diffnt JRE. Java Skt pgamming an b nntin-intd nntin-lss. Skt and SvSkt lasss a sd f nntin-intd skt pgamming and atagam Skt and atagam Pakt lasss a sd f nntin-lss skt pgamming. Th lint in skt pgamming mst knw tw infmatin IP Addss f Sv, and Pt nmb. H, w a ging t mak n-way lint and sv mmniatin. In this appliatin, lint snds a mssag t th sv, sv ads th mssag and pints it. H, tw lasss a bing sd Skt and SvSkt. Th Skt lass is sd t mmniat lint and sv. Thgh this lass, w an ad and wit mssag. Th SvSkt lass is sd at sv-sid. Th apt( mthd f SvSkt lass blks th nsl ntil th lint is nntd. Aft th sssfl nntin f lint, it tns th instan f Skt atomputer Network Lab AY 2026-202 T.E / V sv-sid. Soket lass A skt is simply an ndpint f mmniatins btwn th mahins. Th Skt lass an b sd t at a skt. Important methods Method pbli InptStam gtInptStam( Desription tns th Inpt Stam attahd with this skt. omputer Network Lab AY 2026-202 T.E / VMethod pbli tptStam gttptStam( Desription tns th tptStam attahd with this skt. pbli synhnizd vid ls( lss this skt Exampl f Java Skt Pgamming reating Server T at th sv appliatin, w nd t at th instan f SvSkt lass. H, w a sing 6666 pt nmb f th mmniatin btwn th lint and sv. Y may als hs any th pt nmb. Th apt( mthd waits f th lint. If lints nnts with th givn pt nmb, it tns an instan f Skt. SvSkt ssnw SvSkt(6666 Skt sss.apt(//stablishs nntin and waits f th lint reating lient T at th lint appliatin, w nd t at th instan f Skt lass. H, w nd t pass th IP addss hstnam f th Sv and a pt nmb. H, w a sing "lalhst" bas sv is nning n sam systm. Skt snw Skt("lalhst",6666 t's s a simpl f Java skt pgamming wh lint snds a txt and sv ivs and pints it. Algorithm Server Algorithm 1. Start. 2. reate a ServerSoket on port 5000.omputer Network Lab AY 2026-202 T.E / V 3. Wait for the lient using aept(). 4. Reeive the lient’s message. 5. Display the reeived message. 6. Send a response to the lient. . lose streams and sokets. 8. Stop. lient Algorithm 1. Start. 2. reate a soket using the server IP address and port number. 3. onnet to the server. 4. Send a message. 5. Reeive the server’s response. 6. Display the reeived response. . lose streams and soket. 8. Stop. Proedure Stp 1 Install Java * Install JK (Java vlpmnt Kit n y mpt. * Vify installatin by pning mmand Pmpt/Tm java -version java -version If both ommands display the installed version, Java is ready. Step 2 reate the Java Programs reate two Java files * TPServer.java * TPlient.java Step 3 Write the Server Program The server program should 1. Import networking libraries. 2. reate a ServerSoket using a port number (e.g., 5000).3. Wait for a lient onnetion using aept(). 4. Reeive the message sent by the lient. 5. Display the reeived message. 6. Send a reply bak to the lient. omputer Network Lab AY 2026-202 T.E / V . lose all streams and sokets. Stp 4 Wit th lint Pgam Th lint pgam shld 1. Impt ntwking libais. . at a Skt sing * Sv IP Addss (lalhst if sam mpt * Pt nmb (5 3. nnt t th sv. 4. Snd a mssag. 5. Riv th sv’s ply. 6. isplay th ivd ply. . ls th skt and stams. Stp 5 mpil th Pgams pn mmand Pmpt in th pt fld. mpil th sv ava TPSv.ava ompile the lient ava TPlint.ava If th a n s, .lass fils will b atd. Stp 6 Rn th Sv Stat th sv fist ava TPSv Expeted output Server Started... Waiting for lient… The server will now wait for a lient onnetion. Step Run the lientpen another ommand Prompt window. Exeute java TPlient The lient onnets to the server. Step 8 Data Transmission omputer Network Lab AY 2026-202 T.E / V After onnetion lient sends Hello Server Server reeives Message from lient Hello Server Server replies Hello lient lient reeives Message from Server Hello lient Step 9 lose the onnetion After ommuniation * lose input stream. * lose output stream. * lose lient soket. * lose server soket. File MyServer.java 1. impt ava.i.* . impt ava.nt.* 3. pbli lass MySv { 4. pbli stati vid main(Sting ags{ 5. ty{ 6. SvSkt ssnw SvSkt(6666 . Skt sss.apt(//stablishs nntin 8. ataInptStam disnw ataInptStam(s.gtInptStam(. Sting st(Stingdis.adTF( 1. Systm.t.pintln("mssag "+st 11. ss.ls( 1. }ath(Exptin {Systm.t.pintln(} 13. } omputer Network Lab AY 2026-202 T.E / V 14. } File Mylient.java impt ava.i.* impt ava.nt.* 3 pbli lass Mylint { 4 pbli stati vid main(Sting ags { 5 ty{ 6 Skt snw Skt("lalhst",6666 7 atatptStam dtnw atatptStam(s.gttptStam( 8 dt.witTF("Hll Sv" 9 dt.flsh( dt.ls( s.ls( }ath(Exptin {Systm.t.pintln(} 3 } 4 } dwnlad this xampl T xt this pgam pn tw mmand pmpts and xt ah pgam at ah mmand pmpt as displayd in th blw fig. Aft nning th lint appliatin, a mssag will b displayd n th sv nsl.omputer Network Lab AY 2026-202 T.E / V Example of Java Soket Programming (Read-Write both side) In this xampl, lint will wit fist t th sv thn sv will iv and pint th txt. Thn sv will wit t th lint and lint will iv and pint th txt. Th stp gs n. File Server.java impt ava.nt.* impt ava.i.* 3 lass Sv{ 4 pbli stati vid main(Sting agsthws Exptin{5 SvSkt ssnw SvSkt(3333 6 Skt sss.apt( 7 ataInptStam dinnw ataInptStam(s.gtInptStam( 8 atatptStam dtnw atatptStam(s.gttptStam( . BffdRad bnw BffdRad(nw InptStamRad(Systm.in omputer Network Lab AY 2026-202 T.E / V 1.Sting st"",st"" whil(!st.qals("stp"{ stdin.adTF( 3 Systm.t.pintln("lint says "+st 4 stb.adin( 5 dt.witTF(st 6 dt.flsh( 7 } 8 din.ls( 9 s.ls( ss.ls( }} File lient.java impt ava.nt.* impt ava.i.* 3 lass lint{ 4 pbli stati vid main(Sting agsthws Exptin{ 5 Skt snw Skt("lalhst",3333 6 ataInptStam dinnw ataInptStam(s.gtInptStam( 7 atatptStam dtnw atatptStam(s.gttptStam( BffdRad bnw BffdRad(nw InptStamRad(Systm.in . Sting st"",st"" whil(!st.qals("stp"{ stb.adin( 3 dt.witTF(st 4 dt.flsh( omputer Network Lab AY 2026-202 T.E / V 5 stdin.adTF( 6 Systm.t.pintln("Sv says "+st 7 } dt.ls( 3 s.ls( 4 }} Results onlusion Hn, w hav stdis hw t Pfm TP lint and Sv Skt Pgamming sing Java Sssflly.