计算机网络 - 路由器查表过程模拟 C++(2024)

发布时间:2024年01月06日

1.题目描述

参考计算机网络教材 140 页 4.3 节内容,编程模拟路由器查找路由表的过程,用(目的地址 掩码 下一跳) 的 IP 路由表以及目的地址作为输入,为目的地址查找路由表,找出正确的下一跳并输出结果。

4f0bf7cb877a49c9b462a487e69c9bc7.png

1.当目的地址为128.96.39.10时下一跳

?同子网掩码255.255.255.128做与运算可得128.96.39.0 故接口m0转发

2.当目的地址为128.96.40.12 时下一跳

同子网掩码255.255.255.128做与运算可得128.96.40.0故接口R2转发

3.当目的地址为128.96.40.151 时下一跳

同子网掩码255.255.255.128做与运算可得128.96.40.128,同子网掩码255.255.255.192做与运算可得128.96.40.128,而此时没有目的网络,故通过默认网关进行转发下一跳为R4。

4.当目的地址为192.4.153.17时下一跳

同子网掩码255.255.255.128做与运算可得 192.4.153.0 但此时子网掩码(255.255.255.128)中对应的目的地址没有192.4.153.0。同子网掩码255.255.255.192做运算可得192.4.153.0.故此时通过R3进行转发。


2.程序Demo

0aa0a9c704204bd296effffd2518e033.png

其他功能自行探索(Doge)?


3.参考代码

//time:2024.1.4
//coder:MGJ

//注意
//https://baijiahao.baidu.com/s?id=1683531102064675651
//Demo题目

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
#include <sstream>

using namespace std;
//菜单功能数量
#define MENUNUMS1 4
//路由表最大数量
#define Router_max_num 128

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//函数声明
void cls();

void Init_data();

int Menuchoose1();

void Menushow1(int i);

void Function_Select(int select);

void Net_address_translation();

void Net_address_add();

void Net_address_delect();

void Net_address_revise();

void Stringsplit(string str, const char split, vector<string> &res);

void Net_Map_Show();

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//路由表
struct Router_Map {
    //目的网络地址,子网掩码,下一跳
    int Destination_network_address[4] = {0};
    int Subnet_mask[4] = {0};
    string Next_hop;

    BOOLEAN Init_Router_Map(string ip,string mask,string Next_hop)
    {
        if (ip=="n"|| mask=="n"||Next_hop=="n") return false;
        Set_Router_Map_Destination_add(ip);
        Set_Router_Map_mask_add(mask);
        this->Next_hop=Next_hop;
        return true;
    }

    void Set_Router_Map_Destination_add(string ip) {
        vector<string> strList;
        int num = 0, i = 0;
        Stringsplit(ip, '.', strList);     // 将子串存放到strList中
        for (auto str_num: strList) {
            istringstream ss(str_num);
            ss >> num;
            Destination_network_address[i++] = num;
        }
    }

    void Set_Router_Map_mask_add(string mask) {
        vector<string> strList;
        int num = 0, i = 0;
        Stringsplit(mask, '.', strList);    // 将子串存放到strList中
        for (auto str_num: strList) {
            istringstream ss(str_num);
            ss >> num;
            Subnet_mask[i++] = num;
        }
    }
};

