三种ARQ算法C语言.docx

上传人:b****6 文档编号:6420257 上传时间:2023-01-06 格式:DOCX 页数:9 大小:17.62KB
下载 相关 举报
三种ARQ算法C语言.docx_第1页
第1页 / 共9页
三种ARQ算法C语言.docx_第2页
第2页 / 共9页
三种ARQ算法C语言.docx_第3页
第3页 / 共9页
三种ARQ算法C语言.docx_第4页
第4页 / 共9页
三种ARQ算法C语言.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

三种ARQ算法C语言.docx

《三种ARQ算法C语言.docx》由会员分享,可在线阅读,更多相关《三种ARQ算法C语言.docx(9页珍藏版)》请在冰豆网上搜索。

三种ARQ算法C语言.docx

三种ARQ算法C语言

三种ARQ协议的C语言算法

一、停止等待ARQ(stop-and-waitARQ)

文件名p2.c

/*Protocol2(stop-and-wait)alsoprovidesforaone-directionalflowofdatafrom

sendertoreceiver.Thecommunicationchannelisonceagainassumedtobeerror

free,asinprotocol1.However,thistime,thereceiverhasonlyafinitebuffer

capacityandafiniteprocesingspeed,sotheprotocolmustexplicitlyprevent

thesenderfromfloodingthereceiverwithdatafasterthanitcanbehandled.*/

typedefenum{frame_arrival}event_type;

#include"protocol.h"

voidsender2(void)

{

frames;/*bufferforanoutboundframe*/

packetbuffer;/*bufferforanoutboundpacket*/

event_typeevent;/*frame_arrivalistheonlypossibility*/

while(true){

from_network_layer(&buffer);/*gogetsomethingtosend*/

s.info=buffer;/*copyitintosfortransmission*/

to_physical_layer(&s);/*byebyelittleframe*/

wait_for_event(&event);/*donotproceeduntilgiventhegoahead*/

}

}

voidreceiver2(void)

{

framer,s;/*buffersforframes*/

event_typeevent;/*frame_arrivalistheonlypossibility*/

while(true){

wait_for_event(&event);/*onlypossibilityisframe_arrival*/

from_physical_layer(&r);/*gogettheinboundframe*/

to_network_layer(&r.info);/*passthedatatothenetworklayer*/

to_physical_layer(&s);/*sendadummyframetoawakensender*/

}

}

二、

回退N帧ARQ(go-back-nARQ)

文件名p5.c

/*Protocol5(pipelining)allowsmultipleoutstandingframes.Thesendermaytransmitup

toMAX_SEQframeswithoutwaitingforanack.Inaddition,unlikethepreviousprotocols,

thenetworklayerisnotassumedtohaveanewpacketallthetime.Instead,the

networklayercausesanetwork_layer_readyeventwhenthereisapackettosend.*/

#defineMAX_SEQ7/*shouldbe2^n-1*/

typedefenum{frame_arrival,cksum_err,timeout,network_layer_ready}event_type;

#include"protocol.h"

staticbooleanbetween(seq_nra,seq_nrb,seq_nrc)

{

/*Returntrueif(a<=b

if(((a<=b)&&(b

return(true);

else

return(false);

}

staticvoidsend_data(seq_nrframe_nr,seq_nrframe_expected,packetbuffer[])

{

/*Constructandsendadataframe.*/

frames;/*scratchvariable*/

s.info=buffer[frame_nr];/*insertpacketintoframe*/

s.seq=frame_nr;/*insertsequencenumberintoframe*/

s.ack=(frame_expected+MAX_SEQ)%(MAX_SEQ+1);/*piggybackack*/

to_physical_layer(&s);/*transmittheframe*/

start_timer(frame_nr);/*startthetimerrunning*/

}

voidprotocol5(void)

