云南大学软件学院计算机网络原理实验六实验报告.docx

上传人:夺命阿水 文档编号:1672116 上传时间:2024-11-24 格式:DOCX 页数:50 大小:78.12KB
返回 下载 相关 举报
云南大学软件学院计算机网络原理实验六实验报告.docx_第1页
第1页 / 共50页
云南大学软件学院计算机网络原理实验六实验报告.docx_第2页
第2页 / 共50页
云南大学软件学院计算机网络原理实验六实验报告.docx_第3页
第3页 / 共50页
云南大学软件学院计算机网络原理实验六实验报告.docx_第4页
第4页 / 共50页
云南大学软件学院计算机网络原理实验六实验报告.docx_第5页
第5页 / 共50页
点击查看更多>>
资源描述

《云南大学软件学院计算机网络原理实验六实验报告.docx》由会员分享,可在线阅读,更多相关《云南大学软件学院计算机网络原理实验六实验报告.docx(50页珍藏版)》请在课桌文档上搜索。

1、试验六、传输层牢靠传输协议GBN编程试验报告序号:一姓名:_学号:成果一指导老师:一、试验目的;1、通过编写实现一个简洁牢旅的数据传输协议GBN的发送和接收代码,模拟牢靠数据传输2、理解TCP协议牢轮传输的差错检测、重传、累计确认、定时器的牢拳传输策略。二、试验指导:参考教材。动画演示:三、试验要求:编程实现一个GBN传输协议的发送方和接收方两程序,采纳编程语言不限,要求能将发送一一接收流程以与处理方法表现出来.1.试验流程图如下:OX选择由传2.试验截图与代码如下:截图:GUtrfQiuD*cktopitHfi6GBN001DbugGBN001xe-StopandVaitNetworkSit

2、nj1.atorVersion1.1Snterthenumberofmessagestosinu1.ate:56Enterpacket1.ossprobabi1.ityenterO.Oforno1.oss:3Enterpacketcorruptionprobabi1ityO.Oornocorruption:OSnteraveragetimebetweenEfiSSaEeSfromsenders1.ayer5O.0:1interTRACE:3A:IfeWJ119ssagarrives,sendWindWisfu1.1.,buffernewmessage!entity:0aaaaaaaaaaaaa

3、aaaaaaafu1.1.buffernewmessage!entity:0bbbbbbbbbbbbbbbbbbbbfu1.1.,buffernewmessage!entity:0CeCCCCCCCCCCCCCCCCCCfu1.1.,buffernewmessage!WEbrrtime:O.000870,type:1,frondayer5MAIN1.COP:datagiventostudent:A:Kew11essagearrives,sendMdndoWisVENTticDio:0.000891,typo:1.rondayer5BAIN1.COP:datagiventostudent:A:K

4、ewmessagearrives,sendwindowis1.VEOTtime:0.000909.type:I1frondayer5三AIN1.CX)P:datagiventostudent:A:New11ssagearrives,sendWiCdWis:VENrti:0.000913,type:1,frondayer5entity:0MAIN1.COP:datagiventostudent:dddddddddddddddddddd,A:New11essagearrives,sendWindOWisfu1.1.,buffernew11essage!WENrtime:0.000928,type:

5、I,fro11dayer5entity:0Siwj1.atorterminatedattime0.000928aftersending56sgsfromIayer5correct1.ysentpkts:9resentpkts:0青按任意道继续代码与注样:一、GBN.hpragmaonceinc1.ude基础功能模块的数据结构声明#defineBIDIRECTIONA1.1*changeto1ifyouredoingextracreditandwritearoutineca1.1.edB_output/a*msg*isthedataunitpassedfro三1.ayer5(teachersco

6、de)to1.ayer4(studentscode).Itcontainsthedata(characters)tobede1.iveredto1.ayer5viathestudentstransport1.eve1.protoco1.entities.*/structmsgchardata20;;/apacketisthedataunitpassedfrom1.ayer4(studentscode)to1.ayer3(teacherscode).Notethepre-definedpacketstructure,whicha1.1.studentsmustfo1.1.ow./structpk