Router_Map Router_Map_Data[Router_max_num];//用来存储表
int Now_Router_Num = 0;//当前的表大小
//----------------------------------------------------------------------------------
//主函数
int main() {
    int out = 0;
    Init_data();
    while (true) {
        out = Menuchoose1();
        Function_Select(out);
    }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//初始化函数
void Init_data() {
    //0位为默认网络/外部网络(必须有)
    Router_Map_Data[0].Init_Router_Map("0.0.0.0"  ,"255.255.255.255","R4");
    //下面可以自行修改添加
    Router_Map_Data[1].Init_Router_Map("128.96.39.0"  ,"255.255.255.128","接口M0");
    Router_Map_Data[2].Init_Router_Map("128.96.39.128","255.255.255.128","接口M1");
    Router_Map_Data[3].Init_Router_Map("128.96.40.0"  ,"255.255.255.128","R2");
    Router_Map_Data[4].Init_Router_Map("192.4.153.0"  ,"255.255.255.192","R3");
    Now_Router_Num = 4;//1-4 输入的项数
}

//选择函数
int Menuchoose1() {
    int ch, i = 0;
    Menushow1(0);
    while (true) {
        if (_kbhit()) {
            ch = _getch();
            if (ch == 80) {
                //循环
                i = (++i) % MENUNUMS1;
                Menushow1(i);
            } else if (ch == 72) {
                //循环防止出现负数
                i = ((--i) + MENUNUMS1) % MENUNUMS1;
                Menushow1(i);
            } else if (ch == 13) return i + 1;
        }
    }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//菜单显示
void Menushow1(int i) {
    cls();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                            FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << "\n *===== 路由器查表过程模拟 Demo=====*\n";
    if (i == 0)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
    cout << "          ->>>查询地址去向<<<-        " << endl;
    if (i == 1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
    if (i == 0)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                                FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << "          ->>>增加路由地址<<<-        " << endl;
    if (i == 2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
    if (i == 1)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                                FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << "          ->>>删除路由地址<<<-        " << endl;
    if (i == 3)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
    if (i == 2)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                                FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << "          ->>>修改路由地址<<<-        " << endl;
    if (i == 3)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                                FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << " -------------------------------------\n";
}

//功能函数
void Function_Select(int select) {
    Net_Map_Show();
    switch (select) {
        case 1: {
            Net_address_translation();
            break;
        }
        case 2: {
            Net_address_add();
            break;
        }
        case 3: {
            Net_address_delect();
            break;
        }
        case 4: {
            Net_address_revise();
            break;
        }
    }
    system("pause");
    system("cls");
}
void Net_Map_Show() {
    cout << "\t序号\t目的地址\t\t掩码\t\t下一跳" << endl;
    for (int i = 0; i <= Now_Router_Num; ++i) {
        cout << "  \t "<<i<<"\t";
        for (int j = 0; j < 4; ++j) {
            cout << Router_Map_Data[i].Destination_network_address[j];
            j != 3 ? cout << "." : cout << "  \t";
        }
        for (int j = 0; j < 4; ++j) {
            cout << Router_Map_Data[i].Subnet_mask[j];
            j != 3 ? cout << "." : cout << "\t\t";
        }
        cout << Router_Map_Data[i].Next_hop << endl;
    }
}
void Net_address_translation() {
    int Input_Destination[4] = {0};
    int Get_Destination[4] = {0};
    int Cal_num = 0;
    int Find_Local = 0;
    cout << "\n请输入查询的目的地址(如:128.96.39.10):";
    scanf("%d.%d.%d.%d", &Input_Destination[0], &Input_Destination[1], &Input_Destination[2], &Input_Destination[3]);
    for (int i = 1; i <= Now_Router_Num; ++i) {
        Cal_num = 0;
        for (int j = 0; j < 4; ++j) {
            Get_Destination[j] = Router_Map_Data[i].Subnet_mask[j] & Input_Destination[j];
            Cal_num = Cal_num + (Router_Map_Data[i].Destination_network_address[j] - Get_Destination[j]);
        }
        if (Cal_num == 0) {
            Find_Local = i;
            break;
        }
    }
    cout << endl << " -------------------------------------------" << endl;
    cout << " 计算出的目的地址为:";
    for (int j = 0; j < 4; ++j) {
        cout << Get_Destination[j];
        j != 3 ? cout << "." : cout << "\n";
    }
    cout << " 下一跳为: " << Router_Map_Data[Find_Local].Next_hop << endl;
    cout << " -------------------------------------------" << endl;
}



void Net_address_add() {
    string  input_Destination;
    string  input_Subnet_mask;
    string  input_Next_hop;
    cout << endl << " -------------(输入n可取消当前操作)-------------" << endl;
    cout<<" 请输入需要添加的目的网络:",cin>>input_Destination;
    cout<<" 请输入需要添加的网络掩码:",cin>>input_Subnet_mask;
    cout<<" 请输入需要添加的下一跳:",cin>>input_Next_hop;
    cout << endl << " -------------------------------------------" << endl;
    if(Router_Map_Data[Now_Router_Num+1].Init_Router_Map(input_Destination,input_Subnet_mask,input_Next_hop))
    Now_Router_Num++;
}

void Net_address_delect() {
    cout << " * 不想开发数组的删除与移动捏(doge), 请尝试修改吧" << endl;
}

void Net_address_revise() {
    string  input_Destination;
    string  input_Subnet_mask;
    string  input_Next_hop;
    int input_num;
    cout << endl << " -------------(输入n可取消当前操作)-------------" << endl;
    cout<<" 请输入需要修改的项序号(0-"<<Now_Router_Num<<"):",cin>>input_num;
    cout<<" 请输入修改的目的网络:",cin>>input_Destination;
    cout<<" 请输入修改的网络掩码:",cin>>input_Subnet_mask;
    cout<<" 请输入修改的下一跳:",cin>>input_Next_hop;
    cout << " -------------------------------------------" << endl;
    Router_Map_Data[input_num].Init_Router_Map(input_Destination,input_Subnet_mask,input_Next_hop);
}

//清屏函数
void cls() {
    COORD pos;
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    pos.Y = pos.X = 0;
    SetConsoleCursorPosition(hOut, pos);
}

//Stringsplit
void Stringsplit(string str, const char split, vector<string> &res) {
    istringstream iss(str);    // 输入流
    string token;              // 接收缓冲区
    while (getline(iss, token, split))    // 以split为分隔符
    {
        res.push_back(token);
    }
}

点个关注吧哈哈?

?2024 HNUST计算机网络课程设计-(????)?芒果酱-参考文章

代码可以参考,?? ? ? ? ?? 但同学们要认真编写哦
-------------------------------------------------------------------------
1、网络聊天程序的设计与实现
C++ Socket 多线程 网络聊天室 支持用户端双向交流(2023)-CSDN博客
2、Tracert 与 Ping 程序设计与实现
Tracert 与 Ping 程序设计与实现(2024)-CSDN博客
3、滑动窗口协议仿真
滑动窗口协议仿真(2024)-CSDN博客
4、OSPF 路由协议原型系统设计与实现
OSPF 路由协议原型系统设计与实现-CSDN博客
5、基于 IP 多播的网络会议程序
基于 IP 多播的网络会议程序(2024)-CSDN博客
6、编程模拟 NAT 网络地址转换
编程模拟 NAT 网络地址转换(2024)-CSDN博客
7、网络嗅探器的设计与实现
网络嗅探器的设计与实现(2024)-转载-CSDN博客
8、网络报文分析程序的设计与实现
网络报文分析程序的设计与实现(2024)-CSDN博客
9、简单 Web Server 程序的设计与实现
简单 Web Server 程序的设计与实现 (2024)-CSDN博客
10、路由器查表过程模拟

计算机网络 - 路由器查表过程模拟 C++(2024)-CSDN博客

文章来源:https://blog.csdn.net/weixin_46337464/article/details/135397829
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。