计算机网络原理GBN协议代码

上传人:ba****u6 文档编号:168267758 上传时间:2022-11-08 格式:DOCX 页数:24 大小:33.95KB
收藏 版权申诉 举报 下载
计算机网络原理GBN协议代码_第1页
第1页 / 共24页
计算机网络原理GBN协议代码_第2页
第2页 / 共24页
计算机网络原理GBN协议代码_第3页
第3页 / 共24页
资源描述:

《计算机网络原理GBN协议代码》由会员分享,可在线阅读,更多相关《计算机网络原理GBN协议代码(24页珍藏版)》请在装配图网上搜索。

1、、GBN.h#pragma once#include /基础功能模块的数据结构声明#define BIDIRECTIONAL 1 /* change to 1 if youre doing extra credit and write a routine called B_output */* a msg is the data unit passed from layer 5 (teachers code) to layer4 (students code). It contains the data (characters) to be delivered to layer 5 via t

2、he students transport level protocol entities. */ struct msg char data20;/* a packet is the data unit passed from layer 4 (students code) to layer3 (teachers code). Note the pre-defined packet structure, which all students must follow. */struct pktint seqnum;int acknum;int checksum;char payload20;#d

3、efine WINDOWSIZE 8 #define MAXBUFSIZE 50 #define RTT 15.0 #define NOTUSED 0 #define NACK -1 #define TRUE 1 #define FALSE 0 #define A 0 #define B 1/ 网络仿真部分数据结构声明* struct eventfloat evtime;int evtype;int eventity; struct pkt *pktptr; struct event *prev;/* event time */* event type code */* entity wher

4、e event occurs */* ptr to packet (if any) assoc w/ this event */struct event *next; ;/* possible events: */#define TIMER_INTERRUPT 0#defineFROM_LAYER51#defineFROM_LAYER32#defineOFF0#defineON1/ 基 础 功能模块的函数声void ComputeChecksum(struct pkt *packet); 计算校验和 int CheckCorrup ted(s truct pkt packe t); 检査数据是

5、否出错 void A_output( struct msg message);/A 端向外发送数据 void A_input(struct pkt packet);/A 端接收数据 void A_timerinterrupt();/A 计时器超时void A_init();/A 端初始化void B_output(struct msg message);void B_input(struct pkt packet);void B_timerinterrupt();void B_init();/ 网 络 仿 真 部 分 的 函 数 声 明*void init(); /初始化仿真器float ji

6、msrand(); 随机数发生器0,1/ 处 理 事 件 列 表 部 分 的 函 数 声 明*void genera te_next_arrival(); 产生下一个到达的分组void inser tevent(s truct event *p);向事件列表中插入一条新的事件void printevlis t(); 打印事件列表void st op timer(int);/停止计时器void st ar tt imer(int,floa t); 启动计时器/*/* 网 络 各 层 之 间 传 送 模 块void tolayer3(int AorB,struct pkt packet) ; /向

7、第3层发送信息void tolayer5(int AorB,char datasent20);向第 5 层发送信息二、GBN.c#include GBN.h#include #include #include /* for my debugging */*numberofmessagesfrom5to4sofar/* number of msgs to generate, then stopextern int TRACE = 1;extern int nsim = 0;*/extern int nsimmax = 0;*/extern float time = 0.000;float los

8、sprob; float corruptprob;*/* probability that a packet is dropped */ /*probabilitythatonebitispacketisflippedfloat lambda;int ntolayer3;static int nlost = 0;static int ncorrupt = 0;/* arrival rate of messages from layer 5 */* number sent into layer 3 */* number lost in media */* number corrupted by

9、media*/static int expectedseqnum = 0; /* expected sequence number at receiver side */static int nextseqnum; /* next sequence number to use in sender side */static int base; /* the head of sender window */struct pkt winbufWINDOWSIZE; /* window packets buffer */static int winfront,winrear; /* front an

10、d rear points of window buffer */static int pktnum; /* packet number of window buffer */struct msg bufferMAXBUFSIZE; /* sender message buffer */int buffront,bufrear; /* front and rear pointers of buffer */ static int msgnum; /* message number of buffer */int packet_lost =0;int packet_corrupt=0;int p

