c#开发路由选择程序

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

r1

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

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

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

拥有的资源:

private System.Windows.Forms.NumericUpDown numericUpDown1;
        private System.Windows.Forms.NumericUpDown numericUpDown2;
        private System.Windows.Forms.NumericUpDown numericUpDown3;
        private System.Windows.Forms.NumericUpDown numericUpDown4;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.NumericUpDown numericUpDown5;
        private System.Windows.Forms.NumericUpDown numericUpDown6;
        private System.Windows.Forms.NumericUpDown numericUpDown7;
        private System.Windows.Forms.NumericUpDown numericUpDown8;
        private System.Windows.Forms.NumericUpDown numericUpDown9;
        private System.Windows.Forms.NumericUpDown numericUpDown10;
        private System.Windows.Forms.NumericUpDown numericUpDown11;
        private System.Windows.Forms.NumericUpDown numericUpDown12;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.NumericUpDown numericUpDown13;
        private System.Windows.Forms.NumericUpDown numericUpDown14;
        private System.Windows.Forms.NumericUpDown numericUpDown15;
        private System.Windows.Forms.NumericUpDown numericUpDown16;
        private System.Windows.Forms.Label label4;

 

  代码开始:

 private void button1_Click(object sender, EventArgs e)
        {
            byte a1 = ConvertToString((byte)numericUpDown1.Value);

           byte a2 = ConvertToString((byte)numericUpDown2.Value);

          byte a3 = ConvertToString((byte)numericUpDown3.Value);

            byte a4 = ConvertToString((byte)numericUpDown4.Value);

            byte b1 = ConvertToString((byte)numericUpDown8.Value);
           byte b2 = ConvertToString((byte)numericUpDown7.Value);
            byte b3 = ConvertToString((byte)numericUpDown6.Value);
            byte b4 = ConvertToString((byte)numericUpDown5.Value);

            byte n1 = ConvertToString((byte)numericUpDown12.Value);
           byte n2 = ConvertToString((byte)numericUpDown11.Value);
            byte n3 = ConvertToString((byte)numericUpDown10.Value);
            byte n4 = 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);
            }
        }
        private byte ConvertToString(byte x)
        {
            string y = Convert.ToString(x,2).PadLeft(8,'0');
            byte y1 = Convert.ToByte(y,2);
            return y1;
        }
        private bool Match(byte x, byte y,byte z)
        {
            if ((z&(x&y))==z)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值