7、t(intseqnus;intacknun;intchecksum;charpay1.oad20;);#defineWINDOfSIZE8#defineMAXBUFSIZE50defineRTT15.0WefineNOTUSED0ffdefineNACK-1MefineTRUE1defineFA1.SE0#defineB1网络仿真部分数据结构声明structeventf1.oatevtine;*eventtine/intevtype;/eventtypecode*/inteventity;/entitywhereeventoccurs*/structpkt*pktptr;/ptrtopacke

8、t(ifany)assocw/thisevent/structevent*prev;structevent*nxt;);/possib1.eevents:/#defineT1.ME1.1.1.NTERRUpT0ffdefineFROi1.1.AYER51#defineFRoi1.1.AYER32defineOFF0基础功能模块的函数声明voidCoaDputeChecksuin(structpkt*packet);计算校殴和intCheckCorrupted(structpktPaCket);检查数据是否出错voidA_output(structmsgDeSSage);A蟠向外发送数据void

9、A_input(structpktPaCket);A嵋接收数据voidAjinerinterruptO;/A计时器超时voidA-i11itO;/A端初始化voidB_output(structmsgmessage);voidB_input(structpktpacket);voidB-tinerinterruptO;voidB_initO;网络仿真部分的函数声明voidinit;初始化仿真器f1.oatJiBSrandO;随机数发生器0,1处理事务列表部分的函数声明voidgenerate_next_arriva1.O;产生下一个到达的分组voidinsertevent(structeven

10、t*p);向事务列表中插入一条新的事务voidPrinteV1.iSt();打印事务列表/aVb/9K91Vkajj1.jK2Bb1.jjKj2BJf1.1.scjK2BZZB1.S1.jjKjKH1.?B1.SCJK3K2HjU1.2f1.1.MCJKjKZB1.B1.jBCjKKZ0ZB2H1.IHCjkjKZf1.31.Jf1.JKK*计时器模块voidStoPtimer(int);停止计时器voidStarttiBer(int,f1.oat);启动计时器/aaTj1.jK2KS1.Jb1.jKijB1.k2HS1.jHjkjKjKJk1.BcjKjKjK2K2S1.jKjKJf1.1.

11、XCJKjKjKJkB1.jCjKAIH网络各层之间传送模块voidto1.ayer3(intAorB,structpktPaCket);向第3层发送信voidto1.ayer5(intAorB,chardatasent20);向第5层发送信息二、GBN.cinc1.ude*GBN.h*ffinc1.udetfinc1.udeinc1.udeexternintTRACE=1;/formydebugging/为我的调试externintnsin=0;/nunberofmessagesfro三5to4sofar*/目前为止信息的数字是从5到4externintnsinnax=0;/numberof

12、msgstogenerate,thenstop*/假如信息产生的数字为0,然后就停止externf1.oattime=0.000;f1.oatIossprob;*probabi1.itythatapacketisdropped*/数据包可能会丢失f1.oatcorruptprob;/probabi1.itythatonebitispacketisf1.ipped*/这一点的数据包可能会被弹出去f1.oat1.ambda:/arriva1.rateofnessagesfrom1.ayer5/第五层到达的信息的次序intnto1.ayer3;*numbersentinto1.ayer3/被传送到第

13、三层的数据staticintn1.ost=0;*nunber1.ostinmedia*/在媒介中数据丢失staticintncorrupt=0;/nu)rcorruptedbymedia*/被媒介毁坏的数据staticintexpectedseqnun=O;*expectedsequencenumberatreceiverside/.在接收者这边接收到Bi期的序列数据staticintnextseqnum;*nextsequencenumbertouseinsenderside/.下一个序列数据运用在发送者这边staticintbase;*theheadofsenderwindow/发送者的头

14、窗口structpktwinbufWINDOWSIZE;*windowpacketsbuffer/.数据包缓冲区窗口staticintwinfront,winrear;/frontandrearpointsofwindowbuffer/窗口缓冲区的前方点和后方点staticintPktnuI;/packetnumberofwindowbuffer/窗口缓冲区的数据包个数structmsgbufferMAXBUFSIZE;/senderessagebuffer/.发送消息缓冲区intbuffront,bufrear;/frontandrearpointersofbuffer/.缓冲区的前指针与后