11、acket_sent =0;extern int packet_correct=0;extern int packet_resent =0;int packet_timeout=0;extern struct event *evlist = NULL; /* the event list */计算校验和void ComputeChecksum( struct pkt *packet)int checksum;int i;checksum = packet-seqnum;checksum = checksum + packet-acknum;for ( i=0; ipayloadi);check

12、sum = 0-checksum; packet-checksum = checksum;/检查是否出错int CheckCorrupted(struct pkt packet)int checksum;int i;checksum = packet.seqnum;checksum = checksum + packet.acknum;for ( i=0; i20; i+ ) checksum = checksum + (int)(packet.payloadi);if ( (packet.checksum+checksum) = 0 ) return (FALSE);elsereturn (

13、TRUE);/A端向外发送数据/* called from layer 5, passed the data to be sent to other side */ void A_output(struct msg message)int i;struct pkt sendpkt;/* if window is not full */if ( nextseqnum base+WINDOWSIZE )printf(A: New message arrives, send window is not full, send newmessge to layer3!n);/* create packe

14、t */ sendpkt.seqnum = nextseqnum; sendpkt.acknum = NOTUSED;for ( i=0; i20 ; i+ )sendpkt.payloadi = message.datai;/* computer checksum */ ComputeChecksum (&sendpkt);/* send out packet */ tolayer3 (A, sendpkt);/* copy the packet to window packet buffer */ winrear = (winrear+1)%WINDOWSIZE;pktnum +; win

15、bufwinrear = sendpkt;for (i=0; i20; i+) winbufwinrear.payloadi= sendpkt.payloadi;/* update state variables */ nextseqnum = nextseqnum+1; starttimer(A,RTT);B_input(sendpkt);A_input(sendpkt);/* if window is full */elseprintf(A: New message arrives, send window is full,);/* if buffer full, give up and

16、exit*/ if ( msgnum = MAXBUFSIZE)printf ( Error: Sender buffer is full! n); exit (1);/* otherwise, buffer the message */ else printf(buffer new message!n); bufrear = (bufrear+1) % MAXBUFSIZE;for (i=0; i20; i+)bufferbufrear.datai = message.datai;msgnum +;/B端向外发送数据/* called from layer 5, passed the dat

17、a to be sent to other side */ void B_output(struct msg message)int i;struct pkt sendpkt;/* if window is not full */if ( nextseqnum base+WINDOWSIZE )printf(A: New message arrives, send window is not full, send newmessge to layer3!n);/* create packet */ sendpkt.seqnum = nextseqnum; sendpkt.acknum = NO

18、TUSED;for ( i=0; i20 ; i+ ) sendpkt.payloadi = message.datai;/* computer checksum */ ComputeChecksum (&sendpkt);/* send out packet */ tolayer3 (A, sendpkt);A_input(sendpkt);/* copy the packet to window packet buffer */ winrear = (winrear+1)%WINDOWSIZE;pktnum +; winbufwinrear = sendpkt;for (i=0; i20;

19、 i+) winbufwinrear.payloadi= sendpkt.payloadi;/* if it is the first packet in window, start timeout */ /if ( base = nextseqnum )/starttimer(A,RTT);/printf(A: start a new timer!n);/ /* update state variables */ nextseqnum = nextseqnum+1;/* if window is full */elseprintf(A: New message arrives, send w

20、indow is full,);/* if buffer full, give up and exit*/if ( msgnum = MAXBUFSIZE)printf ( Error: Sender buffer is full! n); exit (1);/* otherwise, buffer the message */ else printf(buffer new message!n); bufrear = (bufrear+1) % MAXBUFSIZE;for (i=0; i20; i+)bufferbufrear.datai = message.datai; msgnum +;

