Вы находитесь на странице: 1из 22

Program No.

1
//WAP to simulate the functionality of Lamport's Logical clock in C. #include<stdio.h> #include<conio.h> #include<iostream.h> #include<stdlib.h> #include<graphics.h> #include<string.h> #include<dos.h> void main(){ int s[4][9],n,m !" int i,#,ne$t !,step !" int msg[%!][4] {!&,totmsg" char op" int pi,p#,ei,e#" clrscr()" cout<<'(n)rogram *or +amport +ogical ,loc-'" cout<<'(n.nter /umber 0* )rocess '" cin>>n" *or(i !"i<n"i11){ cout<<'(n.nter number o* 2343.2 o* process )'<<i<<' cin>>s[i][5]" *or(# %"#< s[i][5]"#11){ s[i][#] #" & & do{ cout<<'(n.nter message transit'" cout<<'(n6708 9>(n.nter )rocess /umber )'" cin>>msg[m][!]" cout<<'(n.nter .vent /umber e'" cin>>msg[m][%]" cout<<'(n30 9>(n.nter )rocess /umber )'" cin>>msg[m][:]" cout<<'(n.nter .vent /umber e'" cin>>msg[m][;]" cout<<'(n(n)ress <=< to continue'" op getch()" cout<<op" m11" totmsg m" &>hile(op <=<)" m !" *or (i !"i<totmsg"i11){ pi msg[i][!]"

'"

