distributed s lab manual2-2

Upload: abhijeetkushwaha

Post on 02-Jun-2018

250 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Distributed S Lab Manual2-2

    1/36

    Lab Manual

    Subject Name Distributed System Lab

    Subject Code- ECS-751/EI-!51

    1

  • 8/10/2019 Distributed S Lab Manual2-2

    2/36

    INDEX

    S" No" #ro$ram Names #a$e-No" Date

    1WAP to simulate the functionality of Lamport's

    Logical clock in C.3

    2

    WAP to Implement the functionality of Vector

    clock in C.

    3WAP to sho! the "imulation of #istri$ute%

    mutual e&clusion in a(a.12

    )Implementation of *ost name an% IP a%%ress of

    system using net!orking.1+

    ,WAP to implement a %istri$ute% chat ser(er

    using -CP sockets in a(a. 21

    WAP to implement /0a(a I mechanism for

    accessing metho%s of remote systems.23

    4Implementation of C56A 7Common 5$ect

    e8uest 6roker Architecture9 mechanism. 3:

    +Write a a(a program for implementing sli%ing

    !in%o! protocol. 3)

    2

  • 8/10/2019 Distributed S Lab Manual2-2

    3/36

    Program No.1

    //WAP to simulate the functionality of Lamport's Logical clock in C.

    Determining the order of events is difficult in a distributed computer system, since different nodes or

    processes will typically not be perfectly synchronized. Lamport timestamps is a simple algorithm to

    provide a partial orderingof events with minimal overhead, and conceptually provide a starting point for

    the more advanced Vector clock method.

    Distributed algorithms such as resource synchronization often depend on some method of ordering

    events to function. For example, consider a system with two processes and a disk. The processes send

    messages to each other, and also send messages to the disk reuesting access. The disk grants access

    in the order the messages were sent. !ow, imagine process " sends a message to the disk asking for

    access to write, and then sends a message to process # asking it to read. $rocess # receives the

    message, and as a result sends its own message to the disk. !ow, due to some timing delay, the disk

    receives both messages at the same time% how does it determine which message happened&before the

    other' ( logical clock algorithm provides a mechanism to determine facts about the order of such events.

    )eslie )amport invented a simple mechanism by which the happened&before ordering can be captured

    numerically. ( )amport logical clock is a monotonically incrementing software counter maintained in each

    process.

    *t follows some simple rules%

    ". ( process increments its counter before each event in that process+

    #. hen a process sends a message, it includes its counter value with the message+

    -. n receiving a message, the receiver process sets its counter to be greater than the maximum of

    its own value and the received value before it considers the message received.

    /onceptually, this logical clock can be thought of as a clock that only has meaning in relation to

    messages moving between processes. hen a process receives a message, it resynchronizes its logical

    clock with that sender.

    Considerations:For every two events aand boccurring in the same process, and C(x)being the

    timestamp for a certain event x, it is necessary that C(a)never eual C(b).

    Therefore it is necessary that%

    ". The logical clock be set so that there is minimum of one clock 0tick0 1increment of the counter2

    between events aand b+

    3

  • 8/10/2019 Distributed S Lab Manual2-2

    4/36

    #. *n a multiprocessors or multithreaded environment, it might be necessary to attach the process *D

    1$*D2 or any other uniue *D to the timestamp so that it is possible to differentiate between

    events aand bwhich may occur simultaneously in different processes.

    Implications:( )amport clock may be used to create a partial causal ordering of events between

    processes. 3iven a logical clock following these rules, the following relation is true% if then C(a)< C(b), where means 0happened before.0

    This relation only goes one way% if one event comes before another, then that event4s logical clock comes

    before the other4s. 5sing only a simple )amport clock, only a partial causal ordering can be inferred from

    the clock. For example, if you know that C(a) < C(b), then you do not know that .

    6owever, via the contra positive, it4s true that implies . 7o, for example,

    if then acannot have occurred before b.

    (nother way of putting this is that C(a) < C(b)means that amay have happened before or at the same

    time as b, but adid not happen after b.

    !evertheless, )amport timestamps can be used to create a total ordering of events in a distributed

    system by using some arbitrary mechanism to break ties. The caveat is that this ordering cannot be

    depended on to imply a causal relationship.

    [Lamport's logical clock in distributed systems

    *n a distributed system, it is not possible in practice to synchronize time across entities 1typically

    thought of as processes2 within the system+ hence, the entities can use the concept of a logical clock

    based on the events through which they communicate.

    *f two entities do not exchange any messages, then they probably do not need to share a

    common clock+ events occurring on those entities are termed as concurrent events.

    (mong the processes on the same local machine we can order the events based on the local

    clock of the system.

    hen two entities communicate by message passing, then the send event is said to 4happen

    before4 the receive event, and the logical order can be established among the events.

    ( distributed system is said to have partial order if we can have a partial order relationship among

    the events in the system. *f 4totality4, i.e., causal relationship among all events in the system can be

    established, then the system is said to have total order.

    Program for lamport clock

    #include

    4

  • 8/10/2019 Distributed S Lab Manual2-2

    5/36

    #include#include#include#include#include#include

    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()"coutn"*or(i!"i>s[i][5]"*or(%"msg[m][!]"cout

  • 8/10/2019 Distributed S Lab Manual2-2

    6/36

    &&

    &int gd?33,gm"initgraph(@gd,@gm,'A3BCD')"outte$t$E(:!!,%F,'rogram 6or +amport +ogical loc-')"GGdra=ing process and events*or(i!"imsg[m][!]){

    line(msg[m][;]HF!1F!,msg[m][:]H%!!1%!!,msg[m][;]HF!1F!,msg[m][:]H%!!19!)"

    line(msg[m][;]HF!1F!,msg[m][:]H%!!1%!!,msg[m][;]HF!14!,msg[m][:]H%!!19!)"

    &else{

    line(msg[m][;]HF!1F!,msg[m][:]H%!!1%!!,msg[m][;]HF!1F!,msg[m][:]H%!!1%%!)"

    line(msg[m][;]HF!1F!,msg[m][:]H%!!1%!!,msg[m][;]HF!14!,msg[m][:]H%!!1%%!)"

    &&getch()"

    &

    6

  • 8/10/2019 Distributed S Lab Manual2-2

    7/36

    Output

    7

  • 8/10/2019 Distributed S Lab Manual2-2

    8/36

    8

  • 8/10/2019 Distributed S Lab Manual2-2

    9/36

    Program No.2

    //WAP to mplement !ector clock in C.

    Vector clocksis an algorithm for generating a partial ordering of events in a distributed system and

    detecting causality violations. 8ust as in )amport timestamps, interprocess messages contain the state ofthe sending process4s logical clock. ( vector clock of a system of Nprocesses is an array of Nlogical

    clocks, one per process, a local copy of which is kept in each process with the following rules for clock

    updates%

    9xample of a system of vector clocks

    *nitially all clocks are zero.

    9ach time a process experiences an internal event, it increments its own logical clock in the

    vector by one.

    9ach time a process prepares to send a message, it increments its own logical clock in the vector

    by one and then sends its entire vector along with the message being sent.

    9ach time a process receives a message, it increments its own logical clock in the vector by one

    and updates each element in its vector by taking the maximum of the value in its own vector clock

    and the value in the vector in the received message 1for every element2.

    Program:#include#include#include#includelong Hp%(int i,long Hcomp)"long Hp:(int i,long Hcomp)"

    9

    http://en.wikipedia.org/wiki/File:Vector_Clock.svghttp://en.wikipedia.org/wiki/File:Vector_Clock.svg
  • 8/10/2019 Distributed S Lab Manual2-2

    10/36

    long Hp;(int i,long Hcomp)"void main(){

    long start[]{!,!,!&,Hvector"clrscr()"=hile(-bhit()){

    p%(%,@start[!])"

    &print*('n rocess Kectorn')"vectorp%(!,@start[!])"print*('p%[LldLldLld]n',Hvector,H(vector1%),H(vector1:))"vectorp:(!,@start[!])"print*('p:[LldLldLld]n',Hvector,H(vector1%),H(vector1:))"vectorp;(!,@start[!])"print*('p;[LldLldLld]n',Hvector,H(vector1%),H(vector1:))"

    &long Hp%(int i,long Hcomp){

    static long a[]{!,!,!&"

    int ne$t"i*(i%){

    a[!]11"i*(H(comp1%)>a[%])

    a[%]H(comp1%)"i*(H(comp1:)>a[:])

    a[:]H(comp1:)"ne$trandom(:)"i*(ne$t!)

    p:(%,@a[!])"else i*(ne$t%)

    p;(%,@a[!])"return(@a[!])"&else

    return(@a[!])"&

    long Hp:(int i,long Hcomp){

    static long b[]{!,!,!&"int ne$t"i*(i%){

    b[i]11"i*(Hcomp>b[!])

    b[!]H(comp)"i*(H(comp1:)>b[:])

    b[:]H(comp1:)"ne$trandom(:)"i*(ne$t!)

    p%(%,@b[!])"else i*(ne$t%)

    10

  • 8/10/2019 Distributed S Lab Manual2-2

    11/36

    p;(%,@b[!])"return @b[!]"

    &else

    return @b[!]"&long Hp;(int i,long Hcomp){

    static long c[]{!,!,!&"int ne$t"i*(i%){

    c[:]11"i*(Hcomp>c[!])

    c[!]H(comp)"i*(H(comp1%)>c[%])

    c[%]H(comp1%)"ne$trandom(:)"i*(ne$t!)

    p%(%,@c[!])"

    return @c[!]"&else

    return @c[!]"&

    11

  • 8/10/2019 Distributed S Lab Manual2-2

    12/36

    Program No."

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

    import Mtilities.H"import 2EnchroniNation.H"

    class 8essage { public int number, id" public 8essage(int number, int id) { this.number number" this.id id"&&

    class /ode e$tends 8E0bect implements 7unnable {

    private static *inal int 8D/ !, 7OM232 %, 7+D2 :" private int =hich0ne !"

    private int id %" private int num/odes %" private int nap0utside2 !" GG both are in private int napDnside2 !" GG milliseconds private 8essageassing[] rePuesthannel null" private 8essageassing[] replEhannel null" private 8essageassing rePuests3o8e null" private 8essageassing replies3o8e null" private int number !" private int high/umber !" private boolean rePuesting *alse" private int replEount !" private BinarE2emaphore s ne= BinarE2emaphore(%)" private BinarE2emaphore =a-eMp ne= BinarE2emaphore(!)" private boolean[] de*erred null"

    public /ode(2tring name, int id, int num/odes, int nap0utside2, int napDnside2, 8essageassing[] rePuesthannel, 8essageassing replEhannel[], 8essageassing rePuests3o8e, 8essageassing replies3o8e) { super(name 1 ' ' 1 id)"

    this.id id" this.num/odes num/odes" this.nap0utside2 nap0utside2" this.napDnside2 napDnside2" this.rePuesthannel rePuesthannel" this.replEhannel replEhannel" this.rePuests3o8e rePuests3o8e" this.replies3o8e replies3o8e" de*erred ne= boolean[num/odes]"

    12

  • 8/10/2019 Distributed S Lab Manual2-2

    13/36

    *or (int i !" i < num/odes" i11) de*erred[i] *alse" 2Estem.out.println(get/ame() 1 ' is alive, nap0utside2' 1 nap0utside2 1 ', napDnside2' 1 napDnside2)" ne= 3hread(this).start()" &

    public void run() { GG start three di**erent threads in the same obect int me?o =hich0ne11" i* (me?o 8D/) { ne= 3hread(this).start()" main()" & else i* (me?o 7OM232) { ne= 3hread(this).start()" handle7ePuests()" & else i* (me?o 7+D2) { handle7eplies()" & &

    private void choose/umber() { (s)" rePuesting true"

    number high/umber 1 %" K(s)" &

    private void send7ePuest() { replEount !" *or (int !" < num/odes" 11) i* ( id) send(rePuesthannel[], ne= 8essage(number, id))" &

    private void =ait6or7eplE() { (=a-eMp)" &

    private void replE3o?e*erred/odes() { (s)" rePuesting *alse" K(s)" *or (int !" < num/odes" 11) { i* (de*erred[]) { de*erred[] *alse" send(replEhannel[], id)" & & &

    private void outside2() {

    int napping" napping ((int) random(nap0utside2)) 1 %" 2Estem.out.println('age()' 1 age() 1 ', ' 1 get/ame() 1 ' napping outside 2 *or ' 1 napping 1 ' ms')" nap(napping)" &

    private void inside2() { int napping"

    13

  • 8/10/2019 Distributed S Lab Manual2-2

    14/36

    napping ((int) random(napDnside2)) 1 %" 2Estem.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) { outside2()" 2Estem.out.println('age()' 1 age() 1 ', node ' 1 id 1 ' =ants to enter its critical section')" choose/umber()" GG 770300+ send7ePuest()" GG ' =ait6or7eplE()" GG ' inside2()" 2Estem.out.println('age()' 1 age() 1 ', node ' 1 id 1 ' has no= le*t its critical section')" replE3o?e*erred/odes()" GG 02370300+ & &

    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(replEhannel[receivedD?], id)" K(s)" & &

    private void handle7eplies() { =hile (true) { int receivedD? receiveDnt(replies3o8e)" replEount11" i* (replEount num/odes %) K(=a-eMp)" & &&

    class ?istributed8utual$clusion e$tends 8E0bect {

    public static void main(2tring[] args) {

    GG parse command line options, i* anE, to override de*aults Cet0pt go ne= Cet0pt(args, 'MnA7A')" 2tring usage 'MsageA n num/odes 7 run3ime' 1 ' nap0utside2[i] napDnside2[i] i!,%,...'" go.optrr true" int ch %" int num/odes F" int run3ime I!" GG seconds =hile ((ch go.getopt()) go.opt06) {

    14

  • 8/10/2019 Distributed S Lab Manual2-2

    15/36

    i* ((char)ch M) { 2Estem.out.println(usage)" 2Estem.e$it(!)" & else i* ((char)ch n) num/odes go.processrg(go.optrgCet(), num/odes)" else i* ((char)ch 7) run3ime go.processrg(go.optrgCet(), run3ime)" else { 2Estem.err.println(usage)" 2Estem.e$it(%)" & & 2Estem.out.println('?istributed8utual$clusionA num/odes' 1 num/odes 1 ', run3ime' 1 run3ime)"

    GG process nonoption command line arguments int[] nap0utside2 ne= int[num/odes]" int[] napDnside2 ne= int[num/odes]" int arg/um go.optDnde$Cet()" *or (int i !" i < num/odes" i11) { nap0utside2[i] go.trErg(arg/um11, 5)" napDnside2[i] go.trErg(arg/um11, :)" &

    GG create communication channels 8essageassing[] rePuesthannel null, replEhannel null, rePuesthannel2 null, rePuesthannel7 null, replEhannel2 null, replEhannel7 null" rePuesthannel ne= 8essageassing[num/odes]" replEhannel ne= 8essageassing[num/odes]" rePuesthannel2 ne= 8essageassing[num/odes]" replEhannel2 ne= 8essageassing[num/odes]" rePuesthannel7 ne= 8essageassing[num/odes]" replEhannel7 ne= 8essageassing[num/odes]" *or (int i !" i < num/odes" i11) { rePuesthannel[i] ne= sEnc8essageassing()" replEhannel[i] ne= sEnc8essageassing()" rePuesthannel2[i] ne= 8essageassing2end0nlE(rePuesthannel[i])" replEhannel2[i] ne= 8essageassing2end0nlE(replEhannel[i])" rePuesthannel7[i] ne=8essageassing7eceive0nlE(rePuesthannel[i])" replEhannel7[i] ne= 8essageassing7eceive0nlE(replEhannel[i])" &

    GG create the /odes (theE start their o=n threads) *or (int i !" i < num/odes" i11) ne= /ode('/ode', i, num/odes, nap0utside2[i]H%!!!, napDnside2[i]H%!!!, rePuesthannel2, replEhannel2, rePuesthannel7[i], replEhannel7[i])" 2Estem.out.println('ll /odes created')"

    GG let the /odes run *or a =hile nap(run3imeH%!!!)" 2Estem.out.println('age()' 1 age() 1 ', time to stop the threads and e$it')" 2Estem.e$it(!)" &&

    15

  • 8/10/2019 Distributed S Lab Manual2-2

    16/36

    Output+

    ?Ara-ashRava7/?dvanced>avac dimu.ava

    ?A ra-ashRava7/?dvanced >ava ?istributed8utual$clusion 7:!?istributed8utual$clusionA num/odesF, run3ime:!/ode ! is alive, nap0utside25!!!, napDnside2:!!!/ode % is alive, nap0utside25!!!, napDnside2:!!!/ode : is alive, nap0utside25!!!, napDnside2:!!!/ode ; is alive, nap0utside25!!!, napDnside2:!!!/ode 4 is alive, nap0utside25!!!, napDnside2:!!!age()%S!, /ode % napping outside 2 *or :S%9 msage()%S!, /ode : napping outside 2 *or :S9 msll /odes createdage()%S!, /ode ; napping outside 2 *or :;FF msage()::!, /ode ! napping outside 2 *or :;9; msage()::!, /ode 4 napping outside 2 *or 5 msage()::!, node 4 =ants to enter its critical sectionage();;!, /ode 4 napping inside 2 *or 9%% msage()44!, node : =ants to enter its critical sectionage()%:I!, node 4 has no= le*t its critical sectionage()%:I!, /ode 4 napping outside 2 *or 4!4: msage()%:I!, /ode : napping inside 2 *or %5; msage()%45!, node : has no= le*t its critical sectionage()%45!, /ode : napping outside 2 *or S;;F msage():F;!, node ; =ants to enter its critical sectionage():F;!, /ode ; napping inside 2 *or S4% msage():F5!, node ! =ants to enter its critical sectionage():5I!, node % =ants to enter its critical sectionage();;!!, node ; has no= le*t its critical sectionage();;!!, /ode ; napping outside 2 *or I549 ms

    age();;!!, /ode ! napping inside 2 *or %S%! msage()F!!!, node ! has no= le*t its critical sectionage()F!!!, /ode ! napping outside 2 *or F:F; msage()F!!!, /ode % napping inside 2 *or %I94 msage()F;;!, node 4 =ants to enter its critical sectionage()IS!!, node % has no= le*t its critical sectionage()IS!!, /ode % napping outside 2 *or ;!I; msage()IS!!, /ode 4 napping inside 2 *or ;9S msage()S%4!, node 4 has no= le*t its critical section

    16

  • 8/10/2019 Distributed S Lab Manual2-2

    17/36

    age()S%4!, /ode 4 napping outside 2 *or ;I5S msage()5S9!, node : =ants to enter its critical sectionage()5S9!, /ode : napping inside 2 *or %!: msage()59!!, node : has no= le*t its critical sectionage()59!!, /ode : napping outside 2 *or %%S4 msage()9S5!, node % =ants to enter its critical sectionage()9S5!, /ode % napping inside 2 *or %I%S msage()%!%%!, node : =ants to enter its critical sectionage()%!%I!, node ; =ants to enter its critical sectionage()%!:S!, node ! =ants to enter its critical sectionage()%!5:!, node 4 =ants to enter its critical sectionage()%%4;!, node % has no= le*t its critical sectionage()%%4;!, /ode % napping outside 2 *or F;:I msage()%%4;!, /ode : napping inside 2 *or I:5 msage()%:!9!, node : has no= le*t its critical sectionage()%:!9!, /ode : napping outside 2 *or 49S! msage()%:!9!, /ode ; napping inside 2 *or F4F msage()%:I;!, node ; has no= le*t its critical sectionage()%:I;!, /ode ; napping outside 2 *or S959 msage()%:I;!, /ode ! napping inside 2 *or 9!4 msage()%;F%!, node ! has no= le*t its critical sectionage()%;F%!, /ode ! napping outside 2 *or 4%I: ms

    age()%;F%!, /ode 4 napping inside 2 *or %44! msage()%F!!!, node 4 has no= le*t its critical sectionage()%F!!!, /ode 4 napping outside 2 *or :FS5 msage()%ISF!, node % =ants to enter its critical sectionage()%ISF!, /ode % napping inside 2 *or %:; msage()%I5I!, node % has no= le*t its critical sectionage()%I5I!, /ode % napping outside 2 *or ;S!9 msage()%S!;!, node : =ants to enter its critical sectionage()%S!;!, /ode : napping inside 2 *or 9S msage()%S%4!, node : has no= le*t its critical sectionage()%S%4!, /ode : napping outside 2 *or S9!% msage()%SF5!, node 4 =ants to enter its critical sectionage()%SF5!, /ode 4 napping inside 2 *or %I9F msage()%SI9!, node ! =ants to enter its critical sectionage()%9:5!, node 4 has no= le*t its critical sectionage()%9:5!, /ode 4 napping outside 2 *or ;SF% msage()%9:5!, /ode ! napping inside 2 *or 5I9 msage():!%I!, node ! has no= le*t its critical sectionage():!%I!, /ode ! napping outside 2 *or I459 msage():!%I!, time to stop the threads and e$it ... end o* e$ample run(s) HG

    17

  • 8/10/2019 Distributed S Lab Manual2-2

    18/36

    Program No.,

    Implementation of host name and IP address of system

    using networking.

    TheoryA

    An IP (Internet Protocol) address is a unique identifer or a node or ost connection

    on an IP net!or"# An IP address is a 32 $it $inar% nu&$er usuall% re'resented as 4

    deci&al alues eac re'resentin* 8 $its in te ran*e 0 to 255 ("no!n as octets)

    se'arated $% deci&al 'oints# +is is "no!n as ,dotted deci&al, notation#

    ;&ample< 1):.14.22:.2::

    It is sometimes useful to (ie! the (alues in their $inary form.

    140 .179 .220 .200

    10001100.10110011.11011100.11001000

    ;(ery IP a%%ress consists of t!o parts= one i%entifying the net!ork an% one i%entifying the no%e.

    -he Class of the a%%ress an% the su$net mask %etermine !hich part $elongs to the net!ork

    a%%ress an% !hich part $elongs to the no%e a%%ress.

    %ddress Classes

    18

  • 8/10/2019 Distributed S Lab Manual2-2

    19/36

    +ere are 5 di-erent address classes# .ou can deter&ine !ic class an% IP address

    is in $% e/a&inin* te frst 4 $its o te IP address#

    lass A addresses $e*in !it 0/// or 1 to 126 deci&al#

    lass addresses $e*in !it 10// or 128 to 191 deci&al#

    lass addresses $e*in !it 110/ or 192 to 223 deci&al#

    lass addresses $e*in !it 1110 or 224 to 239 deci&al#

    lass addresses $e*in !it 1111 or 240 to 254 deci&al#

    : #;

    /lass (% < !etwork *D 6ost *D

    "; "=

    /lass >% " < !etwork *D 6ost *D

    #" ?

    /lass /% " " < !etwork *D 6ost *D

    #?

    /lass D 1multicast2% " " " < @ulticast address

    #:

    /lass 9 1reserved2% " " " " unused ?@ou can test this< you shoul% al!ays $e a$le to ping 124.:.:.1= !hich

    points to yourself Class # a%%resses are reser(e% for multicasting> Class ; a%%resses are

    reser(e% for future use. -hey shoul% not $e use% for host a%%resses.

    Bo! !e can see ho! the Class %etermines= $y %efault= !hich part of the IP a%%ress $elongs to

    the net!ork 7B= in$lue9 an% !hich part $elongs to the no%e 7n= in re%9.

    lass A #nnnnnnnn#nnnnnnnn#nnnnnnnn lass ##nnnnnnnn#nnnnnnnn

    lass ###nnnnnnnn

    In the e&ample= 1):.14.22:.2:: is a Class 6 a%%ress so $y %efault the Bet!ork part of thea%%ress 7also kno!n as theNetwork Address9 is %efine% $y the first t!o octets 71):.14.&.&9 an%

    the no%e part is %efine% $y the last 2 octets 7&.&.22:.2::9.

    19

  • 8/10/2019 Distributed S Lab Manual2-2

    20/36

    In or%er to specify the net!ork a%%ress for a gi(en IP a%%ress= the no%e section is set to all :s.

    In our e&ample= 1):.14.:.: specifies the net!ork a%%ress for 1):.14.22:.2::. When the no%e

    section is set to all 1s= it specifies a $roa%cast that is sent to all hosts on the net!ork.1):.14.2,,.2,, specifies the e&ample $roa%cast a%%ress. Bote that this is true regar%less of the

    length of the no%e section.

    $rivate 7ubnets

    +ere are tree IP net!or" addresses resered or 'riate net!or"s# +e addressesare 10#0#0#0 u$net as" 255#0#0#0 172#16#0#0 u$net as" 255#240#0#0

    and 192#168#0#0 u$net as" 255#255#0#0# +ese addresses are also

    notated 10#0#0#08 172#16#0#012 and 192#168#0#016 tis notation !ill $e

    e/'lained later in tis tutorial# +e% can $e used $% an%one settin* u' internal IP

    net!or"s suc as a la$ or o&e :A $eind a A+ or 'ro/% serer or a router# It is

    al!a%s sae to use tese $ecause routers on te Internet $% deault !ill neer

    or!ard 'ac"ets co&in* ro& tese addresses# +ese addresses are defned in ;aa#io#?

    'u$lic class fnd1

    @

    'u$lic static oid &ain(trin* ar*sB)tro!s /ce'tion

    @

    %steout#'rintln(,nter te ost na&e=,)

    trin* sCne! ataIn'uttrea&(%stein)#read:ine()

    InetAddress iaCInetAddress#*et%na&e(s)

    %steout#'rintln(,IP address#,Dia)E

    E

    i&'ort >aa#net#?

    i&'ort >aa#io#?

    'u$lic class 'arse

    @

    20

  • 8/10/2019 Distributed S Lab Manual2-2

    21/36

    'u$lic static oid &ain(trin* ar*sB)tro!s /ce'tion

    @

    F;: uCne! F;:(,tt'=>aa#sun#co&=80docsinde/Gdo!n,)

    %steout#'rintln(,'rotocol=,Du#*etProtocol())

    %steout#'rintln(,ost =,Du#*etHost())

    %steout#'rintln(,flena&e=,Du#*et

  • 8/10/2019 Distributed S Lab Manual2-2

    22/36

    Bu**ered7eader sinne= Bu**ered7eader(ne=Dnput2tream7eader(2Estem.in))"

    2Estem.out.println('D am readE tEpe no=')"=hile((lsin.read+ine())null){

    out.println(l)"&out.close()"sin.close()"

    c.close()"s%.close()"

    &&

    2.Client.)a*a

    import ava.net.H"import ava.io.H"public class client{

    public static void main(2tring args[])thro=s D0$ception{2oc-et snull"Bu**ered7eader bnull"

    trE{sne= 2oc-et(Dnetddress.get+ocalUost(),95)"bne= Bu**ered7eader(ne=

    Dnput2tream7eader(s.getDnput2tream()))"&catch($ception e){

    2Estem.out.println('D do not host')"e.print2tac-3race()"

    &2tring inp"=hile((inpb.read+ine())null){

    2Estem.out.println(inp)"&

    b.close()"s.close()"

    &&

    Running the application'en t!o c&d 'ro&'t and ollo! tese1#>aa erer

    22

  • 8/10/2019 Distributed S Lab Manual2-2

    23/36

    2#>aa client

    Output=JPra"asJ;JKaaJetLor"in*JatererM>aa sereronnection ro&oc"etaddrC127#0#0#1'ortC1120local'ortC98BI a& read% t%'e no!Hello o! r uN dudeO=JPra"asJ;JKaaJetLor"in*JatererM>aa clientHello o! r uN dudeO

    Program No.

    mplement 0a*a 34 mechanism for accessing methos of remote

    systems.

    ;I a''lications oten co&'rise t!o se'arate 'ro*ra&s a serer

    and a client# A t%'ical serer 'ro*ra& creates so&e re&ote o$>ects

    &a"es reerences to tese o$>ects accessi$le and !aits or clientsto ino"e &etods on tese o$>ects# A t%'ical client 'ro*ra& o$tains

    a re&ote reerence to one or &ore re&ote o$>ects on a serer and

    ten ino"es &etods on te ;I 'roides te &ecanis& $%

    !ic te serer and te client co&&unicate and 'ass inor&ation

    $ac" and ort# uc an a''lication is so&eti&es reerred to as

    a distributed object application#

    #istri$ute% o$ect applications nee% to %o the follo!ingects#

  • 8/10/2019 Distributed S Lab Manual2-2

    24/36

    "ommunicate with remote o!jects.etails oco&&unication $et!een re&ote o$>ects are andled $%;I# +o te 'ro*ra&&er re&ote co&&unication loo"ssi&ilar to re*ular Kaa &etod inocations#

    oad class de#nitions for o!jects that are passed

    around.ecause ;I ena$les o$>ects to $e 'assed $ac"and ort it 'roides &ecanis&s or loadin* an o$>ectsclass defnitions as !ell as or trans&ittin* an o$>ectsdata#

    +e ollo!in* illustration de'icts an ;I distri$uted a''lication tat

    uses te ;I re*istr% to o$tain a reerence to a re&ote o$>ect# +e

    serer calls te re*istr% to associate (or $ind) a na&e !it a re&ote

    o$>ect# +e client loo"s u' te re&ote o$>ect $% its na&e in te

    serers re*istr% and ten ino"es a &etod on it# +e illustration

    also so!s tat te ;I s%ste& uses an e/istin* !e$ serer to load

    class defnitions ro& serer to client and ro& client to serer or

    o$>ects !en needed#

    Advantages of Dynamic Code Loading

    ne o te central and unique eatures o ;I is its a$ilit% to

    do!nload te defnition o an o$>ects class i te class is not defned

    in te receiers Kaa irtual &acine# All o te t%'es and $eaior

    o an o$>ect 'reiousl% aaila$le onl% in a sin*le Kaa irtual

    &acine can $e trans&itted to anoter 'ossi$l% re&ote Kaa

    irtual &acine# ;I 'asses o$>ects $% teir actual classes so te

    $eaior o te o$>ects is not can*ed !en te% are sent to

    anoter Kaa irtual &acine# +is ca'a$ilit% ena$les ne! t%'es and

    $eaiors to $e introduced into a re&ote Kaa irtual &acine tus

    24

  • 8/10/2019 Distributed S Lab Manual2-2

    25/36

    d%na&icall% e/tendin* te $eaior o an a''lication# +e co&'ute

    en*ine e/a&'le in tis trail uses tis ca'a$ilit% to introduce ne!

    $eaior to a distri$uted 'ro*ra

    Remote Interfaces !b"ects and #et$ods

    :i"e an% oter Kaa a''lication a distri$uted a''lication $uilt $%

    usin* Kaa ;I is &ade u' o interaces and classes# +e interaces

    declare &etods# +e classes i&'le&ent te &etods declared in

    te interaces and 'era's declare additional &etods as !ell# In a

    distri$uted a''lication so&e i&'le&entations &i*t reside in so&e

    Kaa irtual &acines $ut not oters# $>ects !it &etods tat can

    $e ino"ed across Kaa irtual &acines are called remote objects#

    An o$ect $ecomes remote $y implementing a remote interface= !hich has the

    follo!ing characteristicsaa

    public inter*ace alculatore$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"

    &

    28

  • 8/10/2019 Distributed S Lab Manual2-2

    29/36

    3#alculatorerer#>aa

    import ava.rmi./aming"public class alculator2erver { public alculator2erver() { trE { alculator c ne= alculatorDmpl()"

    /aming.rebind('rmiAGGlocalhostA%!99Galculator2ervice', c)" & catch ($ception e) { 2Estem.out.println('3roubleA ' 1 e)" & &public static void main(2tring args[]) { ne= alculator2erver()" &&

    4#alculatorlient#>aa

    import ava.rmi./aming"import ava.rmi.7emote$ception"import ava.net.8al*ormedM7+$ception"import ava.rmi./otBound$ception"public class alculatorlient {public static void main(2tring[] args) {

    trE {alculator c (alculator)

    /aming.loo-up('rmiAGGlocalhostGalculator2ervice')"

    2Estem.out.println( c.sub(4, ;) )"2Estem.out.println( c.add(4, F) )"

    2Estem.out.println( c.mul(;, I) )"2Estem.out.println( c.div(9, ;) )"

    &catch (8al*ormedM7+$ception murle) {

    2Estem.out.println()"2Estem.out.println('8al*ormedM7+$ception')"2Estem.out.println(murle)"

    &catch (7emote$ception re) {

    2Estem.out.println()"2Estem.out.println('7emote$ception')"2Estem.out.println(re)"

    &catch (/otBound$ception nbe) {

    2Estem.out.println()"2Estem.out.println(

    '/otBound$ception')"2Estem.out.println(nbe)"

    &catch ( ava.lang.rithmetic$ception ae) {

    29

  • 8/10/2019 Distributed S Lab Manual2-2

    30/36

    2Estem.out.println()"2Estem.out.println(

    'ava.lang.rithmetic$ception')"2Estem.out.println(ae)"

    &&

    &

    Running &he 'pplication:

    =JPra"asJ;JKaaJetLor"in*J;IMr&ic alculatorI&'lo! o'en tree c&d 'ro&'ts and ollo! tese at eac#1# =JPra"asJ;JKaaJetLor"in*J;IM;&ire*istr%2# =JPra"asJ;JKaaJetLor"in*J;IM>aa alculatorerer3# =JPra"asJ;JKaaJetLor"in*J;IM>aa alculatorlient

    Output:

    If all goes !ell you !ill see the follo!ing outputects 'ro*ra&&in*#

    ;A !as desi*ned to $e 'lator& and lan*ua*e inde'endent#

    an $e !ritten in an% lan*ua*e tat as Interace efnition :an*ua*e (I:)

    &a''in*s#

    +ere are a nu&$er o ste's inoled in deelo'in* ;A a''lications# +ese are=

    1# efne an interace in I:2# a' te I: interace to Kaa (done auto&aticall%)

    3# I&'le&ent te interace

    4# eelo' te serer

    5# eelo' a client

    ;un te na&in* serice te serer and te client

    1#

  • 8/10/2019 Distributed S Lab Manual2-2

    32/36

    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);}

    }

    3#aa

    import java.io.*;

    import org.omg.CosNaming.*;

    import org.omg.CosNaming.NamingContextPackage.*;

    import 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);

    }

    }

    }

    4#aa

    32

  • 8/10/2019 Distributed S Lab Manual2-2

    33/36

    import java.io.*;

    import java.util.*;

    import org.omg.CosNaming.*;

    import 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 t$e application

    1.=JPra"asJ;JKaaJ;AMtnameserv2.=JPra"asJ;JKaaJ;AMjava FileServer3.=JPra"asJ;JKaaJ;AMidlj -fclient FileInterface.idl4.=JPra"asJ;JKaaJ;AMjava FileClient hello.txt

    33

  • 8/10/2019 Distributed S Lab Manual2-2

    34/36

    Output+

    34

  • 8/10/2019 Distributed S Lab Manual2-2

    35/36

    Program No.:

    Write a )a*a program for implementing sliing ;ino; protocol.A sliding window protocol is a eature o 'ac"et$ased data trans&ission'rotocols# lidin* !indo! 'rotocols are used !ere relia$le inorder delier% o'ac"ets is required suc as in te ata :in" :a%er (I &odel) as !ell as in te

    +rans&ission ontrol Protocol (+P)#1.slic.java

    GG2+D?D/C TD/?0T 70300+ V +D/3

    import ava.io.H"

    import ava.net.H"

    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(2Estem.in))"

    2tring dout,din"

    2Estem.out.println(WtX1*romserver.read+ine())"

    2Estem.out.println(Wenter Puit to e$itX)"

    2Estem.out.println(Wenter data *or server AX)"

    Thile(true)

    {

    ?out d.read+ine()"

    D*(dout.ePuals(WPuitX))

    Brea-"

    toserver.=riteBEtes(dout1YnY)"

    din *romserver.read+ine()"

    2Estem.out.println(W2erver AX1din)"

    35

  • 8/10/2019 Distributed S Lab Manual2-2

    36/36

    2Estem.out.println(Wnnter *or server AX)"

    &

    &

    &

    (" slis.java

    GG2+D?D/C TD/?0T 70300+ V 27K7

    import ava.io.H"

    import ava.net.H"

    public class slis{

    public static void main(2tring args[])thro=s $ception{

    2erver2oc-et ss ne= 2erver2oc-et(5555)"

    2Estem.out.println(Wt =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(2Estem.in))"

    2tring dout,din"

    toclient.=riteBEtes(W2erver readEZ.X1YnY)"

    =hile(true){

    din *romclient.read+ine()"

    2Estem.out.println(Wn client dataAX1din)"2Estem.out.println(Wenter *or client AX)"

    dout d.read+ine()"

    i*(dout.ePuals(WPuitX))

    brea-"

    toclient.=riteBEtes(dout1YnY)"

    &

    &

    &