21、A端接收数据void A_input(struct pkt packet)struct pkt sendpkt;int i;/* if received packet is not corrupted and ACK is received */ if ( (CheckCorrupted(packet) = FALSE) & (packet.acknum != NACK) ) printf(A: ACK %d is correctly received,packet.acknum);packet_correct+;/* delete the acked packets from window

22、buffer */winfront = (winfront+(packet.acknum+1-base) % WINDOWSIZE;pktnum = pktnum - (packet.acknum+1-base);/* move window base */base = packet.acknum+1;stoptimer(A);if ( base nextseqnum)/starttimer(A,RTT);printf (nnnsend new packets!);/* if buffer is not empty, send new packets */while ( (msgnum!=0)

23、 & (nextseqnumbase+WINDOWSIZE) )/* create packet */sendpkt.seqnum = nextseqnum;sendpkt.acknum = NOTUSED;buffront = (buffront+1) % MAXBUFSIZE;for ( i=0; i20 ; i+ )sendpkt.payloadi = bufferbuffront.datai;/* computer checksum */ComputeChecksum (&sendpkt);/* if it is the first packet in window, start ti

24、meout */ if ( base = nextseqnum )/starttimer(A,RTT);printf (send new packets!n);/* send out packet */ tolayer3 (A, sendpkt);/* copy the packet to window packet buffer */ winrear = (winrear+1)%WINDOWSIZE; winbufwinrear = sendpkt;pktnum +;/* update state variables */ nextseqnum = nextseqnum+1;/* delet

25、e message from buffer */ msgnum -;elseprintf (A: NACK is received, do nothing!n);/B 端接收数据*一定 要调用这个/* Note that with simplex transfer from a-to-B, there is no B_output() */ /* called from layer 3, when a packet arrives for layer 4 at B*/ void B_input(struct pkt packet)struct pkt sendpkt;int i;/* if n

26、ot corrupted and received packet is in order */if ( (CheckCorrupted(packet) = FALSE) & (packet.seqnum = expectedseqnum)printf(nB: packet %d is correctly received, sendACK!n,packet.seqnum);/* send an ACK for the received packet */* create packet */ sendpkt.seqnum = NOTUSED;sendpkt.acknum = expectedse

27、qnum; for ( i=0; i20 ; i+ )sendpkt.payloadi = 0;/* computer checksum */ComputeChecksum (&sendpkt);/* send out packet */tolayer3 (B, sendpkt);/* update state variables */expectedseqnum = expectedseqnum+1;printf(B:expectedseqnum = %dn,expectedseqnum);/* deliver received packet to layer 5 */ /tolayer5(

28、B,packet.payload);/* otherwise, discard the packet and send a NACK */elsesendprintf(B: packet %d is corrupted or not I expects,NACK!n,packet.seqnum);/* create packet */sendpkt.seqnum = NOTUSED;sendpkt.acknum = NACK;for ( i=0; i20 ; i+ ) sendpkt.payloadi = 0;/* computer checksum */ComputeChecksum (&s

29、endpkt);/* send out packet */tolayer3 (B, sendpkt);/A计时器超时/* called when As timer goes off */void A_timerinterrupt()int i;printf(A: time out,resend packets!n);/* start timer */starttimer(A,RTT);/* resend all packets not acked */ for ( i=1; i=pktnum; i+ ) packet_resent+;tolayer3(A,winbuf(winfront+i)%

30、WINDOWSIZE); /B 计时器超时/* called when Bs timer goes off */ void B_timerinterrupt()int i;printf(B: time out,resend packets!n);/* start timer */ starttimer(B,RTT);/* resend all packets not acked */ for ( i=1; i0.0: );/fscanf(fp,%f,&lambda);scanf(%f,&lambda);printf(nEnter TRACE: ); /fscanf(fp,%d,&TRACE);

31、 scanf(%d,&TRACE);printf(nn);srand(9999); /* init random number generator */ sum = 0.0; /* test random number generator for students */for (i=0; i1000; i+)sum=sum+jimsrand(); /* jimsrand() should be uniform in 0,1 */ avg = sum/1000.0;/*if(avg 0.75)printf(It is likely that random number generation on

32、 your machinen ); printf(is different from what this emulator expects. Please taken); printf(a look at the routine jimsrand() in the emulator code. Sorry. n);exit(0);*/printf(%f,avg); ntolayer3 = 0; nlost = 0; ncorrupt = 0;time=0.0; /* initialize time to 0.0 */ generate_next_arrival(); /* initialize

33、 event list */随机数发生器float jimsrand()double mmm = 2147483647; /* largest int - MACHINE DEPENDENT! */float x;*/x = rand()/mmm;/* individual students may need to change mmm/* x should be uniform in 0,1 */return(x);*/* 事 件 处 理 部 分 *void generate_next_arrival()double x,log(),ceil();struct event *evptr;ch

34、ar *malloc();float ttime;int tempint;/if (TRACE2)/printf(GENERATE NEXT ARRIVAL: creating newarrivaln);x = lambda*jimsrand()*2; /* x is uniform on 0,2*lambda */* having mean of lambda */evptr = (struct event *)malloc(sizeof(struct event); evptr-evtime = time + x;evptr-evtype = FROM_LAYER5;if (jimsran

35、d()eventity = A;else evptr-eventity = B;insertevent(evptr);/向事件列表中插入一条新的事件 void insertevent(struct event *p) struct event *q,*qold;if (TRACE2)/printf( INSERTEVENT: time is %lfn,time);/printf( INSERTEVENT: future time will be %lfn,p-evtime);q = evlist; */* q points to front of list in which p struct

36、inserted if (q=NULL)/* list is empty */evlist=p;p-next=NULL;p-prev=NULL;elsefor (qold = q; q !=NULL & p-evtime q-evtime; q=q-next) qold=q;if (q=NULL)/* end of list */qold-next = p;p-prev = qold;p-next = NULL;else if (q=evlist)/* front of list */ p-next=evlist; p-prev=NULL; p-next-prev=p; evlist = p;

37、else /* middle of list */p-next=q;p-prev=q-prev;q-prev-next=p; q-prev=p;/打印事件列表void printevlist()struct event *q;int i;printf(nEvent List Follows:n);for(q = evlist; q!=NULL; q=q-next) printf(Event time: %f, type: entity: %dn,q-evtime,q-evtype,q-eventity);printf(n);/启动计时器void starttimer(int AorB,floa

38、t increment)struct event *q;struct event *evptr;char *malloc();if (TRACE2)printf(nA: START TIMER: starting timer at %fn,time);%dwarn/* be nice: check to see if timer is already started, if so, then */* for (q=evlist; q!=NULL & q-next!=NULL; q = q-next) */ for (q=evlist; q!=NULL ; q = q-next)if ( (q-

39、evtype=TIMER_INTERRUPT & q-eventity=AorB) )/printf(Warning: attempt to start a timer that is already startedn);return;/* create future event for when timer goes off */ evptr = (struct event *)malloc(sizeof(struct event); evptr-evtime = time + increment;evptr-evtype = TIMER_INTERRUPT;evptr-eventity =

40、 AorB; insertevent(evptr);/停止计时器/* called by students routine to cancel a previously-started timer */ void stoptimer(int AorB) /* A or B is trying to stop timer */struct event *q,*qold;if (TRACE2)printf(nA: STOP TIMER: stopping timern);/* for (q=evlist; q!=NULL & q-next!=NULL; q = q-next) */for (q=e

41、vlist; q!=NULL ; q = q-next)if ( (q-evtype=TIMER_INTERRUPT & q-eventity=AorB) )/* remove this event */if (q-next=NULL & q-prev=NULL) evlist=NULL; /* remove first and only event on list*/else if (q-next=NULL) /* end of list - there is one in front */ q-prev-next = NULL;else if (q=evlist) /* front of

42、list - there must be event after*/q-next-prev=NULL; evlist = q-next;else /* middle of list */q-next-prev = q-prev; q-prev-next = q-next;free(q);return;/printf(Warning: unable to cancel your timer. It wasnt running.n);/向第三层发送信息/aaaaaaaaaaaaaaaaaaaaaaaaaa TCT A VT7D Q aaaaaaaaaaaaaaa /* TOLAYER3 */voi

43、d tolayer3(int AorB,struct pkt packet)struct pkt *mypktptr;struct event *evptr,*q;char *malloc();float lastime, x, jimsrand();int i;ntolayer3+;/* simulate losses: */if (jimsrand() 0)printf( TOLAYER3: packet being lostn);return;/* make a copy of the packet student just gave me since he/she may decide

44、 */* to do something with the packet after we return back to him/her */ mypktptr = (struct pkt *)malloc(sizeof(struct pkt);mypktptr-seqnum = packet.seqnum;mypktptr-acknum = packet.acknum; mypktptr-checksum = packet.checksum;for (i=0; ipayloadi = packet.payloadi;if (TRACE2)printf( TOLAYER3: seq: %d,

45、ack %d, check: %d mypktptr-seqnum,mypktptr-acknum, mypktptr-checksum);for (i=0; ipayloadi);printf();/* create future event for arrival of packet at the other side */ evptr = (struct event *)malloc(sizeof(struct event);evptr-evtype = FROM_LAYER3; /* packet will pop out from layer3 */ evptr-eventity =

46、 (AorB) % 2; /* event occurs at other entity */ evptr-pktptr = mypktptr; /* save ptr to my copy of packet */ /* finally, compute the arrival time of packet at the other end. medium can not reorder, so make sure packet arrives between 1 and 10 time units after the latest arrival time of packets curre

47、ntly in the medium on their way to the destination */lastime = time;/* for (q=evlist; q!=NULL & q-next!=NULL; q = q-next) */for (q=evlist; q!=NULL ; q = q-next)if ( (q-evtype=FROM_LAYER3 & q-eventity=evptr-eventity) ) lastime = q-evtime;evptr-evtime = lastime + 1 + 9*jimsrand();/* simulate corruptio

48、n: */if (jimsrand() corruptprob)ncorrupt+;if ( (x = jimsrand() payload0=Z; /* corrupt payload */else if (x seqnum = 999999;elsemypktptr-acknum = 999999;if (TRACE0)printf( TOLAYER3: packet being corruptedn);/if (TRACE2)/printf( TOLAYER3: scheduling arrival on other siden); insertevent(evptr);/向第五层发送信

49、息/* TOLAYER5 */void tolayer5(int AorB,char datasent20)int i;if (TRACE2)printf( TOLAYER5: data received: );for (i=0; i20; i+)printf(%c,datasenti);printf(n);三、GBN-CS.c#include extern int TRACE ;/* for my debugging */* number of messages from 5 to 4 so far/* number of msgs to generate, then stop */#inc

50、lude GBN.hextern int nsim ;*/extern int nsimmax;extern float time;extern int packet_correct; extern int packet_resent;extern struct event *evlist;int main()struct event *eventptr;struct msg msg2give;struct pkt pkt2give;int i,j;char c;init();A_init();B_init();while (1)eventptr = evlist; /* get next e

51、vent to simulate */ if (eventptr=NULL)goto terminate;evlist = evlist-next; /* remove this event from event list */if (evlist!=NULL) evlist-prev=NULL;if (TRACE = 2)printf(nEVENT time: %f,eventptr-evtime); printf( type: %d,eventptr-evtype);if (eventptr-evtype=0) printf(, timerinterrupt );else if (even

52、tptr-evtype=1) printf(, fromlayer5 );elseprintf(, fromlayer3 );printf( entity: %dn,eventptr-eventity);time = eventptr-evtime; /* update time to next event time */if (nsim=nsimmax)break; /* all done with simulation */if (eventptr-evtype = FROM_LAYER5 ) generate_next_arrival(); /* set up future arriva

53、l */* fill in msg to give with string of same letter */ j = nsim % 26;for (i=0; i2)printf( MAINLOOP: data given to student: ); for (i=0; ieventity = A)A_output(msg2give);elseB_output(msg2give);else if (eventptr-evtype = FROM_LAYER3)pkt2give.seqnum = eventptr-pktptr-seqnum; pkt2give.acknum = eventptr-pktptr-acknum; pkt2give.checksum = eventptr-pktptr-checksum; for (i=0; ipktptr-payloadi;if (eventptr-eventity = A)A_input(pkt2give);elseB_input(pkt2give);free(eventptr-pktptr);else if (eventptr-evtype = TIMER_INTERRUPT) if

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!