C语言模拟滑动窗口协议

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
main()
{
struct swphdr
{
unsigned long seqnum;
unsigned long acknum;
unsigned short flags; /*flag=0表示这是一个数据帧,flag=1表示这是一个ACK帧*/
};
struct sendq_slot
{
unsigned short timeout;/*1时表示计时启动,0时表示已收到确认*/
unsigned long msg;
struct sendq_slot *next;
};
struct recvq_slot
{
unsigned short received;/*1表示顺序接收,0表示错序接受*/
unsigned long msg;
struct recvq_slot *next;
};
struct swpstate
{
unsigned long lar;
unsigned long lfs;
unsigned long sws;
struct swphdr hdr;
unsigned long rws;
struct sendq_slot *sendq;
unsigned long nfe;
struct recvq_slot *head;
}swpstate1;
unsigned long a;
int flag,b;
/*发送*/
struct sendq_slot *sendq_rear,*sendp,*p3,*p4;
struct recvq_slot *recvp,*recvq_rear,*p1,*p2;
swpstate1.head=NULL;
swpstate1.sendq=sendq_rear=(struct sendq_slot*)malloc(sizeof(struct sendq_slot));
if(!swpstate1.sendq) exit(1);
sendq_rear->next=NULL;
printf("请输入窗口大小:");
scanf("%ld",&swpstate1.sws);
swpstate1.rws=swpstate1.sws;
if (swpstate1.sws>0)
{
printf("请输入第一帧的序列号:");
scanf("%ld",&swpstate1.hdr.seqnum);
}
swpstate1.nfe=swpstate1.hdr.seqnum;
sendp=(struct sendq_slot*)malloc(sizeof(struct sendq_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;
r=swpstate1.hdr.seqnum;
do
{
while(swpstate1.sws>0)
{
sendp=(struct sendq_slot*)malloc(sizeof(struct sendq_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",r);
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=(struct recvq_slot*)malloc(sizeof(struct recvq_slot));
recvp->next=NULL;
swpstate1.head=recvp;
}
else
if(swpstate1.head!=NULL)
{
recvp=(struct recvq_slot*)malloc(sizeof(struct recvq_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;
else
recvp->rece

ived=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;
else p2->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.rws>0)&&(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;
else swpstate1.sendq->next=p3;
}
swpstate1.sws=swpstate1.sws+(swpstate1.sendq->next->r);
r=swpstate1.sendq->next->msg;
swpstate1.hdr.seqnum=swpstate1.hdr.acknum;
printf("最近收到的ACK的帧序号(收到ACK后):%ld\n",r);
printf("最近发送的帧序号(此时还未发送新的数据):%ld\n",swpstate1.lfs);
}
printf("继续执行吗?是请输入1\n");
scanf("%d",&b);
}while(b==1&&swpstate1.sws>0);
}

相关文档
最新文档