参考源:https://blog.youkuaiyun.com/smilesundream/article/details/71149434
// CongestControl.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include <windows.h>
#include<cstdlib>
#define SIZE 30
using namespace std;
int asks[SIZE] = { 1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,1,1,1,1,-1};//确认序列
int h = 0;
int tag=0;
bool SlowStart( int cwnd)//慢启动
{
int i = cwnd,count=1;
while (i<= (2*cwnd-1))
{
printf_s("M%d发送到接收方,请确认!\n",i);
Sleep(1000);
if (asks[h++]==1){//如果被确认
printf_s("M%d已确认!\n", i);
count = 1;
i++;
}
else if(count<3)//未被确认
{
count++;
cout << "重新确认中。。。!" << endl;
}
else if(count==3)//3次未被确认
{
cout << "三次请求未回复!!!" << endl;
}
}
return true;
}
int CongestionAvoidance(int cwnd,int &restran) {//拥塞避免
int count = 1;
while (asks[h]!=-1)
{
printf_s("M%d发送到接收方,请确认!\n", cwnd);
Sleep(1000);
if (asks[h++] == 1) {//如果被确认
printf_s("M%d已确认!\n", cwnd);
count = 1;
cwnd++;//线性增加
}
else if (count < 3)//未被确认
{
count++;
cout << "重新确认中。。。!" << endl;
}
else if (count == 3)//3次未被确认
{
cout << "重新确认中。。。!" << endl;
count = 1;
restran = cwnd;
return 1;
}
}
return 0;
}
void CongestionAvoidanceCheck(int ×,int ssthresh) {
int cwnd = (int)pow(2, times);
if (cwnd<ssthresh)
{
cout << "**********慢启动***********" << endl;
if(SlowStart(cwnd))
times++;
}
if (cwnd>ssthresh)
{
int restran1, restran2;
if (tag == 0) {
cout << "**********拥塞避免***********" << endl;
tag = CongestionAvoidance(cwnd, restran1);
}
if (tag==1) {
/**///快恢复///**/
cout << "**********快恢复-拥塞避免***********" << endl;
cwnd = ssthresh / 2;
CongestionAvoidance(restran1,restran2);
}
}
if(cwnd == ssthresh) {
Sleep(1000);
if ((int)(rand() % 2) == 1) {
cout << "**********将慢启动***********" << endl;
}
else {
cout << "**********将拥塞避免***********" << endl;
}
}
//return false;
}
int main()
{
int time=0 ;
while (asks[h] != -1) {
CongestionAvoidanceCheck(time, 6);
}
return 0;
}