您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页数据链路层“回退N帧窗口协议”模拟程序

数据链路层“回退N帧窗口协议”模拟程序

来源:五一七教育网

#include<iostream>
#include<conio.h>
#include<windows.h>
#include<cstdlib>

using namespace std;

class Protocol           //定义一个协议类
{
public:
       Protocol(int m,int c,int n);      //协议类的构造函数
       void Sender();                    //发送方函数
       bool Reciever();                  //接收方函数
private:
        int MAXSIZE;                     //定义发送窗口
        int count;                       //定义一个在程序中用到的辅助计数变量
        int number;                      //定义窗口中用来循环发送消息的序列号
        bool ackNumber;                  //定义为bool类型的确认号变量
};

Protocol::Protocol(int m,int c,int n)     //定义Protocol类的构造函数,用来初始化成员变量
{
    MAXSIZE=m;
    count=c;
    number=n;
}


bool Protocol::Reciever()     //定义接收方函数
{
     int random;
     srand((unsigned)time(NULL));
     random=rand()%10;
     if(random<=7)           //产生0-7时代表网络通信正常,8-9时代表网络中出现通信错误
     return true;
     else
     return false;
}

void Protocol::Sender()     //定义发送方函数
{
     int Queue[MAXSIZE-1];       //使用了代表缓存区的循环队列
     int front=0;
     int rear=0;
     cout<<"----------------The Stimulating Programme Of DataLink Protocol Five.-----------"<<endl;
     while(number<=MAXSIZE)
     {
       Sleep(1000);
       cout<<"The Sender is Sending the No."<<number<<" message\n";
       Queue[rear]=number;
       rear=(rear+1)%(MAXSIZE-1);
       if(count>=6)
       {
            ackNumber=Reciever();
            if(!ackNumber)              //当网络通信出现错误时,重发定时器超时的那一帧
            {
                front=(rear+6)%6;
                number=Queue[front];
                for(int i=0;i<7;i++)
                Queue[i]=0;
                cout<<"The No."<<number<<" is Out Time.So We Need To Send Again."<<endl;
                count=-1;
            }
            else
            number=(number+1)%MAXSIZE;
       }
       else
       {
            number=(number+1)%MAXSIZE;         //循环的发送序号
       }
       count++;
     }
}

int main()
{
    Protocol protocol(8,0,0);         //实例化一个协议,并将其最大发送窗口定义为8
    protocol.Sender();
    system("PAUSE");
    return 0;
}

转载于:https://www.cnblogs.com/thinkingoverlimao/archive/2011/10/04/2198794.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务