np lab manual.docx

28
7/23/2019 NP Lab Manual.docx http://slidepdf.com/reader/full/np-lab-manualdocx 1/28 National Institute Of Technology, Raipur Department of Computer Science & Engineering Lab ile  CS !"#!!$CS% Netor' (rogramming Lab )utumn Session, !"*+ Course – B.Tech(NIT Scheme) Semester- 7 th  Sem.  Name: NAVEEN KUMAR Ro Num!er: "#""$%&'

Upload: arvind

Post on 19-Feb-2018

256 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 1/28

National Institute Of Technology, Raipur

Department of Computer Science & Engineering

Lab ile

  CS !"#!!$CS%  Netor' (rogramming Lab

)utumn Session, !"*+

Course – B.Tech(NIT Scheme) Semester- 7th Sem.

 Name: NAVEEN KUMAR 

Ro Num!er: "#""$%&'

Page 2: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 2/28

INDE

S-No Name of The E.periment(age

No-

Date of 

E.p-

Date of 

Sub-Remar's

".rte a *ro+ram ,hch taes a tet /e

as a0 0*ut a01 +e0erate the toe0s

us0+ s*t metho1

" %$2%'2"$ %'2"%2"$

#.

rte a *ro+ram to ,hch taes a tet

/e as a0 0*ut a01 +e0erate toe0s

us0+ Str0+Toe03er cass.

4 %$2%'2"$ %'2"%2"$

4.

rte a *ro+ram ,hch taes a tet /e

as a0 0*ut a01 etract the 0um!ers

/rom t a01 cacuate the sum o/ these

us0+ Sca00er cass

5 %$2%'2"$ %'2"%2"$

&.rte a *ro+ram to cou0t the 0um!er o/ 

toe0s o/ 1//ere0t 1ata t6*es %$2%'2"$ %'2%"2"$

$.rte a *ro+ram to m*eme0t TC8

Socet 8ro+ramm0+ 0 9aa."4 "#2%'2"$ %'2"%2"$

5.

rte a *ro+ram to se01 /e /rom serer 

to ce0t us0+ TC8 Socet

 *ro+ramm0+."5 "#2%'2"$ %'2"%2"$

7.rte a *ro+ram to m*eme0t U;8

Socet 8ro+ramm0+ 0 <aa#" "#2%'2"$ %'2"%2"$

'. rte a *ro+ram to m*eme0t RMI 09aa.

#& #42%2"$ %'2"%2"$

.rte a *ro+ram to 1s*a6 S6stem

Vara!es. #$ #42%2"$ %'2"%2"$

"%.rte a *ro+ram to m*eme0t UR=

 *ro+ramm0+ 0 9aa. # #42%2"$ %'2"%2"$

Page 3: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 3/28

E(ERI/ENT 0*

)I/1 rte a *ro+ram ,hch taes a tet /e as a0 0*ut a01 +e0erate the toe0s us0+ s*t metho1.

DESCRI(TION1 

The 2a3a-lang-String-split$String rege., int limit% metho1 s*ts ths str0+ arou01 matches o/ the

+e0 re+uar e*resso0. The arra6 retur0e1 !6 ths metho1 co0ta0s each su!str0+ o/ ths str0+ that s

term0ate1 !6 a0other su!str0+ that matches the +e0 e*resso0 or s term0ate1 !6 the e01 o/ the str0+.

I/ the e*resso0 1oes 0ot match a06 *art o/ the 0*ut the0 the resut0+ arra6 has <ust o0e eeme0t> 0ame6

ths str0+.

CODE1

m*ort <aa.o.Bu//ere1Rea1er?

m*ort <aa.o.@eRea1er?

m*ort <aa.ut.Str0+Toe03er?

 *u!c cass Ma0

 

 *u!c statc o1 ma0(Str0+ ar+s)

  Str0+ st D 0u?

  Bu//ere1Rea1er !r?

 

tr6

 !r D 0e, Bu//ere1Rea1er(0e, @eRea1er(C:20*a!2Feo.tt))?

  Str0+Bu1er s! D 0e, Str0+Bu1er()?

  Str0+ 0 D !r.rea1=0e()?

  ,he (0 GD 0u)

  s!.a**e01(0)?

  s!.a**e01(S6stem.0eSe*arator())?

  0 D !r.rea1=0e()?

  H  stD s!.toStr0+()?

 

 !r.cose()?

 

H catch (Ece*to0 e)

e.*r0tStacTrace()?

H

S6stem.out.*r0t0(str)?

S6stem.out.*r0t0(Us0+ Str0+ cass)?

 Str0+ 1ems D ?

Str0+ toe0s" D str.s*t(1ems)?

Page 4: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 4/28

/or (0t D %? toe0s".e0+th? JJ)

  S6stem.out.*r0t0(toe0s")?

H

H

O4T(4T1

 

Page 5: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 5/28

E(ERI/ENT 5 !

)I/1 rte a *ro+ram to ,hch taes a tet /e as a0 0*ut a01 +e0erate toe0s us0+ Str0+Toe03er cass.

DESCRI(TION1

The 2a3a-util-StringTo'eni6er cass ao,s to !rea a str0+ 0to toe0s. It s sm*e ,a6 to !rea

str0+. Str0+Toe03er(Str0+ str> Str0+ 1em) co0structor creates Str0+Toe03er ,th s*ec/e1 str0+ a01

1emeter.

 !ooea0 hasMoreEeme0ts(): Checs / there s more toe0s aaa!e.

!<ect 0etEeme0t(): Retur0s the 0et toe0 /rom the Str0+Toe03er o!<ect.

CODE1

 *aca+e str0+toe0s?

m*ort <aa.o.Bu//ere1Rea1er?

m*ort <aa.o.@eRea1er?

m*ort <aa.ut.Str0+Toe03er?

 *u!c cass Ma0

 

 *u!c statc o1 ma0(Str0+ ar+s)

  Str0+ st D 0u?

  Bu//ere1Rea1er !r?

 tr6

 !r D 0e, Bu//ere1Rea1er(0e, @eRea1er(E:20*a!2Feo.tt))?

  Str0+Bu1er s! D 0e, Str0+Bu1er()?

  Str0+ 0 D !r.rea1=0e()?

  ,he (0e GD 0u)

  s!.a**e01(0)?

  s!.a**e01(L0)?

  0 D !r.rea1=0e()?

  H

  stD s!.toStr0+()?

  !r.cose()?

 

H catch (Ece*to0 e)

e.*r0tStacTrace()?

H

S6stem.out.*r0t0(str)?

  Str0+ 1ems D ?S6stem.out.*r0t0(Us0+ Str0+Toe03er cass)?

St T t St T ( t 1 )

Page 6: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 6/28

,he (st.hasMoreEeme0ts())

S6stem.out.*r0t0(st.0etEeme0t())?

H

H

H

O4T(4T1

Page 7: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 7/28

E(ERI/ENT07

)I/1 rte a *ro+ram ,hch taes a tet /e as a0 0*ut a01 etract the 0um!ers /rom t a01 cacuate the

sum o/ these us0+ Sca00er cass.

DESCRI(TION1

The Sca00er cass s a cass 0 <aa.ut> ,hch ao,s the user to rea1 aues o/ arous t6*es.

It !reas ts 0*ut 0to toe0s us0+ a 1emter *atter0> ,hch !6 1e/aut matches ,htes*ace. The resut0+

toe0s ma6 the0 !e co0erte1 0to aues o/ 1//ere0t t6*es us0+ the arous 0et metho1s. hasNet()

metho1 retur0s true / ths sca00er has a0other toe0 0 ts 0*ut.  I0 ths *ro+ram 0etI0t() a01

0et;ou!e() metho1s hae !ee0 use1 to etract the 0um!ers a01 the0 the a11to0 s *er/orme1.

CODE1

m*ort <aa.o.@e?

m*ort <aa.o.@eNot@ou01Ece*to0?

m*ort <aa.ut.?

 *u!c cass Ma0

  *u!c statc o1 ma0(Str0+ ar+s) thro,s @eNot@ou01Ece*to0

 

 

@e /D0e, @e(E:20*a!2Feo.tt)?

  Sca00er scD0e, Sca00er(/)?  0t ?

  1ou!e 1>sumD%?

  ,he(sc.hasNet())

 

  /(sc.hasNetI0t())

 

  sumJDsc.0etI0t()?

  H

  ese /(sc.hasNet;ou!e()) 

  sumJDsc.0et;ou!e()?

H

  ese

 

Str0+ sDsc.0et()?H

 

H

  S6stem.out.*r0t0(Sum D Jsum)?

  H

H

Page 8: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 8/28

O4T(4T1

Page 9: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 9/28

E(ERI/ENT 5 8

)I/1 rte a *ro+ram to cou0t the 0um!er o/ toe0s o/ 1//ere0t 1ata t6*es.

DESCRI(TION1

The 2a3a-util-Scanner cass s a sm*e tet sca00er ,hch ca0 *arse *rmte t6*es. A Sca00er

 !reas ts 0*ut 0to toe0s us0+ a 1emter *atter0> ,hch !6 1e/aut matches ,htes*ace. A sca000+

o*erato0 ma6 !oc ,at0+ /or 0*ut. A Sca00er s 0ot sa/e /or mutthrea1e1 use ,thout eter0a

s60chro03ato0.

 !ooea0 hasNetBooea0()

Ths metho1 retur0s true / the 0et toe0 0 ths sca00ers 0*ut ca0 !e 0ter*rete1 as a !ooea0 aue us0+

a case 0se0ste *atter0 create1 /rom the str0+ trueO/ase.

 !ooea0 hasNetI0t()

Ths metho1 retur0s true / the 0et toe0 0 ths sca00ers 0*ut ca0 !e 0ter*rete1 as a0 0t aue 0 the

1e/aut ra1 us0+ the 0etI0t() metho1.

 !ooea0 hasNet;ou!e()

Ths metho1 retur0s true / the 0et toe0 0 ths sca00ers 0*ut ca0 !e 0ter*rete1 as a 1ou!e aue us0+

the 0et;ou!e() metho1.

Str0+ 0et()

Ths metho1 /01s a01 retur0s the 0et com*ete toe0 /rom ths sca00er.

The FashMa* cass uses a hashta!e to m*eme0t the Ma* 0ter/ace. Ths ao,s the eecuto0 tme

o/ !asc o*erato0s> such as +et( ) a01 *ut( )> to rema0 co0sta0t ee0 /or ar+e sets.

CODE1

package scannercount;

import java.util.*;

public class Main {  public static void main(String[] args)  {  Scanner sc=ne Scanner(!" # $ t%is is &' aipur " # $. + +. true ,alse!);

-as%MapString/'nteger0 map=ne -as%MapString/'nteger0();  int i;  double d;

  boolean b;  String str;

%il ( % & t())

Page 10: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 10/28

  {  i,(sc.%as&e1t'nt())  {  i=sc.ne1t'nt();  i,(map.get(!int!)==null)  map.put(!int!/");  else  { 'nteger in=ne 'nteger(map.get(!int!));  map.put(!int!/in2");  3  3  else i,(sc.%as&e1t4ouble())  {  d=sc.ne1t4ouble();  i,(map.get(!double!)==null)  map.put(!double!/");  else  { 'nteger in=ne 'nteger(map.get(!double!));

  map.put(!double!/in2");  3  3 

else i,(sc.%as&e1t5oolean())  {  b=sc.ne1t5oolean();

 i,(map.get(!bool!)==null)

  map.put(!bool!/");  else

  { 'nteger in=ne 'nteger(map.get(!bool!));  map.put(!bool!/in2");  3  3  else  {  str=sc.ne1t();  i,(map.get(!string!)==null)  map.put(!string!/"); 

else

  { 'nteger in=ne 'nteger(map.get(!string!));  map.put(!string!/in2");

3  3  3 

S6stem.out.println(!'nteger tokens = !2map.get(!int!)2! 4ouble tokens =!2map.get(!double!)2! 5oolean tokens = !2map.get(!bool!)2! String tokens =!2map.get(!string!)); 

33

Page 11: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 11/28

O4T(4T1

Page 12: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 12/28

E(ERI/ENT0+

)I/1 rte a *ro+ram to m*eme0t TC8 Socet 8ro+ramm0+ 0 9aa.

DESCRI(TION1

The <aa.0et.Socet cass re*rese0ts a socet> a01 the <aa.0et.SererSocet cass *ro1es a

mecha0sm /or the serer *ro+ram to ste0 /or ce0ts a01 esta!sh co00ecto0s ,th them.The /oo,0+

ste*s occur ,he0 esta!sh0+ a TC8 co00ecto0 !et,ee0 t,o com*uters us0+ socets.

". The serer 0sta0tates a SererSocet o!<ect> 1e0ot0+ ,hch *ort 0um!er commu0cato0 s to occur o0.

#. The serer 0oes the acce*t() metho1 o/ the SererSocet cass. Ths metho1 ,ats u0t a ce0t

co00ects to the serer o0 the +e0 *ort.

4. A/ter the serer s ,at0+> a ce0t 0sta0tates a Socet o!<ect> s*ec/60+ the serer 0ame a01 *ort

0um!er to co00ect to.

&. The co0structor o/ the Socet cass attem*ts to co00ect the ce0t to the s*ec/e1 serer a01 *ort 0um!er.

I/ commu0cato0 s esta!she1> the ce0t 0o, has a Socet o!<ect ca*a!e o/ commu0cat0+ ,th the

serer.$.0 the serer s1e> the acce*t() metho1 retur0s a re/ere0ce to a 0e, socet o0 the serer that s co00ecte1

to the ce0ts socet. 

CODE1

".Serer.<aa

m*ort <aa.o.?

m*ort <aa.0et.? 

 *u!c cass Serer 

  *rate statc Socet socet? 

 *u!c statc o1 ma0(Str0+ ar+s)

 

  ut*utStream os? 

tr6

 

SererSocet sererSocet D 0e, SererSocet(54%)?

  S6stem.out.*r0t0(Serer Starte1 a01 ste00+ to the *ort 54%)?

  22Serer s ru000+ a,a6s. Ths s 1o0e us0+ ths ,he(true) oo*  ,he(true)

 

  22Rea10+ the messa+e /rom the ce0t

  socet D sererSocet.acce*t()?

  Bu//ere1Rea1er !r D 0e, Bu//ere1Rea1er(0e, I0*utStreamRea1er(socet.+etI0*utStream()))?

  Str0+ 0um D !r.rea1=0e()?

  S6stem.out.*r0t0(Messa+e recee1 /rom ce0t s J0um)? 

22Mut*60+ the 0um!er !6 # a01 /orm0+ the retur0 messa+e

  Str0+ retMs+?

  tr6 

0t 0tNum D I0te+er *arseI0t(0um)?

Page 13: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 13/28

  0t retVa D 0tNum#?

  retMs+ D Str0+.aue/(retVa) J L0?

  H

  catch(Num!er@ormatEce*to0 e)

 

  22I0*ut ,as 0ot a 0um!er. Se010+ *ro*er messa+e !ac to ce0t.

  retMs+D 8ease se01 a *ro*er 0um!erL0?

  H

 22Se010+ the res*o0se !ac to the ce0t.

 

Bu//ere1rter !, D 0e, Bu//ere1rter(0e, ut*utStreamrter(socet.+etut*utStream()))?

  !,.,rte(retMs+)?

  S6stem.out.*r0t0(Messa+e se0t to the ce0t s JretMs+)?

  !,./ush()?

  H

  H

  catch (Ece*to0 e)

 

  e.*r0tStacTrace()?

  H

  /0a6

 

  tr6

 

  socet.cose()?

  H

  catch(Ece*to0 e)H

  H

  HH

#.Ce0t.<aa

m*ort <aa.o.?

m*ort <aa.0et.?

 

 *u!c cass Ce0t

  *u!c statc o1 ma0(Str0+ ar+s)

 

I0*utStream s?

ut*utStream os?

Socet socet D 0u?

  tr6

 

  socet D 0e, Socet(ocahost> 54%)?

 

22Se01 the messa+e to the serer   os D socet.+etut*utStream()?

Page 14: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 14/28

  Bu//ere1rter !, D 0e, Bu//ere1rter(0e, ut*utStreamrter(os))?

 

Str0+ 0um!er D #?

 

Str0+ se01Ms+ D 0um!er J L0?

  !,.,rte(se01Ms+)?

  !,./ush()?

  S6stem.out.*r0t0(Messa+e se0t to the serer : Jse01Ms+)? 

22Pet the retur0 messa+e /rom the serer 

s D socet.+etI0*utStream()?

  Bu//ere1Rea1er !r D 0e, Bu//ere1Rea1er(0e, I0*utStreamRea1er(s))?

  Str0+ ms+D !r.rea1=0e()?

  S6stem.out.*r0t0(Messa+e recee1 /rom the serer : Jms+)?

  H

  catch (Ece*to0 ece*to0)

    ece*to0.*r0tStacTrace()?

  H

  /0a6

 

  22Cos0+ the socet

  tr6

 

  socet.cose()?

  H

  catch(Ece*to0 e)

 

  e.*r0tStacTrace()?

  H

  H

  H

H

Page 15: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 15/28

O4T(4T1

".Serer 

#.Ce0t

Page 16: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 16/28

E(ERI/ENT09

)I/1 rte a *ro+ram to se01 /e /rom serer to ce0t us0+ TC8 Socet *ro+ramm0+.

DESCRI(TION1

The 9aa.o.Bu//ere1Rea1er cass rea1s tet /rom a character-0*ut stream> !u//er0+ characters so as

to *ro1e /or the e//ce0t rea10+ o/ characters> arra6s> a01 0es. The !u//er s3e ma6 !e s*ec/e1> or the

1e/aut s3e ma6 !e use1.Each rea1 reQuest ma1e o/ a Rea1er causes a corres*o010+ rea1 reQuest to !e

ma1e o/ the u01er60+ character or !6te stream.

Cass Co0stuctors:

Bu//ere1Rea1er(Rea1er 0)

Bu//ere1Rea1er(Rea1er 0> 0ts3)

Metho1s

0t rea1()-Ths metho1 rea1s a s0+e character.

0t rea1(char c!u/> 0t o//> 0te0)-Ths metho1 rea1s characters 0to a *orto0 o/ a0 arra6.Str0+ rea1=0e()-Ths metho1 rea1s a 0e o/ tet

CODE1

".@romSerer.<aa

m*ort <aa.o.?

m*ort <aa.0et.?

 *u!c cass @romSerer

  *u!c /0a statc 0t SCKET8RT D %?

 *u!c /0a statc Str0+ @I=ETSEN; D C:LLUsersLL0trLL;esto*LLN8 =a!LL/e".tt?

 *u!c statc o1 ma0(Str0+ ar+s) thro,s IEce*to0

 

@eI0*utStream /s D 0u?

  Bu//ere1I0*utStream !s D 0u?

  ut*utStream os D 0u?

  SererSocet Serer D 0u?

  Socet socet D 0u?  tr6

  Serer D 0e, SererSocet(SCKET8RT)?

  ,he (true)

 

  S6stem.out.*r0t0(at0+...)?

  tr6

  socet D Serer.acce*t()?  S6stem.out.*r0t0(Acce*te1 co00ecto0 : J socet)?

22 se01 /e

Page 17: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 17/28

  @e m6@e D 0e, @e (@I=ETSEN;)?

  !6te m6!6tearra6 D 0e, !6te (0t)[email protected]+th()?

  /s D 0e, @eI0*utStream(m6@e)?

  !s D 0e, Bu//ere1I0*utStream(/s)?

  !s.rea1(m6!6tearra6>%>m6!6tearra6.e0+th)?

  os D socet.+etut*utStream()?

  S6stem.out.*r0t0(Se010+ J @I=ETSEN; J ( J m6!6tearra6.e0+th J !6tes))?

  os.,rte(m6!6tearra6>%>m6!6tearra6.e0+th)?

  os./ush()?

  S6stem.out.*r0t0(;o0e.)?

  H

  /0a6

  / (!s GD 0u)

 !s.cose()?

  / (os GD 0u)

os.cose()?  / (socetGD0u)

socet.cose()?

  H

  H

  H

  /0a6

  / (Serer GD 0u)

Serer.cose()?

  H

  H

H

#.ToCe0t.<aa

m*ort <aa.o.?

m*ort <aa.0et.?

 *u!c cass ToCe0t

  *u!c /0a statc 0t SCKET8RT D %?

 *u!c /0a statc Str0+ SERVER D "#7.%.%."?

 *u!c /0a statc Str0+ @I=ETRECEIVE; D C:LLUsersLL0trLL;esto*LLN8 =a!LL/e"co*6#.tt?

 *u!c /0a statc 0t @I=ESIE D 5%##4'5?

 *u!c statc o1 ma0(Str0+ ar+s) thro,s Ece*to0

 

0t !6tesRea1?

  0t curre0t D %?

@eut*utStream /os D 0u?

Page 18: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 18/28

  Bu//ere1ut*utStream !os D 0u?

  Socet socet D 0u?

  tr6

  socet D 0e, Socet(SERVER> SCKET8RT)?

  S6stem.out.*r0t0(Co00ect0+...)?

  22 recee /e

  !6te m6!6tearra6 D 0e, !6te @I=ESIE?

  I0*utStream s D socet.+etI0*utStream()?

  /os D 0e, @eut*utStream(@I=ETRECEIVE;)?

  !os D 0e, Bu//ere1ut*utStream(/os)?

  !6tesRea1 D s.rea1(m6!6tearra6>%>m6!6tearra6.e0+th)?

  curre0t D !6tesRea1?

  1o

  !6tesRea1 D s.rea1(m6!6tearra6> curre0t> (m6!6tearra6.e0+th-curre0t))?

  /(!6tesRea1 D %)

curre0t JD !6tesRea1?

  H ,he(!6tesRea1 -")?

  !os.,rte(m6!6tearra6> % > curre0t)?

  !os./ush()?

  S6stem.out.*r0t0(@e J @I=ETRECEIVE; J 1o,0oa1e1 ( J curre0t J !6tes rea1))?

  H

  /0a6

  / (/os GD 0u)

/os.cose()?

  / (!os GD 0u)

 !os.cose()?

  / (socet GD 0u)

socet.cose()?

  H

HH

Page 19: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 19/28

O4T(4T1

Serer 

Ce0t

Page 20: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 20/28

E(ERI/ENT 5 #

)I/1 rte a *ro+ram to m*eme0t U;8 Socet 8ro+ramm0+ 0 <aa.

DESCRI(TION1The ;ata+ram8acet a01 ;ata+ramSocet casses 0 the <aa.0et *aca+e m*eme0t s6stem-01e*e01e0t

1ata+ram commu0cato0 us0+ U;8.;ata+ramSocet> ;ata+ram8acet> a01 MutcastSocet are use1 to

se01 a01 recee *acets oer 0et,or.A0 a**cato0 ca0 se01 a01 recee ;ata+ram8acets throu+h a

;ata+ramSocet. ;ata+ram8acets ca0 !e !roa1cast to mut*e rec*e0ts a ste00+ to a MutcastSocet.

CODE1

".U;8Serer.<aa

 *aca+e u1*socet?

m*ort <aa.0et.?m*ort <aa.o.?

 *u!c cass U;8Serer 

  *u!c statc o1 ma0(Str0+ ar+s) thro,s Ece*to0

 

  ;ata+ramSocet sererSocet D 0e, ;ata+ramSocet('75)?

  !6te recee;ata D 0e, !6te"%#&?

  !6te se01;ata D 0e, !6te"%#&?

  ,he(true)

    ;ata+ram8acet recee8acet D 0e, ;ata+ram8acet(recee;ata> recee;ata.e0+th)?

  sererSocet.recee(recee8acet)?

  Str0+ se0te0ce D 0e, Str0+( recee8acet.+et;ata())?

  S6stem.out.*r0t0(RECEIVE;: J se0te0ce)?

  I0etA11ress I8A11ress D recee8acet.+etA11ress()?

  0t *ort D recee8acet.+et8ort()?

  Str0+ ca*ta3e1Se0te0ce D se0te0ce.toU**erCase()?

  se01;ata D ca*ta3e1Se0te0ce.+etB6tes()?

  ;ata+ram8acet se018acet D

  0e, ;ata+ram8acet(se01;ata> se01;ata.e0+th> I8A11ress> *ort)?

  sererSocet.se01(se018acet)?  H

  H

H

#.U;8Ce0t.<aa

 *aca+e u1*socet?

m*ort <aa.0et.;ata+ram8acet?

m*ort <aa.0et.?

m*ort <aa.o.?

Page 21: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 21/28

 *u!c cass U;8Ce0t

  *u!c statc o1 ma0(Str0+ ar+s) thro,s Ece*to0

 

  Bu//ere1Rea1er 0@romUser D

  0e, Bu//ere1Rea1er(0e, I0*utStreamRea1er(S6stem.0))?

  ;ata+ramSocet ce0tSocet D 0e, ;ata+ramSocet()?

  I0etA11ress I8A11ress D I0etA11ress.+etB6Name(ocahost)?

  !6te se01;ata D 0e, !6te"%#&?  !6te recee;ata D 0e, !6te"%#&?

  Str0+ se0te0ce D [email protected]=0e()?

  se01;ata D se0te0ce.+etB6tes()?

  ;ata+ram8acet se018acet D 0e, ;ata+ram8acet(se01;ata> se01;ata.e0+th> I8A11ress> '75)?

  ce0tSocet.se01(se018acet)?

  ;ata+ram8acet recee8acet D 0e, ;ata+ram8acet(recee;ata> recee;ata.e0+th)?

  ce0tSocet.recee(recee8acet)?

  Str0+ mo1/e1Se0te0ce D 0e, Str0+(recee8acet.+et;ata())?

  S6stem.out.*r0t0(@RM SERVER: J mo1/e1Se0te0ce)?

  ce0tSocet.cose()?

  H

H

O4T(4T1

U;8Serer 

U;8Ce0t

Page 22: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 22/28

E(ERI/ENT 5 :

)I/1 rte a *ro+ram to m*eme0t RMI 0 9aa.

DESCRI(TION1The 9aa Remote Metho1 I0ocato0 (9aa RMI) s a 9aa A8I that *er/orms the o!<ect-

ore0te1 eQuae0t o/ remote *roce1ure cas (R8C)> ,th su**ort /or 1rect tra0s/er o/ sera3e1 9aa

casses a01 1str!ute1 +ar!a+e coecto0.

  The or+0a m*eme0tato0 1e*e01s o0 9aa Vrtua Mach0e (9VM) cass re*rese0tato0

mecha0sms a01 t thus o06 su**orts ma0+ cas /rom o0e 9VM to a0other. The *rotoco u01er60+ ths

9aa-o06 m*eme0tato0 s 0o,0 as 9aa Remote Metho1 8rotoco (9RM8

CODE1

".A11er I0ter/ace

m*ort <aa.rm.?

 *u!c 0ter/ace A11er ete01s Remote

 

 *u!c 0t a11(0t >0t 6)thro,s RemoteEce*to0?

H

#.A11erRemote.<aa

m*ort <aa.rm.?

m*ort <aa.rm.serer.U0castRemote!<ect?

m*ort <aa.rm.RemoteEce*to0?

 *u!c cass A11erRemote ete01s U0castRemote!<ect m*eme0ts A11er 

A11erRemote() thro,s RemoteEce*to0

su*er()?

H

 *u!c 0t a11(0t >0t 6)

S6stem.out.*r0t0(A110+ the 0um!ers )?

retur0 J6?

HH

4.M6Serer.<aa

  m*ort <aa.rm.?

m*ort <aa.0et.?

  m*ort <aa.o.? 

 *u!c cass M6Serer 

 

 *u!c statc o1 ma0(Str0+ ar+s) thro,s RemoteEce*to0> Ma/orme1UR=Ece*to0> NotBou01Ece*to0

Page 23: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 23/28

tr6

A11er stu!D0e, A11erRemote()?

 Nam0+.re!01(rm:22ocahost:$%%%2so0oo>stu!)?

H

catch(Ece*to0 e)

S6stem.out.*r0t0(e)?

HH

H

&.M6Ce0t.<aa

  m*ort <aa.rm.?

m*ort <aa.0et.?

m*ort <aa.o.?

m*ort <aa.ut.?

 *u!c cass M6Ce0t

 *u!c statc o1 ma0(Str0+ ar+s) thro,s RemoteEce*to0> Ma/orme1UR=Ece*to0>

 NotBou01Ece*to0> IEce*to0

tr6

Sca00er sc D 0e, Sca00er(S6stem.0)?

S6stem.out.*r0t0(+e the 0um!ers 6ou ,a0t to a11)?

0t D sc.0etI0t()?

0t 6 D sc.0etI0t()?A11er stu!D(A11er)Nam0+.oou*(rm:22ocahost:$%%%2so0oo)?

S6stem.out.*r0t0(resut D J stu!.a11(>6))?

H

catch(Ece*to0 e)

S6stem.out.*r0t0(e)?

H

H

H

Page 24: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 24/28

O4T(4T1

M6Serer 

M6Ce0t

Page 25: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 25/28

E(ERI/ENT0; 

)I/1rte a *ro+ram to 1s*a6 S6stem Vara!es.

DESCRI(TION1

  E0ro0me0t ara!es are a set o/ 160amc 0ame1 aues that ca0 a//ect the ,a6ru000+ *rocesses , !ehae o0 a com*uter.The6 are *art o/ the e0ro0me0t 0 ,hch a

 *rocess ru0s.

S6stem.+ete0(Str0+ 0ame) metho1 +ets the aue o/ the s*ec/e1 e0ro0me0tara!e. A0 e0ro0me0t ara!e s a s6stem-1e*e01e0t eter0a 0ame1 aue.

E0ro0me0t ara!es shou1 !e use1 ,he0 a +o!a e//ect s 1esre1> or ,he0 a0

eter0a s6stem 0ter/ace reQures a0 e0ro0me0t ara!e (such as 8ATF).

DECL)R)TION

 *u!c statc Str0+ +ete0(Str0+ 0ame)

8arameters:

0ame -Ths s the 0ame o/ the e0ro0me0t ara!e.Retur0 Vaue:

Ths metho1 retur0s the str0+ aue o/ the ara!e> or 0u / the ara!e s 0ot 1e/0e10 the s6stem e0ro0me0t.

CODE1

m*ort <aa.ut.Ma*?

 *u!c cass e0Ma*

  *u!c statc o1 ma0 (Str0+ ar+s)   Ma*Str0+> Str0+ e0 D S6stem.+ete0()?

  /or (Str0+ e0Name : e0.e6Set())   S6stem.out./ormat(sDs0>  e0Name>

  e0.+et(e0Name))?

  H  H

H

Page 26: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 26/28

O4T(4T1

Page 27: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 27/28

E(ERI/ENT 5 ;

)I/1 rte a *ro+ram to m*eme0t UR= *ro+ramm0+ 0 9aa.

DESCRI(TION1 UR= sta01s /or U0/orm Resource =ocator a01 re*rese0ts a resource

o0 the or1 1e e!> such as a e! *a+e or @T8 1rector6.

  Ths secto0 sho,s 6ou ho, to ,rte 9aa *ro+rams that commu0cate ,th a UR=.  

The 2a3a-net-4RL cass re*rese0ts a UR= a01 has com*ete set o/ metho1s to ma0*uate

UR= 0 9aa.

CODE1

22 @e Name : UR=Co00;emo.<aa

m*ort <aa.0et.?

m*ort <aa.o.?

 *u!c cass UR=Co00;emo

  *u!c statc o1 ma0(Str0+ ar+s)

    tr6

 

  UR= ur D 0e, UR=(htt*:22,,,.amroo1.com)?

  UR=Co00ecto0 urCo00ecto0 D ur.o*e0Co00ecto0()?  Ftt*UR=Co00ecto0 co00ecto0 D 0u?

  /(urCo00ecto0 0sta0ceo/ Ftt*UR=Co00ecto0)

    co00ecto0 D (Ftt*UR=Co00ecto0) urCo00ecto0?

  H

  ese 

  S6stem.out.*r0t0(8ease e0ter a0 FTT8 UR=.)?

  retur0?  H

  Bu//ere1Rea1er 0 D 0e, Bu//ere1Rea1er(

Page 28: NP Lab Manual.docx

7/23/2019 NP Lab Manual.docx

http://slidepdf.com/reader/full/np-lab-manualdocx 28/28

  0e, I0*utStreamRea1er(co00ecto0.+etI0*utStream()))?

  Str0+ urStr0+ D ?

  Str0+ curre0t?  ,he((curre0t D 0.rea1=0e()) GD 0u)

 

  urStr0+ JD curre0t?  H

  S6stem.out.*r0t0(urStr0+)?

  Hcatch(IEce*to0 e) 

  e.*r0tStacTrace()?

  H

  HH

O4T(4T1