慢启动、拥塞避免

参考源: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 &times,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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值