VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#开发路由选择程序

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

  

路由器名字 路由器子网掩码 路由器网络地址

  r1

255.255.252.0 130.50.15.0
路由选择算法可以说是在路由器这个网络层就解决的问题了,最近学习了路由选择算法,所以把它实现为程序,方便以后的计算。

  下面是思路,一个数据包被发送到路由端,它包含了目的ip地址(130.50.15.9),它要选择一个路由器来继续发送,路由器有r1。真实事件中有很多个路由可以选择,这里只简单的判断,能否透过此路由来发送这个数据包。

  判断方法:把目的ip地址转换为2进制,把路由子网掩码也换成2进制,两个值逐位相与,最后的结果又换回十进制点分后的ip地址,如果网络号和路由器网络地址相同,则可以通过它转发,否则不行。下面给出代码:

  拥有的资源:

  资源Code

privateSystem.Windows.Forms.NumericUpDownnumericUpDown1;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown2;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown3;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown4;
    privateSystem.Windows.Forms.Labellabel1;
    privateSystem.Windows.Forms.Labellabel2;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown5;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown6;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown7;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown8;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown9;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown10;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown11;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown12;
    privateSystem.Windows.Forms.Buttonbutton1;
    privateSystem.Windows.Forms.Labellabel3;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown13;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown14;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown15;
    privateSystem.Windows.Forms.NumericUpDownnumericUpDown16;
    privateSystem.Windows.Forms.Labellabel4;

 

  代码开始:

  Code

privatevoidbutton1_Click(objectsender,EventArgse)
    {
      bytea1=ConvertToString((byte)numericUpDown1.Value);
     bytea2=ConvertToString((byte)numericUpDown2.Value);
     bytea3=ConvertToString((byte)numericUpDown3.Value);
      bytea4=ConvertToString((byte)numericUpDown4.Value);
      byteb1=ConvertToString((byte)numericUpDown8.Value);
     byteb2=ConvertToString((byte)numericUpDown7.Value);
      byteb3=ConvertToString((byte)numericUpDown6.Value);
      byteb4=ConvertToString((byte)numericUpDown5.Value);
      byten1=ConvertToString((byte)numericUpDown12.Value);
     byten2=ConvertToString((byte)numericUpDown11.Value);
      byten3=ConvertToString((byte)numericUpDown10.Value);
      byten4=ConvertToString((byte)numericUpDown9.Value);
      numericUpDown16.Value=(a1&b1);
      numericUpDown15.Value=(a2&b2);
      numericUpDown14.Value=(a3&b3);
      numericUpDown13.Value=(a4&b4);
      
      
     if(Match(a1,b1,n1)&&Match(a2,b2,n2)&&Match(a3,b3,n3))
      {
        MessageBox.Show("可以通过该路由器转发","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
      }
      else
      {
        MessageBox.Show("不可以转发,请换一个路由器","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
      }
    }
    privatebyteConvertToString(bytex)
    {
      stringy=Convert.ToString(x,2).PadLeft(8,'0');
      bytey1=Convert.ToByte(y,2);
      returny1;
    }
    privateboolMatch(bytex,bytey,bytez)
    {
      if((z&(x&y))==z)
      {
        returntrue;
      }
      else
      {
        returnfalse;
      }
    }

 

 

  下图显示计算结果:130.50.12.0和路由地址(网络地址)130.50.15.0不同所以不能转发。

  c#开发路由选择程序

  计算机在交流中得到发展,所以有了互联网。技术在交流中成长,所以有了cnblog!请不要吝啬自己的才华,尽情的与我们分享吧! 

 



相关教程