您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > C语言模拟滑动窗口协议
#includestdio.h#includestdlib.h#includemalloc.hmain(){structswphdr{unsignedlongseqnum;unsignedlongacknum;unsignedshortflags;/*flag=0±íʾÕâÊÇÒ»¸öÊý¾ÝÖ¡£¬flag=1±íʾÕâÊÇÒ»¸öACKÖ¡*/};structsendq_slot{unsignedshorttimeout;/*1ʱ±íʾ¼ÆʱÆô¶¯£¬0ʱ±íʾÒÑÊÕµ½È·ÈÏ*/unsignedlongmsg;structsendq_slot*next;};structrecvq_slot{unsignedshortreceived;/*1±íʾ˳Ðò½ÓÊÕ£¬0±íʾ´íÐò½ÓÊÜ*/unsignedlongmsg;structrecvq_slot*next;};structswpstate{unsignedlonglar;unsignedlonglfs;unsignedlongsws;structswphdrhdr;unsignedlongrws;structsendq_slot*sendq;unsignedlongnfe;structrecvq_slot*head;}swpstate1;unsignedlonga;intflag,b;/*·¢ËÍ*/structsendq_slot*sendq_rear,*sendp,*p3,*p4;structrecvq_slot*recvp,*recvq_rear,*p1,*p2;swpstate1.head=NULL;swpstate1.sendq=sendq_rear=(structsendq_slot*)malloc(sizeof(structsendq_slot));if(!swpstate1.sendq)exit(1);sendq_rear-next=NULL;printf(ÇëÊäÈë´°¿Ú´óС£º);scanf(%ld,&swpstate1.sws);swpstate1.rws=swpstate1.sws;if(swpstate1.sws0){printf(ÇëÊäÈëµÚÒ»Ö¡µÄÐòÁкţº);scanf(%ld,&swpstate1.hdr.seqnum);}swpstate1.nfe=swpstate1.hdr.seqnum;sendp=(structsendq_slot*)malloc(sizeof(structsendq_slot));if(!sendp)exit(1);sendp-msg=swpstate1.hdr.seqnum;sendp-timeout=1;sendp-next=NULL;sendq_rear-next=sendp;sendq_rear=sendp;--swpstate1.sws;swpstate1.lfs=swpstate1.hdr.seqnum;swpstate1.lar=swpstate1.hdr.seqnum;do{while(swpstate1.sws0){sendp=(structsendq_slot*)malloc(sizeof(structsendq_slot));if(!sendp)exit(1);sendp-msg=swpstate1.lfs+1;sendp-timeout=1;sendp-next=NULL;sendq_rear-next=sendp;sendq_rear=sendp;--swpstate1.sws;++swpstate1.lfs;}swpstate1.hdr.acknum=0;swpstate1.hdr.flags=0;printf(×î½üÊÕµ½µÄACKµÄÖ¡ÐòºÅ£º%ld\n,swpstate1.lar);printf(×î½ü·¢Ë͵ÄÖ¡ÐòºÅ£¨·¢ËÍÐÂÖ¡ºó£©£º%ld\n,swpstate1.lfs);/*½ÓÊÕ*/if(swpstate1.hdr.flags==0){do{printf(ÇëÊäÈëÊÕµ½µÄÊý¾ÝÖ¡ºÅ£º);scanf(%ld,&a);if(a=swpstate1.nfe&&a=swpstate1.lfs){if(swpstate1.head==NULL){recvp=recvq_rear=(structrecvq_slot*)malloc(sizeof(structrecvq_slot));recvp-next=NULL;swpstate1.head=recvp;}elseif(swpstate1.head!=NULL){recvp=(structrecvq_slot*)malloc(sizeof(structrecvq_slot));recvp-next=NULL;recvq_rear-next=recvp;recvq_rear=recvp;}}else{printf(ËùÊäÊý¾Ý²»ÔÚ½ÓÊÕ´°¿ÚÄÚ£¡);break;}recvp-msg=a;if(recvp-msg==swpstate1.nfe)recvp-received=1;elserecvp-received=0;--swpstate1.rws;if(recvp-received==1){a=a-1;do{a=a+1;if(swpstate1.head==NULL)break;p1=swpstate1.head;flag=0;while((a!=p1-msg)&&(p1-next!=NULL)){p2=p1;p1=p1-next;}if(a==p1-msg){flag=1;if(p1==swpstate1.head)swpstate1.head=swpstate1.head-next;elsep2-next=p1-next;swpstate1.nfe=a+1;swpstate1.hdr.acknum=a+1;swpstate1.hdr.flags=1;}}while(flag==1);}printf(ACKºÅ£¨ÆÚ´ýµÄÏÂÒ»Ö¡µÄÐòºÅ£©£º%ld\n,swpstate1.nfe);printf(û°´Ðò½ÓÊܵÄÐòºÅ:\n);p1=swpstate1.head;while(p1!=NULL){printf(%ld\t,p1-msg);p1=p1-next;}printf(\n);printf(¼ÌÐø½ÓÊÕÖ¡£¿ÈôÊÇ£¬ÊäÈë1£¬Èô²»ÊÇ£¬ÊäÈë0.\n);scanf(%d,&b);}while((swpstate1.rws0)&&(b==1));}if(swpstate1.hdr.flags==1){p3=swpstate1.sendq-next;flag=0;while((swpstate1.hdr.acknum)!=p3-msg&&p3-next!=NULL){p4=p3;p3=p3-next;}if(swpstate1.hdr.acknum==p3-msg){flag=1;if(p3-msg==swpstate1.sendq-next-msg)swpstate1.sendq-next=p3;elseswpstate1.sendq-next=p3;}swpstate1.sws=swpstate1.sws+(swpstate1.sendq-next-msg-swpstate1.lar);swpstate1.lar=swpstate1.sendq-next-msg;swpstate1.hdr.seqnum=swpstate1.hdr.acknum;printf(×î½üÊÕµ½µÄACKµÄÖ¡ÐòºÅ£¨ÊÕµ½ACKºó£©£º%ld\n,swpstate1.lar);printf(×î½ü·¢Ë͵ÄÖ¡ÐòºÅ£¨´Ëʱ»¹Î´·¢ËÍеÄÊý¾Ý£©£º%ld\n,swpstate1.lfs);}printf(¼ÌÐøÖ´ÐÐÂð£¿ÊÇÇëÊäÈë1\n);scanf(%d,&b);}while(b==1&&swpstate1.sws0);}
本文标题:C语言模拟滑动窗口协议
链接地址:https://www.777doc.com/doc-8456883 .html