{

seq_nrnext_frame_to_send;/*MAX_SEQ>1;usedforoutboundstream*/

seq_nrack_expected;/*oldestframeasyetunacknowledged*/

seq_nrframe_expected;/*nextframeexpectedoninboundstream*/

framer;/*scratchvariable*/

packetbuffer[MAX_SEQ+1];/*buffersfortheoutboundstream*/

seq_nrnbuffered;/*#outputbufferscurrentlyinuse*/

seq_nri;/*usedtoindexintothebufferarray*/

event_typeevent;

enable_network_layer();/*allownetwork_layer_readyevents*/

ack_expected=0;/*nextackexpectedinbound*/

next_frame_to_send=0;/*nextframegoingout*/

frame_expected=0;/*numberofframeexpectedinbound*/

nbuffered=0;/*initiallynopacketsarebuffered*/

while(true){

wait_for_event(&event);/*fourpossibilities:

seeevent_typeabove*/

switch(event){

casenetwork_layer_ready:

/*thenetworklayerhasapackettosend*/

/*Accept,save,andtransmitanewframe.*/

from_network_layer(&buffer[next_frame_to_send]);/*fetchnewpacket*/

nbuffered=nbuffered+1;/*expandthesender'swindow*/

send_data(next_frame_to_send,frame_expected,buffer);/*transmittheframe*/

inc(next_frame_to_send);/*advancesender'supperwindowedge*/

break;

caseframe_arrival:

/*adataorcontrolframehasarrived*/

from_physical_layer(&r);/*getincomingframefromphysicallayer*/

if(r.seq==frame_expected){

/*Framesareacceptedonlyinorder.*/

to_network_layer(&r.info);/*passpackettonetworklayer*/

inc(frame_expected);/*advanceloweredgeofreceiver'swindow*/

}

/*Acknimpliesn-1,n-2,etc.Checkforthis.*/

while(between(ack_expected,r.ack,next_frame_to_send)){

/*Handlepiggybackedack.*/

nbuffered=nbuffered-1;/*oneframefewerbuffered*/

stop_timer(ack_expected);/*framearrivedintact;stoptimer*/

inc(ack_expected);/*contractsender'swindow*/

}

break;

casecksum_err:

;/*justignorebadframes*/

break;

casetimeout:

/*trouble;retransmitalloutstandingframes*/

next_frame_to_send=ack_expected;/*startretransmittinghere*/

for(i=1;i<=nbuffered;i++){

send_data(next_frame_to_send,frame_expected,buffer);/*resend1frame*/

inc(next_frame_to_send);/*preparetosendthenextone*/

}

}

if(nbuffered

enable_network_layer();

else

disable_network_layer();

}

}

三、选择拒绝ARQ(selective-rejectARQ)

文件名p6.c

/*Protocol6(nonsequentialreceive)acceptsframesoutoforder,butpassespacketstothe

networklayerinorder.Associatedwitheachoutstandingframeisatimer.Whenthetimer

goesoff,onlythatframeisretransmitted,notalltheoutstandingframes,asinprotocol5.*/

#defineMAX_SEQ7/*shouldbe2^n-1*/

#defineNR_BUFS((MAX_SEQ+1)/2)

typedefenum{frame_arrival,cksum_err,timeout,network_layer_ready,ack_timeout}event_type;

#include"protocol.h"

booleanno_nak=true;/*nonakhasbeensentyet*/

seq_nroldest_frame=MAX_SEQ+1;/*initvalueisforthesimulator*/

staticbooleanbetween(seq_nra,seq_nrb,seq_nrc)

{

/*Sameasbetweeninprotocol5,butshorterandmoreobscure.*/

return((a<=b)&&(b

}

staticvoidsend_frame(frame_kindfk,seq_nrframe_nr,seq_nrframe_expected,packetbuffer[])

{

/*Constructandsendadata,ack,ornakframe.*/

frames;/*scratchvariable*/

s.kind=fk;/*kind==data,ack,ornak*/

if(fk==data)s.info=buffer[frame_nr%NR_BUFS];

s.seq=frame_nr;/*onlymeaningfulfordataframes*/

s.ack=(frame_expected+MAX_SEQ)%(MAX_SEQ+1);

if(fk==nak)no_nak=false;/*onenakperframe,please*/

to_physical_layer(&s);/*transmittheframe*/

if(fk==data)start_timer(frame_nr%NR_BUFS);

stop_ack_timer();/*noneedforseparateackframe*/

}

voidprotocol6(void)

{

seq_nrack_expected;/*loweredgeofsender'swindow*/

seq_nrnext_frame_to_send;/*upperedgeofsender'swindow+1*/

seq_nrframe_expected;/*loweredgeofreceiver'swindow*/

seq_nrtoo_far;/*upperedgeofreceiver'swindow+1*/

inti;/*indexintobufferpool*/

framer;/*scratchvariable*/

packetout_buf[NR_BUFS];/*buffersfortheoutboundstream*/

packetin_buf[NR_BUFS];/*buffersfortheinboundstream*/

booleanarrived[NR_BUFS];/*inboundbitmap*/

seq_nrnbuffered;/*howmanyoutputbufferscurrentlyused*/

event_typeevent;

enable_network_layer();/*initialize*/

ack_expected=0;/*nextackexpectedontheinboundstream*/

next_frame_to_send=0;/*numberofnextoutgoingframe*/

frame_expected=0;/*framenumberexpected*/

too_far=NR_BUFS;/*receiver'supperwindow+1*/

nbuffered=0;/*initiallynopacketsarebuffered*/

for(i=0;i

while(true){

wait_for_event(&event);/*fivepossibilities:

seeevent_typeabove*/

switch(event){

casenetwork_layer_ready:

/*accept,save,andtransmitanewframe*/

nbuffered=nbuffered+1;/*expandthewindow*/

from_network_layer(&out_buf[next_frame_to_send%NR_BUFS]);/*fetchnewpacket*/

send_frame(data,next_frame_to_send,frame_expected,out_buf);/*transmittheframe*/

inc(next_frame_to_send);/*advanceupperwindowedge*/

break;

caseframe_arrival:

/*adataorcontrolframehasarrived*/

from_physical_layer(&r);/*fetchincomingframefromphysicallayer*/

if(r.kind==data){

/*Anundamagedframehasarrived.*/

if((r.seq!

=frame_expected)&&no_nak)

send_frame(nak,0,frame_expected,out_buf);elsestart_ack_timer();

if(between(frame_expected,r.seq,too_far)&&(arrived[r.seq%NR_BUFS]==false)){

/*Framesmaybeacceptedinanyorder.*/

arrived[r.seq%NR_BUFS]=true;/*markbufferasfull*/

in_buf[r.seq%NR_BUFS]=r.info;/*insertdataintobuffer*/

while(arrived[frame_expected%NR_BUFS]){

/*Passframesandadvancewindow.*/

to_network_layer(&in_buf[frame_expected%NR_BUFS]);

no_nak=true;

arrived[frame_expected%NR_BUFS]=false;

inc(frame_expected);/*advanceloweredgeofreceiver'swindow*/

inc(too_far);/*advanceupperedgeofreceiver'swindow*/

start_ack_timer();/*toseeif(aseparateackisneeded*/

}

}

}

if((r.kind==nak)&&between(ack_expected,(r.ack+1)%(MAX_SEQ+1),next_frame_to_send))

send_frame(data,(r.ack+1)%(MAX_SEQ+1),frame_expected,out_buf);

while(between(ack_expected,r.ack,next_frame_to_send)){

nbuffered=nbuffered-1;/*handlepiggybackedack*/

stop_timer(ack_expected%NR_BUFS);/*framearrivedintact*/

inc(ack_expected);/*advanceloweredgeofsender'swindow*/

}

break;

casecksum_err:

if(no_nak)send_frame(nak,0,frame_expected,out_buf);break;/*damagedframe*/

casetimeout:

send_frame(data,oldest_frame,frame_expected,out_buf);break;/*wetimedout*/

caseack_timeout:

send_frame(ack,0,frame_expected,out_buf);/*acktimerexpired;sendack*/

}

if(nbuffered

}

}

四、协议中用到的H文件

文件名protocol.h

#defineMAX_PKT4/*determinespacketsizeinbytes*/

typedefenum{false,true}boolean;/*booleantype*/

t

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 动态背景

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1