Form1.cs
上传用户:zpsczj
上传日期:2022-02-13
资源大小:4436k
文件大小:12k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9. using System.Threading;
  10. using System.Collections;
  11. using WindowsApplication1;
  12. namespace wlcs
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         wjian w = new wjian();
  17.         DrawClass dc = new DrawClass();
  18.         public string name;
  19.         public int t, i = 1;
  20.         float s1, s2,s3,s4,s5,s6,p1,p2,p3;
  21.         float[] numbers1 = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
  22.         float[] numbers2 = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0 };
  23.         float[] numbers3 = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0 };
  24.         int[] num = new int[] { 0 };
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.         string sec = DateTime.Now.ToString();
  30.         public string shuju1, shuju2, shuju3;
  31.         private void Form1_Load(object sender, EventArgs e)
  32.         {
  33.             this.timer1.Interval = 1000;
  34.             this.timer1.Start();
  35.             this.timer1.Enabled = true;
  36.             string[] instanceNames;
  37.             ArrayList counters = new ArrayList();
  38.             PerformanceCounterCategory mycat1 = new PerformanceCounterCategory("Network Interface");
  39.             try
  40.             {
  41.                 instanceNames = mycat1.GetInstanceNames();
  42.                 if (instanceNames.Length == 0)
  43.                 {
  44.                     counters.AddRange(mycat1.GetCounters());
  45.                 }
  46.                 else
  47.                 {
  48.                     for (int i = 0; i < instanceNames.Length; i++)
  49.                     {
  50.                         counters.AddRange(mycat1.GetCounters(instanceNames[i]));
  51.                     }
  52.                 }
  53.                 for (int i = 0; i < instanceNames.Length; i++)
  54.                 {
  55.                     comboBox1.Items.Add( instanceNames[i]);
  56.                 }
  57.                 name = instanceNames[0];
  58.                 this.performanceCounter1.InstanceName = name;
  59.                 this.performanceCounter2.InstanceName = name;
  60.                 this.performanceCounter3.InstanceName = name;
  61.                 foreach (PerformanceCounter counter in counters)
  62.                 {
  63.                     Console.WriteLine(counter.CounterName);
  64.                 }
  65.                
  66.             }
  67.             catch (Exception)
  68.             {
  69.                 Console.WriteLine("Unable to list the counters for this category");
  70.             }
  71.             comboBox4.Text = Convert.ToString(20);
  72.         }
  73.         private void  timer1_Tick(object sender, EventArgs e)
  74.         {
  75.             float sulv1 = (float)this.performanceCounter1.NextValue(); 
  76.             this.toolStripLabel2.Text = "下载速率" + sulv1 / 1024 + "KB/s";
  77.             float sulv2 = (float)this.performanceCounter2.NextValue();
  78.             this.toolStripLabel3.Text = "上传速率" + sulv2 / 1024 + "KB/s";
  79.             float sulv3 = (float)this.performanceCounter3.NextValue();
  80.             this.toolStripLabel4.Text = "总速率" + sulv3 / 1024 + "KB/s";
  81.             t = t + 1;
  82.             s1 = s1 + sulv3 / 1024;
  83.             s3 = s3 + sulv2 / 1024;
  84.             s5 = s5 + sulv1 / 1024;
  85.             if (i > 0)
  86.             {
  87.                 numbers1[19] = numbers1[18];
  88.                 numbers1[18] = numbers1[17];
  89.                 numbers1[17] = numbers1[16];
  90.                 numbers1[16] = numbers1[15];
  91.                 numbers1[15] = numbers1[14];
  92.                 numbers1[14] = numbers1[13];
  93.                 numbers1[13] = numbers1[12];
  94.                 numbers1[12] = numbers1[11];
  95.                 numbers1[11] = numbers1[10];
  96.                 numbers1[10] = numbers1[9];
  97.                 numbers1[9] = numbers1[8];
  98.                 numbers1[8] = numbers1[7];
  99.                 numbers1[7] = numbers1[6];
  100.                 numbers1[6] = numbers1[5];
  101.                 numbers1[5] = numbers1[4];
  102.                 numbers1[4] = numbers1[3];
  103.                 numbers1[3] = numbers1[2];
  104.                 numbers1[2] = numbers1[1];
  105.                 numbers1[1] = numbers1[0];
  106.                 numbers1[0] = sulv1/1024;
  107.                 numbers2[19] = numbers2[18];
  108.                 numbers2[18] = numbers2[17];
  109.                 numbers2[17] = numbers2[16];
  110.                 numbers2[16] = numbers2[15];
  111.                 numbers2[15] = numbers2[14];
  112.                 numbers2[14] = numbers2[13];
  113.                 numbers2[13] = numbers2[12];
  114.                 numbers2[12] = numbers2[11];
  115.                 numbers2[11] = numbers2[10];
  116.                 numbers2[10] = numbers2[9];
  117.                 numbers2[9] = numbers2[8];
  118.                 numbers2[8] = numbers2[7];
  119.                 numbers2[7] = numbers2[6];
  120.                 numbers2[6] = numbers2[5];
  121.                 numbers2[5] = numbers2[4];
  122.                 numbers2[4] = numbers2[3];
  123.                 numbers2[3] = numbers2[2];
  124.                 numbers2[2] = numbers2[1];
  125.                 numbers2[1] = numbers2[0];
  126.                 numbers2[0] = sulv2/1024;
  127.                 numbers3[19] = numbers3[18];
  128.                 numbers3[18] = numbers3[17];
  129.                 numbers3[17] = numbers3[16];
  130.                 numbers3[16] = numbers3[15];
  131.                 numbers3[15] = numbers3[14];
  132.                 numbers3[14] = numbers3[13];
  133.                 numbers3[13] = numbers3[12];
  134.                 numbers3[12] = numbers3[11];
  135.                 numbers3[11] = numbers3[10];
  136.                 numbers3[10] = numbers3[9];
  137.                 numbers3[9] = numbers3[8];
  138.                 numbers3[8] = numbers3[7];
  139.                 numbers3[7] = numbers3[6];
  140.                 numbers3[6] = numbers3[5];
  141.                 numbers3[5] = numbers3[4];
  142.                 numbers3[4] = numbers3[3];
  143.                 numbers3[3] = numbers3[2];
  144.                 numbers3[2] = numbers3[1];
  145.                 numbers3[1] = numbers3[0];
  146.                 numbers3[0] = sulv3 / 1024;
  147.                 pictureBox1.Image = Image.FromStream(dc.draw(numbers1, numbers2, numbers3));
  148.             }
  149.             if (t == Convert.ToInt16(comboBox4.Text))
  150.             {
  151.                 if (i == 1)
  152.                 {
  153.                     listBox1.Items.Add("第1次" + "           " + s5 / t + "KB/s" + "      " + s3 / t + "KB/s" + "        " + s1 / t + "KB/s");
  154.                     s2 = s1;
  155.                     s4 = s3;
  156.                     s6 = s5;
  157.                     p1 = s1 / t;
  158.                     p2 = s3 / t;
  159.                     p3 = s5 / t;
  160.                     t = 0;
  161.                     s1 = 0;
  162.                     s3 = 0;
  163.                     s5 =0;
  164.                     i++;
  165.                     shuju1 += this.p1.ToString()+"  ";
  166.                     shuju2 += this.p2.ToString() + "  ";
  167.                     shuju3 += this.p3.ToString() + "  ";
  168.                 }
  169.                 else
  170.                 {
  171.                     listBox1.Items.Add("第" + i + "次" + "           " + s5 / t + "KB/s" + "      " + s3 / t + "KB/s" + "        " + s1 / t + "KB/s");
  172.                     listBox1.Items.Add("流量增加" + "        " + (s5 - s6) / t + "KB/s" + "      " + (s3 - s4) / t + "KB/s" + "        " + (s1 - s2) / t + "KB/s");
  173.                     s2 = s1;
  174.                     s4 = s3;
  175.                     s6 = s5;
  176.                     p1 = s1 / t;
  177.                     p2 = s3 / t;
  178.                     p3 = s5 / t;
  179.                     t = 0;
  180.                     s1 = 0;
  181.                     s3 = 0;
  182.                     s5 = 0;
  183.                     i++;
  184.                     shuju1 += this.p1.ToString() + "  ";
  185.                     shuju2 += this.p2.ToString() + "  ";
  186.                     shuju3 += this.p3.ToString() + "  ";
  187.                 }
  188.             
  189.             }
  190.             if (this.performanceCounter4.CategoryName == this.comboBox3.Text.ToString())
  191.             {
  192.                 float sulv4 = (float)this.performanceCounter4.NextValue();
  193.                 this.textBox1 .Text = sulv4 + "B/s";
  194.             }
  195.             else
  196.                 if (this.performanceCounter5.CategoryName == this.comboBox3.Text.ToString())
  197.                 {
  198.                     float sulv4 = (float)this.performanceCounter5.NextValue();
  199.                     this.textBox1.Text = sulv4 + "B/s";
  200.                 }
  201.                 else
  202.                     if (this.performanceCounter6.CategoryName == this.comboBox3.Text.ToString())
  203.                     {
  204.                         float sulv4 = (float)this.performanceCounter6.NextValue();
  205.                         this.textBox1.Text = sulv4 + "B/s";
  206.                     }
  207.                     else
  208.                         if (this.performanceCounter7.CategoryName == this.comboBox3.Text.ToString())
  209.                         {
  210.                             float sulv4 = (float)this.performanceCounter6.NextValue();
  211.                             this.textBox1.Text = sulv4 + "B/s";
  212.                         }
  213.         }
  214.         private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
  215.         {
  216.             this.performanceCounter1.InstanceName = this.comboBox1.Text.ToString();
  217.             this.performanceCounter2.InstanceName = this.comboBox1.Text.ToString();
  218.             this.performanceCounter3.InstanceName = this.comboBox1.Text.ToString();
  219.             float sulv1 = (float)this.performanceCounter1.NextValue();
  220.             this.toolStripLabel2.Text = "下载速率" + sulv1 / 1024 + "KB/s";
  221.             float sulv2 = (float)this.performanceCounter2.NextValue();
  222.             this.toolStripLabel3.Text = "上传速率" + sulv2 / 1024 + "KB/s";
  223.             float sulv3 = (float)this.performanceCounter3.NextValue();
  224.             this.toolStripLabel4.Text = "总速率" + sulv3 / 1024 + "KB/s";
  225.             t = 0;
  226.         }
  227.         private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
  228.         {
  229.             string sd = DateTime.Now.ToString();
  230.             richTextBox1.Text += "本次保存开始时间:" + sec.ToString() +"       "+this.comboBox4.Text+"秒内"+ "n" + "下载平均速率" + shuju3 + "n" + "上传平均速率" + shuju2 + "n" + "总平均速率" + shuju1 + "n结束时间:" + sd.ToString() + "n";
  231.             richTextBox1.SaveFile("..//Debug//wenjian.txt..", RichTextBoxStreamType.PlainText);
  232.         }
  233.         private void toolStripButton1_Click(object sender, EventArgs e)
  234.         {
  235.             string sd = DateTime.Now.ToString();
  236.             richTextBox1.Text += "本次保存开始时间:" + sec.ToString() + "       " + this.comboBox4.Text + "秒内" + "n" + "下载平均速率" + shuju3 + "n" + "上传平均速率" + shuju2 + "n" + "总平均速率" + shuju1 + "n结束时间:" + sd.ToString() + "n";
  237.             richTextBox1.SaveFile("..//Debug//wenjian.txt..", RichTextBoxStreamType.PlainText);
  238.         }
  239.         private void toolStripButton5_Click(object sender, EventArgs e)
  240.         {
  241.             this.performanceCounter1.InstanceName = this.comboBox1.Text.ToString();
  242.             this.performanceCounter2.InstanceName = this.comboBox1.Text.ToString();
  243.             this.performanceCounter3.InstanceName = this.comboBox1.Text.ToString();
  244.             float sulv1 = (float)this.performanceCounter1.NextValue();
  245.             this.toolStripLabel2.Text = "下载速率" + sulv1 / 1024 + "KB/s";
  246.             float sulv2 = (float)this.performanceCounter2.NextValue();
  247.             this.toolStripLabel3.Text = "上传速率" + sulv2 / 1024 + "KB/s";
  248.             float sulv3 = (float)this.performanceCounter3.NextValue();
  249.             this.toolStripLabel4.Text = "总速率" + sulv3 / 1024 + "KB/s";
  250.             t = 0;
  251.         }
  252.     }
  253. }