您的位置:首页 > 编程语言 > C#

C#下实现模拟red阻塞控制实验

2011-11-24 16:43 423 查看
最近计算机网络学习网络层的tcp协议相关程序,要模拟测试RED算法中的Thmin与Thmax故写如下算法,并开启我的博客第一篇,首先,我建立了一个数据源类用来模拟慢开始快重传,快恢复算法
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace RED{class ComputerDataSourse{private int currentvalue = 0;    //每次发送的包的个数public void send(){currentvalue++;Form1.currentQ += currentvalue;}public void reset(){currentvalue = 0;Form1.breaknum++;}}}
然后是控制类的实现
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Runtime.InteropServices;using Excel = Microsoft.Office.Interop.Excel;namespace RED{public partial class Form1 : Form{public  static   int M = 0;Random random = new Random();public  static  int Q = 0;public  static  decimal pmax;private int thmin = 0;private int thmax = 0;private int lastQ = 0;public static int Finshed = 0; //已经发送的包的个数;public static  int breaknum = 0;//丢弃掉的包的个数public  static int currentQ = 0;//当前队列的长度private int count = 0;//新到达的分组进入队列的个数private int des = 30;//每单位时间从路由队列中出去的包个数private int time = 0;private ComputerDataSourse d1, d2, d3, d4, d5;public Form1(){InitializeComponent();d1 = new ComputerDataSourse();d2 = new ComputerDataSourse();d3 = new ComputerDataSourse();d4 = new ComputerDataSourse();d5 = new ComputerDataSourse();timer1.Enabled = false;timer1.Interval = 50;}private decimal getP(){decimal  ptem = pmax*(currentQ-thmin)/(thmax-thmin);return (ptem/(1-count*ptem));}private void run(){if (Finshed < M){if (currentQ < thmin){d1.send();d2.send();d3.send();d4.send();d5.send();}else if (currentQ > thmax){d1.reset();d2.reset();d3.reset();d4.reset();d5.reset();}else{decimal p = getP();count += (int)(5 - (currentQ-lastQ) * p);for (int i = 0; i < 40 * p; i++){int r = random.Next(5);switch (r){case 0: d1.reset(); break;case 1: d2.reset(); break;case 2: d3.reset(); break;case 3: d4.reset(); break;case 4: d5.reset(); break;}}}lastQ = currentQ;if (currentQ > 30){Finshed += 30;currentQ -= 30;}else{Finshed += currentQ;currentQ = 0;}}else{timer1.Enabled = false;}}private void button1_Click(object sender, EventArgs e){Excel.Application m_excel = new Excel.Application();m_excel.SheetsInNewWorkbook = 1;Excel._Workbook m_book = (Excel._Workbook)(m_excel.Workbooks.Add(true));Excel._Worksheet m_sheet;}private void button_run_Click(object sender, EventArgs e){try{Finshed = 0; ;breaknum = 0;currentQ = 0;count = 0;time = 0;textBox_count.Text = "";M = Convert.ToInt16(textBox_m.Text.Trim());textBox_m.ReadOnly = true;Q = Convert.ToInt16(textBox_q.Text.Trim());textBox_q.ReadOnly = true;pmax = Convert.ToDecimal(textBox_p.Text.Trim());textBox_p.ReadOnly = true;thmin = Convert.ToInt16(textBox_min.Text.Trim());thmax = Convert.ToInt16(textBox_max.Text.Trim());}catch(Exception ee){MessageBox.Show(ee.Message);}timer1.Enabled = true;}private void timer1_Tick(object sender, EventArgs e){run();textBox_count.Text = breaknum.ToString();time++;textBox_time.Text = time.ToString();}private void Form1_Load(object sender, EventArgs e){}}}
界面实现 机器生成的
namespace RED{partial class Form1{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.components = new System.ComponentModel.Container();this.groupBox1 = new System.Windows.Forms.GroupBox();this.textBox_p = new System.Windows.Forms.TextBox();this.label4 = new System.Windows.Forms.Label();this.textBox_q = new System.Windows.Forms.TextBox();this.label3 = new System.Windows.Forms.Label();this.textBox_m = new System.Windows.Forms.TextBox();this.label2 = new System.Windows.Forms.Label();this.groupBox2 = new System.Windows.Forms.GroupBox();this.label1 = new System.Windows.Forms.Label();this.groupBox3 = new System.Windows.Forms.GroupBox();this.textBox_max = new System.Windows.Forms.TextBox();this.textBox_min = new System.Windows.Forms.TextBox();this.label7 = new System.Windows.Forms.Label();this.label6 = new System.Windows.Forms.Label();this.groupBox4 = new System.Windows.Forms.GroupBox();this.label5 = new System.Windows.Forms.Label();this.groupBox5 = new System.Windows.Forms.GroupBox();this.textBox_time = new System.Windows.Forms.TextBox();this.textBox_count = new System.Windows.Forms.TextBox();this.label9 = new System.Windows.Forms.Label();this.label8 = new System.Windows.Forms.Label();this.groupBox6 = new System.Windows.Forms.GroupBox();this.button_add = new System.Windows.Forms.Button();this.button_run = new System.Windows.Forms.Button();this.timer1 = new System.Windows.Forms.Timer(this.components);this.groupBox1.SuspendLayout();this.groupBox2.SuspendLayout();this.groupBox3.SuspendLayout();this.groupBox4.SuspendLayout();this.groupBox5.SuspendLayout();this.groupBox6.SuspendLayout();this.SuspendLayout();//// groupBox1//this.groupBox1.Controls.Add(this.textBox_p);this.groupBox1.Controls.Add(this.label4);this.groupBox1.Controls.Add(this.textBox_q);this.groupBox1.Controls.Add(this.label3);this.groupBox1.Controls.Add(this.textBox_m);this.groupBox1.Controls.Add(this.label2);this.groupBox1.Controls.Add(this.groupBox2);this.groupBox1.Location = new System.Drawing.Point(12, 12);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(469, 100);this.groupBox1.TabIndex = 0;this.groupBox1.TabStop = false;this.groupBox1.Text = "初始化数据固定区";//// textBox_p//this.textBox_p.Location = new System.Drawing.Point(119, 70);this.textBox_p.Name = "textBox_p";this.textBox_p.Size = new System.Drawing.Size(100, 21);this.textBox_p.TabIndex = 6;//// label4//this.label4.AutoSize = true;this.label4.Location = new System.Drawing.Point(29, 73);this.label4.Name = "label4";this.label4.Size = new System.Drawing.Size(83, 12);this.label4.TabIndex = 5;this.label4.Text = "初始化Pmax :";//// textBox_q//this.textBox_q.Location = new System.Drawing.Point(119, 44);this.textBox_q.Name = "textBox_q";this.textBox_q.Size = new System.Drawing.Size(100, 21);this.textBox_q.TabIndex = 4;//// label3//this.label3.AutoSize = true;this.label3.Location = new System.Drawing.Point(41, 47);this.label3.Name = "label3";this.label3.Size = new System.Drawing.Size(71, 12);this.label3.TabIndex = 3;this.label3.Text = "缓存队列Q:";//// textBox_m//this.textBox_m.Location = new System.Drawing.Point(119, 18);this.textBox_m.Name = "textBox_m";this.textBox_m.Size = new System.Drawing.Size(100, 21);this.textBox_m.TabIndex = 2;//// label2//this.label2.AutoSize = true;this.label2.Location = new System.Drawing.Point(6, 21);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(107, 12);this.label2.TabIndex = 1;this.label2.Text = "待发送分组总量M:";//// groupBox2//this.groupBox2.Controls.Add(this.label1);this.groupBox2.Location = new System.Drawing.Point(316, 0);this.groupBox2.Name = "groupBox2";this.groupBox2.Size = new System.Drawing.Size(153, 100);this.groupBox2.TabIndex = 0;this.groupBox2.TabStop = false;this.groupBox2.Text = "Notice";//// label1//this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(7, 21);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(113, 36);this.label1.TabIndex = 0;this.label1.Text = "该部分用于控制初始\r\n化,以后不要更改,\r\n以控制变量";//// groupBox3//this.groupBox3.Controls.Add(this.textBox_max);this.groupBox3.Controls.Add(this.textBox_min);this.groupBox3.Controls.Add(this.label7);this.groupBox3.Controls.Add(this.label6);this.groupBox3.Controls.Add(this.groupBox4);this.groupBox3.Location = new System.Drawing.Point(13, 135);this.groupBox3.Name = "groupBox3";this.groupBox3.Size = new System.Drawing.Size(468, 100);this.groupBox3.TabIndex = 1;this.groupBox3.TabStop = false;this.groupBox3.Text = "调节变量测试区";//// textBox_max//this.textBox_max.Location = new System.Drawing.Point(118, 53);this.textBox_max.Name = "textBox_max";this.textBox_max.Size = new System.Drawing.Size(100, 21);this.textBox_max.TabIndex = 5;//// textBox_min//this.textBox_min.Location = new System.Drawing.Point(118, 26);this.textBox_min.Name = "textBox_min";this.textBox_min.Size = new System.Drawing.Size(100, 21);this.textBox_min.TabIndex = 4;//// label7//this.label7.AutoSize = true;this.label7.Location = new System.Drawing.Point(15, 56);this.label7.Name = "label7";this.label7.Size = new System.Drawing.Size(95, 12);this.label7.TabIndex = 3;this.label7.Text = "最大门限 THmax:";//// label6//this.label6.AutoSize = true;this.label6.Location = new System.Drawing.Point(15, 29);this.label6.Name = "label6";this.label6.Size = new System.Drawing.Size(95, 12);this.label6.TabIndex = 2;this.label6.Text = "最小门限 THmin:";//// groupBox4//this.groupBox4.Controls.Add(this.label5);this.groupBox4.Location = new System.Drawing.Point(315, 0);this.groupBox4.Name = "groupBox4";this.groupBox4.Size = new System.Drawing.Size(153, 100);this.groupBox4.TabIndex = 1;this.groupBox4.TabStop = false;this.groupBox4.Text = "Notice";//// label5//this.label5.AutoSize = true;this.label5.Location = new System.Drawing.Point(7, 21);this.label5.Name = "label5";this.label5.Size = new System.Drawing.Size(149, 36);this.label5.TabIndex = 0;this.label5.Text = "该部分用于控制上下门限值\r\n以得到不同的几组值,达到\r\n模拟测试的效果";//// groupBox5//this.groupBox5.Controls.Add(this.textBox_time);this.groupBox5.Controls.Add(this.textBox_count);this.groupBox5.Controls.Add(this.label9);this.groupBox5.Controls.Add(this.label8);this.groupBox5.Location = new System.Drawing.Point(13, 241);this.groupBox5.Name = "groupBox5";this.groupBox5.Size = new System.Drawing.Size(242, 93);this.groupBox5.TabIndex = 3;this.groupBox5.TabStop = false;this.groupBox5.Text = "结果";//// textBox_time//this.textBox_time.Location = new System.Drawing.Point(118, 55);this.textBox_time.Name = "textBox_time";this.textBox_time.ReadOnly = true;this.textBox_time.Size = new System.Drawing.Size(100, 21);this.textBox_time.TabIndex = 3;//// textBox_count//this.textBox_count.Location = new System.Drawing.Point(118, 27);this.textBox_count.Name = "textBox_count";this.textBox_count.ReadOnly = true;this.textBox_count.Size = new System.Drawing.Size(100, 21);this.textBox_count.TabIndex = 2;//// label9//this.label9.AutoSize = true;this.label9.Location = new System.Drawing.Point(17, 58);this.label9.Name = "label9";this.label9.Size = new System.Drawing.Size(89, 12);this.label9.TabIndex = 1;this.label9.Text = "算法运行时间:";//// label8//this.label8.AutoSize = true;this.label8.Location = new System.Drawing.Point(15, 30);this.label8.Name = "label8";this.label8.Size = new System.Drawing.Size(89, 12);this.label8.TabIndex = 0;this.label8.Text = "丢弃的分组数:";//// groupBox6//this.groupBox6.Controls.Add(this.button_add);this.groupBox6.Controls.Add(this.button_run);this.groupBox6.Location = new System.Drawing.Point(281, 268);this.groupBox6.Name = "groupBox6";this.groupBox6.Size = new System.Drawing.Size(200, 56);this.groupBox6.TabIndex = 4;this.groupBox6.TabStop = false;this.groupBox6.Text = "控制";//// button_add//this.button_add.Location = new System.Drawing.Point(104, 26);this.button_add.Name = "button_add";this.button_add.Size = new System.Drawing.Size(90, 23);this.button_add.TabIndex = 1;this.button_add.Text = "填入测试结果";this.button_add.UseVisualStyleBackColor = true;this.button_add.Click += new System.EventHandler(this.button1_Click);//// button_run//this.button_run.Location = new System.Drawing.Point(6, 26);this.button_run.Name = "button_run";this.button_run.Size = new System.Drawing.Size(75, 23);this.button_run.TabIndex = 0;this.button_run.Text = "执行测试";this.button_run.UseVisualStyleBackColor = true;this.button_run.Click += new System.EventHandler(this.button_run_Click);//// timer1//this.timer1.Tick += new System.EventHandler(this.timer1_Tick);//// Form1//this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(493, 344);this.Controls.Add(this.groupBox6);this.Controls.Add(this.groupBox5);this.Controls.Add(this.groupBox3);this.Controls.Add(this.groupBox1);this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;this.MaximizeBox = false;this.Name = "Form1";this.Text = "RED拥塞控制算法";this.Load += new System.EventHandler(this.Form1_Load);this.groupBox1.ResumeLayout(false);this.groupBox1.PerformLayout();this.groupBox2.ResumeLayout(false);this.groupBox2.PerformLayout();this.groupBox3.ResumeLayout(false);this.groupBox3.PerformLayout();this.groupBox4.ResumeLayout(false);this.groupBox4.PerformLayout();this.groupBox5.ResumeLayout(false);this.groupBox5.PerformLayout();this.groupBox6.ResumeLayout(false);this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.Label label4;private System.Windows.Forms.TextBox textBox_q;private System.Windows.Forms.Label label3;private System.Windows.Forms.TextBox textBox_m;private System.Windows.Forms.Label label2;private System.Windows.Forms.GroupBox groupBox2;private System.Windows.Forms.Label label1;private System.Windows.Forms.TextBox textBox_p;private System.Windows.Forms.GroupBox groupBox3;private System.Windows.Forms.TextBox textBox_max;private System.Windows.Forms.TextBox textBox_min;private System.Windows.Forms.Label label7;private System.Windows.Forms.Label label6;private System.Windows.Forms.GroupBox groupBox4;private System.Windows.Forms.Label label5;private System.Windows.Forms.GroupBox groupBox5;private System.Windows.Forms.TextBox textBox_time;private System.Windows.Forms.TextBox textBox_count;private System.Windows.Forms.Label label9;private System.Windows.Forms.Label label8;private System.Windows.Forms.GroupBox groupBox6;private System.Windows.Forms.Button button_add;private System.Windows.Forms.Button button_run;private System.Windows.Forms.Timer timer1;}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: