『原創』c#開發路由選擇程式

來源:互聯網
上載者:User
路由器名字 路由器子網路遮罩 路由器網路地址

r1

255.255.252.0 130.50.15.0

路由選擇演算法可以說是在路由器這個網路層就解決的問題了,最近學習了路由選擇演算法,所以把它實現為程式,方便以後的計算。

下面是思路,一個資料包被發送到路由端,它包含了目的ip地址(130.50.15.9),它要選擇一個路由器來繼續發送,路由器有r1。真實事件中有很多個路由可以選擇,這裡只簡單的判斷,能否透過此路由來發送這個資料包。

判斷方法:把目的ip地址轉換為2進位,把路由子網路遮罩也換成2進位,兩個值逐位相與,最後的結果又換回十進位點分後的ip地址,如果網路號和路由器網路地址相同,則可以通過它轉寄,否則不行。下面給出代碼:

 

擁有的資源:

 

 

資源Code
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;

 

代碼開始:

 

Code
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不同所以不能轉寄。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.