15、指针staticintmsgnum;*I1.eSSagenu三berofbuffer/信息数量的缓冲intpacket_1.ost=0;intpacket_corrupt=0;intpacket-sent=0:externintpacket_correct=0;externintpackt-resent=0;intpacket_ti三eout=0;externstructevent*ev1.ist=NU1.1.;*theevent1.ist/计算校验和voidComputeChecksuD(structpkt*packet)(intchecksum;inti;checksum=packet-s

16、eqnun;checksum=checksu+packet-acknun;for(i=0;ipay1.oadi);CheCkSUm=0-chcksu三;packet-checksun=checksun;)检查是否出错intCheckCorrupted(structpktpacket)(intchecksum;inti;checksum=packet,seqnun;checksum=checksum+packet,acknm;for(i=0;i20;i+)checksum=checksum+(int)(packet,pay1.oadi);if(packet,checksunr+checksua)

17、=0)return(FA1.SE);e1.sereturn(TRUE);)/A端向外发送数据/ca1.1.edfrom1.ayer5,passedthedatatobesenttootherside/voidA_output(structmsgnessage)(inti;structpktsendpkt;/ifwindowisnotfu1.1./if(nextseqnumbase+WINDOWSIZE)(printf(*A:Newmessagearrives,sendWindO1.risnotfu1.1.,sendnewmessgeto1.ayer3!n*);/createpacket/sen

18、dpkt.seqnum=nextseqnum;sendpkt.acknum=NOTUSED;for(i=0;i20;i+)sendpkt.pay1.oadi=nessage.datai:/cM三puterchecksum/CoaputeChecksun(Isendpkt);/sendoutpacket/to1.ayer3(A,sendpkt);/copythepackettowindovpacketbuffer*/winrear=(winrar+1.)%WINDOWSIZE;pktnun+;vinbufwinrear=sendpkt;for(i=0;i20;i+)winbufwinrear.p

19、ay1.oadi=sendpkt.pay1.oadi;/updatestatevariab1.es/nextseqnu=nextseqnum+1;starttimer(A,RTT);B_input(sendpkt);A-input(sendpkt);/ifwindowisfu1.1./e1.se(printf(*A:Newmessagearrives,sendVindoWisfu1.1.,9;/ifbufferfu1.1.,giveupandexit*/if(BSgnum=MAXBUFSIZE)(printf(*Error:Senderbufferisfu1.1.(n);exit(1);)/o

20、therwise,bufferthemessage*/e1.se(printfCbuffernewnessage!n*);bufrar=(bufrear+1)%MAXBUFSIZE;for(i=0;i20;i)bufferbufrear.datai=message,datai;nsgnun+;/B端向外发送数据/ca1.1.edfra1.ayer5,passedthedatatobesenttootherside/voidB_output(structmsgnessage)(inti;structpktsendpkt;/ifIrindoVisnotfu1.1./if(nextseqnumbas

21、+WINDOWSIZE)(printf(*A:Newmessagearrives,sendWindOVisnotfu1.1.,sendnevmessgeto1.ayr3!n*);/createpacket/sendpkt.seqnum=nextseqnum;sendpkt.acknum=NOTUSED;for(i=0;i20;i)sendpkt.pay1.oadi=nessage.datai;/c三putrCheCkSU1.n*/ConuteChecksun(Asendpkt);/sendoutpacket/to1.ayer3(A,sendpkt);A_input(sendpkt)/copyt

22、hepackettowindowpacketbuffer*/winrear=(winrear+1)%WINDOWSIZE;pktnun+;winbufwinrear=sendpkt;for(i=0;i20;i+)winbufwinrar.pay1.oadi三sendpkt.pay1.oadi;/ifitisthefirstpacketinvindovrstarttimeout/if(base=nextseqnun)/starttimer(A,RTT);/printfCA:startanewtimer!n*);/*updatestatevariab1.es/nextseqnum=nextseqn

23、un+1:/ifwindowisfu1.1./e1.se(printf(A:Newmessagearrives,sendWindOVisfu1.1.,*)/ifbufferfu1.1.,giveupandexit*/if(asgnum=MAXBUFSIZE)(printf(*Error:Senderbufferisfu1.1.!n*);exit(1);)/otherwise,bufferthemessage/e1.se(printf(*buffernevmessage!n,);bufrear=(bufrear+1)%MAXBUFSIZE;for(i=0;i20;i)bufferbufrear.

24、datai=message,datai;msgnun+;1/A端接收数据voidA_input(structpktpacket)(structpktsendpkt;inti;/ifreceivedpacketisnotcorruptedandACKisreceived/if(CheckCorrupted(packet)=FA1.SE)&(packet,acknum!=NACK)(printf(*A:ACK%discorrect1.yreceived,*,packet,acknun);PaCket.correct+/de1.etetheackedpacketsfromwindowbuffer*/

25、winfront=(winfront+(packet,acknu三+1.-bas)%WINDOWSIZE;pktnun=pktnum-(packet,acknu三+1.-base);/BOVewindowbase/base=packet,acknun1.;Stoptimer(八);if(basenextseqnum)(/starttimer(A,RTT);printf(*nnnsndnewpackets!*);/ifbufferisnotempty,sendnewpackets/whi1.e(USgnUiI!=0)(nextseqnumbase+VINDOVSIZE)(/createpacke

26、t/sendpkt.seqnum=nextseqnum;sendpkt.acknum=NOTUSED;buffront=(buffront+1)%MAXBUFSIZE;for(i=0;inB:packet%discorrect1.yreceived,sendACK!n*,packet,seqnun);/sendanACKforthereceivedpacket/*createpacket/sendpkt.seqnum=NOTUSED:sendpkt.acknum=expectedseqnum;for(i=0;i20;i+)sendpkt.pay1.oadi=0;/c(xq)uterchecks

27、um*/ComputeChecksuB(Asendpkt);/sendoutpacket/to1.ayr3(B,sendpkt);/updatestatevariab1.es/expectedseqnum=expectedseqnum1.;printf(*B:expectedseqnum=%dn*,expectedseqnum):/de1.iverreceivedpacketto1.ayer5/to1.ayr5(B,packet,pay1.oad);/otherwise,discardthepacketandsendaNACK/e1.seprintf(*B:packet%discorrupte

28、dornotIexpects,sendNACK!n*,packet,seqnu);/createpacket/sendpkt.seqnum=NOTUSED;sendpkt.acknum=NACK;for(i=0;i20;i+)sendpkt.pay1.oadi=0;/coi三puterchecksum/CouteQecksuun(sendpkt);/sendoutpacket/to1.ayr3(B,sendpkt);)/A计时器超时/ca1.1.edwhenA,stimergoesoff/voidA-tinrinterrupt0inti;printf(*A:tineout,resendpack

29、ets!n*);/starttimer/Starttimer(A,RTT);/resenda1.1.packetsnotacked/for(i=1.;i=pktnu三;i+)(packet_resent+;to1.ayer3(A,winbuf(winfront+i)%WINDOISIZE);)./B计时器超时/ca1.1.edwhenB,stimergoesoff/voidB-tinerinterrupt0(inti;printf(*B:timeout,resendpackets!n*);/starttimer/Starttimer(B,RTT);/resenda1.1.packetsnota

30、cked/for(i=1.;i=pktnum;i+)packet-rsent+;to1.ayer3(B,winbuf(winfront+i)%WINDOfSIZE):)/A端初始化*entityAroutinesareca1.1.ed.Youcanuseittodoanyinitia1.ization/voidA_init0(base=0;nextseqnum=0;buffront=0;bufrear=0;Bsgnum=0;winfront=O;winrar=O;pktnum=O;)/B蟠初始化/entityBroutinesareca1.1.ed.Youcanuseittodoanyinit

31、ia1.ization*/voidB_initO(expectedseqnum=0;1初始化仿真器voidinit/initia1.izethesimu1.ator/(inti;f1.oatsum,avg;f1.oatjimsrand();FI1.Etfp;fp=fopenCparaoeter.txt,nEnteraveragetimebetweenmessagesfrosender*s1.ayr50.0:);/fscanf(fp,*%f*,ft1.abda);Scanfd1.ambda);printf(*nEntrTRACE:*);/fscanf(fp,*%d*,4TRACE);scanf%

32、d*,4TRACE);printf(*nn*);srand(9999);sum=0.0;/initrandonnunbergenerator/testrandomnumbergeneratorforstudents/for(i=0;i1000;i+)sum=sun+jinsrand();*jinsrand()shou1.dbeuniformin0,1/avg=sum/1000.0;*if(avg0.75)(printfC1.tis1.ike1.ythatrandomnumbergenerationonyourmachinen*);printfCisdifferentfroawhatthisem

33、u1.atorexpects.P1.easetaken*);printf(*a1.ookattheroutinejinsrand()intheeu1.atorcode.Sorry,n*);exit(0);/printf%f*,avg);nto1.ayer3=0:n1.ost=0;ncompt=0;time=0.0:/initia1.izetimeto0.0/generate_next_arriva1.0;/initia1.izeevent1.ist/随机数发生器f1.oatjimsrand()(doub1.enum=2147483647;/1.argestint-MACHINEDEPENDEN

34、T!I!H!/f1.oatx;/individua1.studentsmayneedtochangem三/x=randmmn;/xshou1.dbeuniformin0,1*/return(x):/事务处理部分voidgenerate_next_arriva1.0(doub1.ex,IogO,cei1.;structevent*evptr;f1.oatttine;intte三pint;/if(TRACE2)/printfGENERATENEXTARRIVA1.:creatingnewarriva1.n*);x=Ianbda*jimsrand0*2;/Xisuniformon0,2*1.aabd

35、a/havingmeanof1.ambda/vptr=(structevent*)11a1.Ioc(sizeof(structevent);evptr-evti三e=time+x;evptr-evtype=FR0M_1.AYER5;if(jinsrand()eventity=A;)e1.seevptr-eventity=B;insertevent(evptr);)向事务列表中插入一条新的事务voidinsertevent(structevent*p)(structevent*q,*qo1.d;if(TRACE2)(tinewi1.1./printf(*INSERTEVENT:is%1.fn*,

36、tine);/printf(*INSERTEVafr:futuretimebe%1.fn,p-vtime);q=ev1.ist;/qpointstofrontof1.istinIdIiChpstructinserted/if(q=NU1.1.)/*1.istisempty/(ev1.ist=p;p-next=NU1.1.;p-prev=NU1.1.;.)e1.se(for(qo1.d=q;q!=NU1.1.&p-evtineq-evtime;q=q-nxt)qo1.d=q;if(q=NU1.1.)/*endof1.ist/(qo1.d-next=p;p-prev=qo1.d;p-next=NU

37、1.1.;)e1.seif(q=ev1.ist)/*frontof1.ist/.p-next=ev1.ist;p-prev=NU1.1.;p-next-prev=p;ev1.ist=p;)e1.se/midd1.eof1.ist/(p-nxt=q;p-prev=q-prv;q-prev-next);q-prev=p;)打印事务列表voidprintv1.ist(structevent*q;inti;printf(,nEvnt1.istFo1.1.ows:n*);for(q=ev1.ist;q!=NU1.1.;q=q-next)(printf(*Eventtime:%f,type:%dentit

38、y:%dn*,q-evtime,q-evtype,q-evntity);)printf(*n*);)后动计时器voidStarttiner(intAo,f1.oatincrement)(structevent*q;structevent*evptr;if(TRACE2)printf(*n:STARTTIMER:startingtiuerat%fn*,time);/benice:checktoseeiftimerisa1.readystarted,ifso,thenwarn/for(q=ev1.ist;q!=NU1.1.&q-next!=NU1.1.;q=q-next)/for(q=ev1.is

39、t;q!=NU1.1.;q=q-next)if(q-evtype=TIMER_INTERRUPT&q-evntity=AorB)(/printf(Warning:attepttostartatimerthatisa1.readystartedn*);return;/createfutureeventforwhentimergoesoff/evptr=(structevent)11a1.1.oc(sizeof(structevent);evptr-evtie=time+increment;evptr-vtype=TIMER-INTERRUPT;evptr-ventity=AorB;insertevent(evptr);)停止计时器/ca1.1.edbystud

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

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号