C # Development Routing Program

Source: Internet
Author: User

Router Name Router Subnet mask Router Network address
R1 255.255.252.0 130.50.15.0
routing algorithm can be said that the network layer in the router to solve the problem, and recently learned the routing algorithm, so it is implemented as a program to facilitate the calculation later.

Here is the idea, a packet is sent to the routing end, it contains the destination IP address (130.50.15.9), it will select a router to continue to send, the router has R1. There are many routes to choose from in real events, and here is a simple way of judging whether the packet can be sent through this route.

Judgment method: Converts the destination IP address to 2, the route subnet mask is also replaced by 2, two values by bit phase, the final result is changed back to the decimal point after the IP address, if the network number and router network address the same, you can forward through it, otherwise not. The following code is given:

Resources in Possession:

Resource 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 Start:

Code

 private void Button1_Click (object sender, EventArgs e) {byte A1 = converttostring ((byte) numeri

           Cupdown1.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 ("can
            Forwarding through the router, "System hints", messageboxbuttons.ok,messageboxicon.information); else {MessageBox.Show ("No forwarding, please switch to a router", "system prompts", Messageboxbuttons.ok,messageboxico
            N.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; }
        }

The following figure shows the results: 130.50.12.0 and routing addresses (network addresses) 130.50.15.0 different so they cannot be forwarded.

Computers have developed in communication, so there is the Internet. Technology grows in communication, so with cnblog! Please do not mean your talent, heartily share with us!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.