• 大小: 4KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: 其他
  • 标签: 滑动窗口  仿真  

资源简介

对于计算机网络中滑动窗口协议的仿真实现。

资源截图

代码片段和文件信息

#include “stdio.h“
#include “malloc.h“

#define MAXSIZE 10                          /*窗口大小,即队列大小*/

int i=0;                                     /*全局变量,代表当前指向确认表的位置*/

typedef struct                              /*发送窗口每帧的数据*/
{
int snum;  
int timeout;/*非0时代表发送计时,=6时表明超时*/
}send;

typedef struct                  /*发送窗口*/
{
send s[MAXSIZE];
int front;
int rear;
}wsend;

typedef struct                  /*接收窗口*/
{
int r[MAXSIZE];
int front;
int rear;
}wrece;

void Init(wsend *wswrece *wr)  /*对发送和接收窗口进行初始化*/
{
ws->front=ws->rear=0;
wr->front=wr->rear=0;
}

int SEND(wsend *wswrece *wrint frame)
{
if((ws->rear+1)%MAXSIZE==ws->front)return 0;   /*发送窗口已满*/
if(ws->front==ws->rear)                        /*若窗口为空,则直接进入*/
{
ws->s[ws->rear].snum=frame;
ws->s[ws->rear].timeout=1;
ws->rear=(ws->rear+1)%MAXSIZE;
wr->r[wr->rear]=frame;
wr->rear=(wr->rear+1)%MAXSIZE;
return 1;
}
else                                         /*若窗口不为空,则判断是否是可传送的帧*/ 
{
if(frame==(ws->s[ws->rear-1].snum)+1)
{
ws->s[ws->rear].snum=frame;
ws->s[ws->rear].timeout=1;
ws->rear=(ws->rear+1)%MAXSIZE;
wr->r[wr->rear]=frame;
wr->rear=(wr->rear+1)%MAXSIZE;
return 1;
}
else if(frame<(ws->s[ws->rear-1].snum)+1)printf(“该帧已发送!!\n“);
else if(frame>(ws->s[ws->rear-1].snum)+1)printf(“该帧不可发送!!\n“);
}
}

int RECE(wrece *wrint frameint ack[])
{
if(wr->front==wr->rear) return 0;       /*接收窗口为空*/
else                                    /*接收窗口接收帧号,并返回确认*/
{
if(wr->r[wr->front]==frame) 
{
wr->front=(wr->front+1)%MAXSIZE;
printf(“第%d帧出接收窗口,向发送窗口返回确认。\n“frame);
ack[i]=frame;
i++;
}
else   printf(“该帧不是期望收到的帧,舍弃。\n“);
return 1;
}
}

int ACKNOWLEDGE(wsend *wsint ack[])
{
int j;
if(ws->front==ws->rear) return 0;        /*发送窗口为空*/
if(ack[0]==ws->s[ws->front].snum)        /*发送窗口进

评论

共有 条评论