& int gd ?.3.,3,gm" initgraph(@gd,@gm,',A((3,((BCD')" outte$t$=(:!!,%E,')rogram 6or +amport +ogical ,loc-')" FFdra>ing process and events *or(i !"i<n"i11){ charG p%" itoa(i,p%,%!)" outte$t$=(E,%!!1ne$t,')')" outte$t$=(%;,%!!1ne$t,p%)" line(%!!,%!!1ne$t,H!!,%!!1ne$t)" *or(# %"#< s[i][5]"#11){ charG p:" itoa(#,p:,%!)" outte$t$=(%!!1step,9!1ne$t,'e')" outte$t$=(%%!1step,9!1ne$t,p:)" FFtimestamp charG p;" itoa(s[i][#]9%,p;,%!)" outte$t$=(%!!1step,%%!1ne$t,'t')" outte$t$=(%%!1step,%%!1ne$t,p;)" circle(%!E1step,%!!1ne$t,E)" step1 E!" & step !" ne$t1 %!!" & dela=(:!!!)" FFdra>ing message transit *or(m !"m<totmsg"m11){ setlinest=le(20+D?I+D/.,%,;)" setcolor(m14)" line(msg[m][%]GE!1E!,msg[m][!]G%!!1%!!,msg[m] [;]GE!1E!,msg[m][:]G%!!1%!!)" i* (msg[m][:]>msg[m][!]){ line(msg[m][;]GE!1E!,msg[m][:]G%!!1%!!,msg[m] [;]GE!1E!,msg[m][:]G%!!19!)" line(msg[m][;]GE!1E!,msg[m][:]G%!!1%!!,msg[m] [;]GE!14!,msg[m][:]G%!!19!)" & else{ line(msg[m][;]GE!1E!,msg[m][:]G%!!1%!!,msg[m] [;]GE!1E!,msg[m][:]G%!!1%%!)"

ei msg[i][%]" p# msg[i][:]" e# msg[i][;]" i*(s[p#][e#]< (s[pi][ei]1%)){ s[p#][e#] s[pi][ei]1%" *or (# e#1%"#< s[p#][5]"#11){ s[p#][#] s[p#][#9%]1%" & &

line(msg[m][;]GE!1E!,msg[m][:]G%!!1%!!,msg[m] [;]GE!14!,msg[m][:]G%!!1%%!)" & & getch()" &

Output

Program No.2

//WAP to mplement !ector clock in C. #include<stdio.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> long Gp%(int i,long Gcomp)" long Gp:(int i,long Gcomp)" long Gp;(int i,long Gcomp)" void main() { long start[] {!,!,!&,Gvector" clrscr()" >hile(J-bhit()) { p%(%,@start[!])" & print*('(n )rocess Kector(n')" vector p%(!,@start[!])" print*('p%[LldLldLld](n',Gvector,G(vector1%),G(vector1:))" vector p:(!,@start[!])" print*('p:[LldLldLld](n',Gvector,G(vector1%),G(vector1:))" vector p;(!,@start[!])" print*('p;[LldLldLld](n',Gvector,G(vector1%),G(vector1:))" & long Gp%(int i,long Gcomp) { static long a[] {!,!,!&" int ne$t" i*(i %) { a[!]11" i*(G(comp1%)>a[%]) a[%] G(comp1%)" i*(G(comp1:)>a[:]) a[:] G(comp1:)" ne$t random(:)" i*(ne$t !) p:(%,@a[!])" else i*(ne$t %) p;(%,@a[!])" return(@a[!])" & else return(@a[!])" & long Gp:(int i,long Gcomp) { static long b[] {!,!,!&" int ne$t"

i*(i {

%) b[i]11" i*(Gcomp>b[!]) b[!] G(comp)" i*(G(comp1:)>b[:]) b[:] G(comp1:)" ne$t random(:)" i*(ne$t !) p%(%,@b[!])" else i*(ne$t %) p;(%,@b[!])" return @b[!]" return @b[!]"

& else

& long Gp;(int i,long Gcomp) { static long c[] {!,!,!&" int ne$t" i*(i %) { c[:]11" i*(Gcomp>c[!]) c[!] G(comp)" i*(G(comp1%)>c[%]) c[%] G(comp1%)" ne$t random(:)" i*(ne$t !) p%(%,@c[!])" return @c[!]" & else return @c[!]" &

Program No."

##$imulation of %istri&ute' mutual e(clusion in )a*a.


import Mtilities.G" import 2=nchroniNation.G" class 8essage { public int number, id" public 8essage(int number, int id) { this.number & class /ode e$tends 8=0b#ect implements 7unnable { private static *inal int 84D/ private int >hich0ne !" private private private private private private private private private private private private private private private !, 7.OM.232 %, 7.)+D.2 :" number" this.id id"&

int id 9%" int num/odes 9%" int nap0utside,2 !" FF both are in int napDnside,2 !" FF milliseconds 8essage)assing[] rePuest,hannel null" 8essage)assing[] repl=,hannel null" 8essage)assing rePuests3o8e null" 8essage)assing replies3o8e null" int number !" int high/umber !" boolean rePuesting *alse" int repl=,ount !" Binar=2emaphore s ne> Binar=2emaphore(%)" Binar=2emaphore >a-eMp ne> Binar=2emaphore(!)" boolean[] de*erred null"

public /ode(2tring name, int id, int num/odes, int nap0utside,2, int napDnside,2, 8essage)assing[] rePuest,hannel, 8essage)assing repl=,hannel[], 8essage)assing rePuests3o8e, 8essage)assing replies3o8e) { super(name 1 ' ' 1 id)" this.id id" this.num/odes num/odes" this.nap0utside,2 nap0utside,2" this.napDnside,2 napDnside,2" this.rePuest,hannel rePuest,hannel" this.repl=,hannel repl=,hannel" this.rePuests3o8e rePuests3o8e" this.replies3o8e replies3o8e" de*erred ne> boolean[num/odes]" *or (int i !" i < num/odes" i11) de*erred[i] *alse" 2=stem.out.println(get/ame() 1 ' is alive, nap0utside,2 ' 1 nap0utside,2 1 ', napDnside,2 ' 1 napDnside,2)" ne> 3hread(this).start()" & public void run() { FF start three di**erent threads in the same ob#ect int me?o >hich0ne11" i* (me?o 84D/) { ne> 3hread(this).start()" main()" & else i* (me?o 7.OM.232) {

&

ne> 3hread(this).start()" handle7ePuests()" & else i* (me?o 7.)+D.2) { handle7eplies()" &

private void choose/umber() { )(s)" rePuesting true" number high/umber 1 %" K(s)" & private void send7ePuest() { repl=,ount !" *or (int # !" # < num/odes" #11) i* (# J id) send(rePuest,hannel[#], ne> 8essage(number, id))" & private void >ait6or7epl=() { )(>a-eMp)" & private void repl=3o?e*erred/odes() { )(s)" rePuesting *alse" K(s)" *or (int # !" # < num/odes" #11) { i* (de*erred[#]) { de*erred[#] *alse" send(repl=,hannel[#], id)" & & & private void outside,2() { int napping" napping ((int) random(nap0utside,2)) 1 %" 2=stem.out.println('age() ' 1 age() 1 ', ' 1 get/ame() 1 ' napping outside ,2 *or ' 1 napping 1 ' ms')" nap(napping)" & private void inside,2() { int napping" napping ((int) random(napDnside,2)) 1 %" 2=stem.out.println('age() ' 1 age() 1 ', ' 1 get/ame() 1 ' napping inside ,2 *or ' 1 napping 1 ' ms')" nap(napping)" & private void main() { >hile (true) { outside,2()" 2=stem.out.println('age() ' 1 age() 1 ', node ' 1 id 1 ' >ants to enter its critical section')"

& &

choose/umber()" FF )7.9)7030,0+ send7ePuest()" FF ' >ait6or7epl=()" FF ' inside,2()" 2=stem.out.println('age() ' 1 age() 1 ', node ' 1 id 1 ' has no> le*t its critical section')" repl=3o?e*erred/odes()" FF )0239)7030,0+

private void handle7ePuests() { >hile (true) { 8essage m (8essage) receive(rePuests3o8e)" int received/umber m.number" int receivedD? m.id" high/umber 8ath.ma$(high/umber, received/umber)" )(s)" boolean decide3o?e*er rePuesting @@ (number < received/umber QQ (number received/umber @@ id < receivedD?))" i* (decide3o?e*er) de*erred[receivedD?] true" else send(repl=,hannel[receivedD?], id)" K(s)" & & private void handle7eplies() { >hile (true) { int receivedD? receiveDnt(replies3o8e)" repl=,ount11" i* (repl=,ount num/odes 9 %) K(>a-eMp)" & & & class ?istributed8utual.$clusion e$tends 8=0b#ect { public static void main(2tring[] args) { FF parse command line options, i* an=, to override de*aults Cet0pt go ne> Cet0pt(args, 'MnA7A')" 2tring usage 'MsageA 9n num/odes 97 run3ime' 1 ' nap0utside,2[i] napDnside,2[i] i !,%,...'" go.opt.rr true" int ch 9%" int num/odes E" int run3ime H!" FF seconds >hile ((ch go.getopt()) J go.opt.06) { i* ((char)ch <M<) { 2=stem.out.println(usage)" 2=stem.e$it(!)" & else i* ((char)ch <n<) num/odes go.process4rg(go.opt4rgCet(), num/odes)" else i* ((char)ch <7<) run3ime go.process4rg(go.opt4rgCet(), run3ime)" else { 2=stem.err.println(usage)" 2=stem.e$it(%)" &

& 2=stem.out.println('?istributed8utual.$clusionA num/odes ' 1 num/odes 1 ', run3ime ' 1 run3ime)" FF process non9option command line arguments int[] nap0utside,2 ne> int[num/odes]" int[] napDnside,2 ne> int[num/odes]" int arg/um go.optDnde$Cet()" *or (int i !" i < num/odes" i11) { nap0utside,2[i] go.tr=4rg(arg/um11, 5)" napDnside,2[i] go.tr=4rg(arg/um11, :)" & FF create communication channels 8essage)assing[] rePuest,hannel null, repl=,hannel null, rePuest,hannel2 null, rePuest,hannel7 null, repl=,hannel2 null, repl=,hannel7 null" rePuest,hannel ne> 8essage)assing[num/odes]" repl=,hannel ne> 8essage)assing[num/odes]" rePuest,hannel2 ne> 8essage)assing[num/odes]" repl=,hannel2 ne> 8essage)assing[num/odes]" rePuest,hannel7 ne> 8essage)assing[num/odes]" repl=,hannel7 ne> 8essage)assing[num/odes]" *or (int i !" i < num/odes" i11) { rePuest,hannel[i] ne> 4s=nc8essage)assing()" repl=,hannel[i] ne> 4s=nc8essage)assing()" rePuest,hannel2[i] ne> 8essage)assing2end0nl=(rePuest,hannel[i])" repl=,hannel2[i] ne> 8essage)assing2end0nl=(repl=,hannel[i])" rePuest,hannel7[i] ne> 8essage)assing7eceive0nl=(rePuest,hannel[i])" repl=,hannel7[i] ne> 8essage)assing7eceive0nl=(repl=,hannel[i])" & FF create the /odes (the= start their o>n threads) *or (int i !" i < num/odes" i11) ne> /ode('/ode', i, num/odes, nap0utside,2[i]G%!!!, napDnside,2[i]G%!!!, rePuest,hannel2, repl=,hannel2, rePuest,hannel7[i], repl=,hannel7[i])" 2=stem.out.println('4ll /odes created')" FF let the /odes run *or a >hile nap(run3imeG%!!!)" 2=stem.out.println('age() ' 1 age() 1 ', time to stop the threads and e$it')" 2=stem.e$it(!)"

& &

Output+
?A()ra-ash(Rava(7/?(4dvanced>#avac dimu.#ava

?A( )ra-ash(Rava(7/?(4dvanced >#ava ?istributed8utual.$clusion 97:! ?istributed8utual.$clusionA num/odes E, run3ime :! /ode ! is alive, nap0utside,2 5!!!, napDnside,2 :!!! /ode % is alive, nap0utside,2 5!!!, napDnside,2 :!!! /ode : is alive, nap0utside,2 5!!!, napDnside,2 :!!! /ode ; is alive, nap0utside,2 5!!!, napDnside,2 :!!! /ode 4 is alive, nap0utside,2 5!!!, napDnside,2 :!!! age() %S!, /ode % napping outside ,2 *or :S%9 ms age() %S!, /ode : napping outside ,2 *or :S9 ms 4ll /odes created age() %S!, /ode ; napping outside ,2 *or :;EE ms age() ::!, /ode ! napping outside ,2 *or :;9; ms age() ::!, /ode 4 napping outside ,2 *or 5 ms age() ::!, node 4 >ants to enter its critical section age() ;;!, /ode 4 napping inside ,2 *or 9%% ms age() 44!, node : >ants to enter its critical section age() %:H!, node 4 has no> le*t its critical section age() %:H!, /ode 4 napping outside ,2 *or 4!4: ms age() %:H!, /ode : napping inside ,2 *or %5; ms age() %45!, node : has no> le*t its critical section age() %45!, /ode : napping outside ,2 *or S;;E ms age() :E;!, node ; >ants to enter its critical section age() :E;!, /ode ; napping inside ,2 *or S4% ms age() :E5!, node ! >ants to enter its critical section age() :5H!, node % >ants to enter its critical section age() ;;!!, node ; has no> le*t its critical section age() ;;!!, /ode ; napping outside ,2 *or H549 ms age() ;;!!, /ode ! napping inside ,2 *or %S%! ms age() E!!!, node ! has no> le*t its critical section age() E!!!, /ode ! napping outside ,2 *or E:E; ms age() E!!!, /ode % napping inside ,2 *or %H94 ms age() E;;!, node 4 >ants to enter its critical section age() HS!!, node % has no> le*t its critical section age() HS!!, /ode % napping outside ,2 *or ;!H; ms age() HS!!, /ode 4 napping inside ,2 *or ;9S ms age() S%4!, node 4 has no> le*t its critical section age() S%4!, /ode 4 napping outside ,2 *or ;H5S ms age() 5S9!, node : >ants to enter its critical section age() 5S9!, /ode : napping inside ,2 *or %!: ms age() 59!!, node : has no> le*t its critical section age() 59!!, /ode : napping outside ,2 *or %%S4 ms age() 9S5!, node % >ants to enter its critical section age() 9S5!, /ode % napping inside ,2 *or %H%S ms age() %!%%!, node : >ants to enter its critical section age() %!%H!, node ; >ants to enter its critical section age() %!:S!, node ! >ants to enter its critical section age() %!5:!, node 4 >ants to enter its critical section age() %%4;!, node % has no> le*t its critical section age() %%4;!, /ode % napping outside ,2 *or E;:H ms age() %%4;!, /ode : napping inside ,2 *or H:5 ms age() %:!9!, node : has no> le*t its critical section age() %:!9!, /ode : napping outside ,2 *or 49S! ms age() %:!9!, /ode ; napping inside ,2 *or E4E ms age() %:H;!, node ; has no> le*t its critical section age() %:H;!, /ode ; napping outside ,2 *or S959 ms age() %:H;!, /ode ! napping inside ,2 *or 9!4 ms

age() age() age() age() age() age() age() age() age() age() age() age() age() age() age() age() age() age() age() age() age() age()

%;E%!, %;E%!, %;E%!, %E!!!, %E!!!, %HSE!, %HSE!, %H5H!, %H5H!, %S!;!, %S!;!, %S%4!, %S%4!, %SE5!, %SE5!, %SH9!, %9:5!, %9:5!, %9:5!, :!%H!, :!%H!, :!%H!,

node /ode /ode node /ode node /ode node /ode node /ode node /ode node /ode node node /ode /ode node /ode time

! has no> le*t its critical section ! napping outside ,2 *or 4%H: ms 4 napping inside ,2 *or %44! ms 4 has no> le*t its critical section 4 napping outside ,2 *or :ES5 ms % >ants to enter its critical section % napping inside ,2 *or %:; ms % has no> le*t its critical section % napping outside ,2 *or ;S!9 ms : >ants to enter its critical section : napping inside ,2 *or 9S ms : has no> le*t its critical section : napping outside ,2 *or S9!% ms 4 >ants to enter its critical section 4 napping inside ,2 *or %H9E ms ! >ants to enter its critical section 4 has no> le*t its critical section 4 napping outside ,2 *or ;SE% ms ! napping inside ,2 *or 5H9 ms ! has no> le*t its critical section ! napping outside ,2 *or H459 ms to stop the threads and e$it ... end o* e$ample run(s)

GF

Program No.,
mplement a 'istri&ute' chat ser*er using -CP sockets in )a*a.

1.Server.java
import #ava.net.G" import #ava.io.G" public class server{ public static void main(2tring args[])thro>s D0.$ception{ 2erver2oc-et s% null" tr={ s% ne> 2erver2oc-et(95)" &catch(.$ception e){ 2=stem.out.println(')ort not *ound')" e.print2tac-3race()" & 2oc-et c null" tr={ c s%.accept()" 2=stem.out.println(',onnection *rom'1c)" &catch(.$ception e){ 2=stem.out.println('not accepted')" e.print2tac-3race()" & )rintTriter out ne> )rintTriter(c.get0utput2tream(),true)" Bu**ered7eaderin ne> Bu**ered7eader(ne> Dnput2tream7eader(c.getDnput2tream()))" 2tring l" Bu**ered7eader sin ne> Bu**ered7eader(ne> Dnput2tream7eader(2=stem.in))" 2=stem.out.println('D am read= t=pe no>')" >hile((l sin.read+ine())J null){ out.println(l)" & out.close()" sin.close()" c.close()" s%.close()" & &

2.Client.)a*a
import #ava.net.G" import #ava.io.G"

public class client{ public static void main(2tring args[])thro>s D0.$ception{ 2oc-et s null" Bu**ered7eader b null" tr={ s ne> 2oc-et(Dnet4ddress.get+ocalUost(),95)" b ne> Bu**ered7eader(ne> Dnput2tream7eader(s.getDnput2tream()))" &catch(.$ception e){ 2=stem.out.println('D do not host')" e.print2tac-3race()" & 2tring inp" >hile((inp b.read+ine())J null){ 2=stem.out.println(inp)" & b.close()" s.close()" & & Running the application Open two cmd prompt and follow these 1.java Server 2.java client Output D:\Prakash\RND\ ava\Net!orkin"\#hatServer$java server #onnection fromSocket%addr&'12(.).).1*port&112)*localport&+,. am read/ t/pe now 0ello how r 12 d1de3 D:\Prakash\RND\ ava\Net!orkin"\#hatServer$java client 0ello how r 12 d1de3

Program No..
mplement/0a*a 12 3mechanism for accessing metho's of remote systems.

1.#alc1lator.mpl.java

public class ,alculatorDmpl e$tends #ava.rmi.server.Mnicast7emote0b#ect implements ,alculator { public ,alculatorDmpl() thro>s #ava.rmi.7emote.$ception { super()" & public long add(long a, long b) thro>s #ava.rmi.7emote.$ception { return a 1 b" & public long sub(long a, long b) thro>s #ava.rmi.7emote.$ception { return a 9 b" & public long mul(long a, long b) thro>s #ava.rmi.7emote.$ception { return a G b" & public long div(long a, long b) thro>s #ava.rmi.7emote.$ception { return a F b" & &

2.#alc1lator.java
public inter*ace ,alculator e$tends #ava.rmi.7emote { public long add(long a, long b) thro>s #ava.rmi.7emote.$ception" public long sub(long a, long b) thro>s #ava.rmi.7emote.$ception" public long mul(long a, long b) thro>s #ava.rmi.7emote.$ception" public long div(long a, long b) thro>s #ava.rmi.7emote.$ception" &

4.#alc1latorServer.java
import #ava.rmi./aming" public class ,alculator2erver { public ,alculator2erver() { tr= {

& public static void main(2tring args[]) { ne> ,alculator2erver()" & &

,alculator c ne> ,alculatorDmpl()" /aming.rebind('rmiAFFlocalhostA%!99F,alculator2ervice', c)" & catch (.$ception e) { 2=stem.out.println('3roubleA ' 1 e)" &

5.#alc1lator#lient.java
import #ava.rmi./aming" import #ava.rmi.7emote.$ception" import #ava.net.8al*ormedM7+.$ception" import #ava.rmi./otBound.$ception" public class ,alculator,lient { public static void main(2tring[] args) { tr= { ,alculator c (,alculator) /aming.loo-up('rmiAFFlocalhostF,alculator2e rvice')" 2=stem.out.println( c.sub(4, ;) )" 2=stem.out.println( c.add(4, E) )" 2=stem.out.println( c.mul(;, H) )" 2=stem.out.println( c.div(9, ;) )" & catch (8al*ormedM7+.$ception murle) { 2=stem.out.println()" 2=stem.out.println('8al*ormedM7+.$ception')" 2=stem.out.println(murle)" & catch (7emote.$ception re) { 2=stem.out.println()" 2=stem.out.println('7emote.$ception')" 2=stem.out.println(re)" & catch (/otBound.$ception nbe) { 2=stem.out.println()" 2=stem.out.println( '/otBound.$ception')" 2=stem.out.println(nbe)" & catch ( #ava.lang.4rithmetic.$ception ae) { 2=stem.out.println()" 2=stem.out.println( '#ava.lang.4rithmetic.$ception')" 2=stem.out.println(ae)" & &

& Running The Application: D:\Prakash\RND\ ava\Net!orkin"\R6.$rmic #alc1lator.mpl Now open three cmd prompts and follow these at each. 1. D:\Prakash\RND\ ava\Net!orkin"\R6.$Rmire"istr/ 2. D:\Prakash\RND\ ava\Net!orkin"\R6.$java #alc1latorServer 4. D:\Prakash\RND\ ava\Net!orkin"\R6.$java #alc1lator#lient Output:

If all goes well you will see the following output:


1 9 18 3

mplementation of CO15A 6Common O&)ect 1e7uest 5roker Architecture8 mechanism.

Program No.4

1.7ile.nterface.idl
interface FileInterface {

typedef sequence<octet> Data; Data downloadFile(in string fileName); };


Now, let's compile the FileInterface.idl and generate server-side skeletons. Using the command:

D:\Prakash\RND\Java\CORBA

idlj -fserver FileInterface.idl

2.7ileServant.java
import java.io.*; public class FileServant extends _FileInterfaceImplBase { public byte[] downloadFile(String fileName){ File file = new File(fileName); byte buffer[] = new byte[(int)file.length()]; try { BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileName)); input.read(buffer,0,buffer.length); input.close(); } catch(Exception e) { System.out.println("FileServant Error: "+e.getMessage()); e.printStackTrace(); } return(buffer); } }

4.7ileServer.java
import import import import java.io.*; org.omg.CosNaming.*; org.omg.CosNaming.NamingContextPackage.*; org.omg.CORBA.*;

public class FileServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // create the servant and register it with the ORB FileServant fileRef = new FileServant(); orb.connect(fileRef); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // Bind the object reference in naming NameComponent nc = new NameComponent("FileTransfer", " ");

NameComponent path[] = {nc}; ncRef.rebind(path, fileRef); System.out.println("Server started...."); // Wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized(sync){ sync.wait(); } } catch(Exception e) { System.err.println("ERROR: " + e.getMessage()); e.printStackTrace(System.out); } } }

5.7ile#lient.java
import import import import java.io.*; java.util.*; org.omg.CosNaming.*; org.omg.CORBA.*;

public class FileClient { public static void main(String argv[]) { try { // create and initialize the ORB ORB orb = ORB.init(argv, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); NameComponent nc = new NameComponent("FileTransfer", " "); // Resolve the object reference in naming NameComponent path[] = {nc}; FileInterfaceOperations fileRef = FileInterfaceHelper.narrow(ncRef.resolve(path)); if(argv.length < 1) { System.out.println("Usage: java FileClient filename"); } // save the file File file = new File(argv[0]); byte data[] = fileRef.downloadFile(argv[0]); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(argv[0])); output.write(data, 0, data.length); output.flush(); output.close(); } catch(Exception e) { System.out.println("FileClient Error: " + e.getMessage());

e.printStackTrace(); } } }

Running the application


1. 2. 3. 4.

D:\Prakash\RND\ D:\Prakash\RND\ D:\Prakash\RND\ D:\Prakash\RND\

ava\#OR89$tnameserv ava\#OR89$java FileServer ava\#OR89$idlj -fclient FileInterface.idl ava\#OR89$java FileClient hello.txt

Output:

Program No.9
Write a )a*a program for implementing sli'ing :in'o: protocol.

1. slic.java
FF2+D?D/C TD/?0T )7030,0+ V ,+D./3 import #ava.io.G"

import #ava.net.G" public class slic{ public static void main(2tring args[])thro>s .$ception{ 2oc-et s ne> 2oc-et(Wlocal hostX,5555)" Bu**ered 7eader *rom server ne> Bu**er7eader(ne> Dnput2tream7eader(s.getDnput2tream()))" ?ata0utput2tream toserver ne> ?ata0utput2tream(s.get0utput2tream())" Bu**ered7eader d ne> Bu**ered7eader(ne> Dnput2tream7eader(2=stem.in))" 2tring dout,din" 2=stem.out.println(W(tX1*romserver.read+ine())" 2=stem.out.println(Wenter Puit to e$itX)" 2=stem.out.println(Wenter data *or server AX)" Thile(true) { ?out d.read+ine()" D*(dout.ePuals(WPuitX)) Brea-" toserver.>riteB=tes(dout1Y(nY)" din *romserver.read+ine()" 2=stem.out.println(W2erver AX1din)" 2=stem.out.println(W(n.nter *or server AX)" & & &

2. slis.java
FF2+D?D/C TD/?0T )7030,0+ V 2.7K.7 import #ava.io.G" import #ava.net.G" public class slis{ public static void main(2tring args[])thro>s .$ception{ 2erver2oc-et ss ne> 2erver2oc-et(5555)" 2=stem.out.println(W(t >aiting *or clientZX)" 2oc-et client ss.accept()" Bu**ered7eader *romclient ne> Bu**ered7eader(ne> Dnput2tream7eader(client.getDnput2tream()))" ?ata0utput2tream toclient ne> ?ata0utput2tream(client.get0utput2tream())"

Bu**ered7eader d ne> Bu**ered7eader(ne> Dnput2tream7eader(2=stem.in))" 2tring dout,din" toclient.>riteB=tes(W2erver read=Z.X1Y(nY)" >hile(true){ din *romclient.read+ine()" 2=stem.out.println(W(n client dataAX1din)" 2=stem.out.println(Wenter *or client AX)" dout d.read+ine()" i*(dout.ePuals(WPuitX)) brea-" toclient.>riteB=tes(dout1Y(nY)" & & &

Вам также может понравиться