Java模仿路由协议-rip(路由器仿真实验)

[复制链接]
发表于 2025-11-6 09:51:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
前言:

         很久不见,有段时间没有写文章了,本篇文章,由Blue我带各人来复现rip协议。我们以
  b站湖南西席匠所讲rip的视频中的例子为我这篇文章所模仿的路由路径
  如图:
  

  
模仿路径

  视频:http://【深入浅出盘算机网络 - 4.4.3 路由信息协议RIP】https://www.bilibili.com/video/BV1ST411K7nK?vd_source=bb412cc25ca27e171f8e17085daad038
 Rip协议工作原理:

   

  • 1、rip协议要求网络中每个路由器都要维护从它本身到其他每一个目的网络的隔断纪录。
  • 2、rip以为好的路由就是它通过的网路数目少的,及“隔断最短”。
  • 3、rip最多答应一条路径包罗15个网络。隔断为16时相称于不可达。
  • 4、仅和相邻路由器交换信息,这里的信息指的是全部网络的最短隔断,以及到每个网络应颠末的下一跳路由
  • 5、按固定时间隔断交换路由信息
   代码实现:

第一步:搭建项目

注意:我搭建的是boot项目,现实我这个复现,并没使用boot,以是你也可以不搭建boot
 项目结构:

第二步:界说路由表中的关键列(目的网络,隔断,下一跳)

RouterTable:
  1. package com.example.Kclass;
  2. /**
  3. * 路由表:
  4. * 1、目的网络
  5. * 2、距离
  6. * 3、下一跳路由器
  7. */
  8. public class RouterTable implements Cloneable{
  9.     private Integer AimNet;
  10.     private Integer distance;
  11.     private String NextRouter;
  12.     public RouterTable() {
  13.     }
  14.     @Override
  15.     protected Object clone() throws CloneNotSupportedException {
  16.         return super.clone();
  17.     }
  18.     public RouterTable(Integer AimNet, Integer distance, String NextRouter) {
  19.         this.AimNet = AimNet;
  20.         this.distance = distance;
  21.         this.NextRouter = NextRouter;
  22.     }
  23.     /**
  24.      * 获取
  25.      * @return AimNet
  26.      */
  27.     public Integer getAimNet() {
  28.         return AimNet;
  29.     }
  30.     /**
  31.      * 设置
  32.      * @param AimNet
  33.      */
  34.     public void setAimNet(Integer AimNet) {
  35.         this.AimNet = AimNet;
  36.     }
  37.     /**
  38.      * 获取
  39.      * @return distance
  40.      */
  41.     public Integer getDistance() {
  42.         return distance;
  43.     }
  44.     /**
  45.      * 设置
  46.      * @param distance
  47.      */
  48.     public void setDistance(Integer distance) {
  49.         this.distance = distance;
  50.     }
  51.     /**
  52.      * 获取
  53.      * @return NextRouter
  54.      */
  55.     public String getNextRouter() {
  56.         return NextRouter;
  57.     }
  58.     /**
  59.      * 设置
  60.      * @param NextRouter
  61.      */
  62.     public void setNextRouter(String NextRouter) {
  63.         this.NextRouter = NextRouter;
  64.     }
  65.     public String toString() {
  66.         return "RouterTable{AimNet = " + AimNet + ", distance = " + distance + ", NextRouter = " + NextRouter + "}";
  67.     }
  68. }
复制代码
第三步:界说路由器 

 Router:
  1. package com.example.Kclass;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Set;
  6. /**
  7. * 路由器的模板类
  8. *
  9. */
  10. public class Router {
  11.    private int id;
  12.    List<RouterTable> routerTables = new ArrayList<>();
  13.     public void setId(int id) {
  14.         this.id = id;
  15.     }
  16.     public int getId() {
  17.         return id;
  18.     }
  19.    public String find(int aimid){
  20.         String sc ="完成";
  21.         for (RouterTable e : routerTables){
  22.             if(e.getAimNet()==aimid){
  23.                 System.out.println("需要经过的路由器:"+this.id);
  24.                 System.out.println("");
  25.                 if(e.getNextRouter().equals("直连")){
  26.                     return sc;
  27.                 }else {
  28.                     return e.getNextRouter();
  29.                 }
  30.             }
  31.         }
  32.        return sc;
  33.    }
  34.     //添加操作
  35.     public  void add(RouterTable routerTable){
  36.         int count=0;
  37.         //存在相同的目的网络则不添加
  38.         for (RouterTable e1  : routerTables){
  39.             if(e1.getAimNet().equals(routerTable.getAimNet())){
  40.                 //不添加
  41.                 return;
  42.             }
  43.         }
  44.         routerTables.add(routerTable);
  45.     }
  46.     //更新操作
  47.     public void update(List<RouterTable> r1){
  48.         /**
  49.          * 遍历,看这个路由表满足这几种可能不?
  50.          * 1、没有就插入
  51.          * 2、有,如果下一跳相同则直接更新
  52.          * 3、有,下一跳不同,且r1的距离小于自身的,将下一跳换成r1的下一跳,并且更新
  53.          * 4、有,下一跳不同,且r1的距离大于自身的,不管舍弃
  54.          */
  55.         //遍历
  56.         for (int i=0;i<r1.size();i++){
  57.             for (int j=0;j<routerTables.size();j++){
  58.                 //是否存在相同的目的网络
  59.                 if(r1.get(i).getAimNet().equals(routerTables.get(j).getAimNet())){
  60.                     //是否下一跳也相同
  61.                     if(r1.get(i).getNextRouter().equals(routerTables.get(j).getNextRouter())){
  62.                         //相同,直接更新
  63.                         routerTables.get(j).setDistance(r1.get(i).getDistance());
  64.                         break;
  65.                     }else {
  66.                         //不同,判断距离
  67.                         if(routerTables.get(j).getDistance()>r1.get(i).getDistance()){
  68.                             routerTables.get(j).setDistance(r1.get(i).getDistance());
  69.                             break;
  70.                         }else {
  71.                             break;
  72.                         }
  73.                     }
  74.                 }
  75.                 if(j==routerTables.size()-1){
  76.                     routerTables.add(r1.get(i));
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     //打印其路由表
  82.     public void prints(){
  83.         System.out.println(id+"的路由表为:");
  84.         System.out.println("---目的网络---距离-----下一跳路由-----");
  85.         for(RouterTable e : routerTables){
  86.             System.out.println("---"+e.getAimNet()+"-----"+e.getDistance()+"------"+e.getNextRouter());
  87.             System.out.println("");
  88.         }
  89.     }
  90.     public  List<RouterTable> deepCopyRouterTables() {
  91.         List<RouterTable> copiedList = new ArrayList<>(routerTables.size());
  92.         for (RouterTable routerTable : routerTables) {
  93.             try {
  94.                 copiedList.add((RouterTable) routerTable.clone()); // 深拷贝
  95.             } catch (CloneNotSupportedException e) {
  96.                 e.printStackTrace(); // 处理异常
  97.             }
  98.         }
  99.         return copiedList;
  100.     }
  101. }
复制代码
第四步:界说网络

  1. <strong>NetWork:</strong>
复制代码
  1. package com.example.Kclass;
  2. public class NetWork {
  3.     private int id;
  4.     public NetWork() {
  5.     }
  6.     public NetWork(int id) {
  7.         this.id = id;
  8.     }
  9.     /**
  10.      * 获取
  11.      * @return id
  12.      */
  13.     public int getId() {
  14.         return id;
  15.     }
  16.     /**
  17.      * 设置
  18.      * @param id
  19.      */
  20.     public void setId(int id) {
  21.         this.id = id;
  22.     }
  23.     public String toString() {
  24.         return "NetWork{id = " + id + "}";
  25.     }
  26. }
复制代码
第五步:界说User

  1. package com.example.Kclass;
  2. public class User {
  3.      private int id ;
  4.      private int Routid;
  5.     public User() {
  6.     }
  7.     public User(int id, int Routid) {
  8.         this.id = id;
  9.         this.Routid = Routid;
  10.     }
  11.     /**
  12.      * 获取
  13.      * @return id
  14.      */
  15.     public int getId() {
  16.         return id;
  17.     }
  18.     /**
  19.      * 设置
  20.      * @param id
  21.      */
  22.     public void setId(int id) {
  23.         this.id = id;
  24.     }
  25.     /**
  26.      * 获取
  27.      * @return Routid
  28.      */
  29.     public int getRoutid() {
  30.         return Routid;
  31.     }
  32.     /**
  33.      * 设置
  34.      * @param Routid
  35.      */
  36.     public void setRoutid(int Routid) {
  37.         this.Routid = Routid;
  38.     }
  39.     public String toString() {
  40.         return "User{id = " + id + ", Routid = " + Routid + "}";
  41.     }
  42. }
复制代码
第六步:Ts代码

  1. package com.example.test;
  2. import com.example.Kclass.NetWork;
  3. import com.example.Kclass.Router;
  4. import com.example.Kclass.RouterTable;
  5. import com.example.Kclass.User;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9. import java.util.concurrent.CountDownLatch;
  10. /**
  11. * 1、四个网络
  12. * 2、四个路由器
  13. * 3、创建图(利用图来表示链接)
  14. * 4、该类目的是弄好路由器的交换表
  15. */
  16. public class Ts {
  17.     public static void main(String[] args) {
  18.         List<NetWork> netWorks =new ArrayList<>();
  19.         List<Router> routers =new ArrayList<>();
  20.         rip(netWorks,routers);
  21.         //用户界面
  22.         User user =new User(1,1);
  23.         MainPannel(routers,user);
  24.     }
  25.     public static void ppt(List<Router> routers){
  26.         for (Router e:routers){
  27.             e.prints();
  28.         }
  29.     }
  30.     public static void MainPannel(List<Router> routers,User user){
  31.         while (true){
  32.             int a;
  33.             System.out.println("选项1:打印所有路由表");
  34.             System.out.println("选项2:用户传输信息");
  35.             System.out.println("选项3:退出");
  36.             Scanner s =new Scanner(System.in);
  37.             System.out.println("请输入选项:");
  38.             a=s.nextInt();
  39.             switch (a){
  40.                 case 1:
  41.                     ppt(routers);
  42.                     break;
  43.                 case 2:
  44.                     userprint(routers,user);
  45.                     break;
  46.                 case 3:
  47.                     return;
  48.             }
  49.         }
  50.     }
  51.     public static void userprint(List<Router> routers, User user){
  52.         System.out.println("请输入需要传送到的网络");
  53.         int a;
  54.         Scanner sc = new Scanner(System.in);
  55.         a=sc.nextInt();
  56.         int t=a;
  57.         //知道自己从那个路由出发
  58.        int routid= user.getRoutid();
  59.         Router router = routers.get(routid - 1);
  60.         boolean flage =true;
  61.         while (flage){
  62.             String s = router.find(t);
  63.             if(s.equals("完成")){
  64.                 System.out.println("完成");
  65.                 return;
  66.             }
  67.             a = Integer.parseInt(s);
  68.             router=routers.get(a-1);
  69.         }
  70.     }
  71.     public static void rip( List<NetWork> netWorks, List<Router> routers) {
  72.         //生成四个网络
  73.         //令四个网络为1,2,3,4
  74.         for (int i=1;i<=4;i++){
  75.             NetWork e =new NetWork();
  76.             e.setId(i);
  77.             netWorks.add(e);
  78.         }
  79.         //生成四个路由器
  80.         //令四个路由器为5,6,7,8
  81.         for (int i=5;i<=8;i++){
  82.             Router e =new Router();
  83.             e.setId(i-4);
  84.             routers.add(e);
  85.         }
  86.         //生成线路
  87.         int[][] arry=CreateImage();
  88.         //rip基本原理规定:路由器刚开始工作只知道自己到直连网络的距离为1
  89.         //根据线路初始化
  90.         initialization(routers,arry);
  91.         //打印初始化后的每个路由器的路由表
  92. //        System.out.println("--------------------路由表的初始化-----------------------");
  93. //        for (Router e:routers){
  94. //            e.prints();
  95. //        }
  96.         //根据rip基本原理每个路由器仅和相邻路由器周期性地交换并更新路由信息
  97.         find(routers,arry);
  98. //        System.out.println("--------------------路由表的最终-----------------------");
  99. //        for (Router e:routers){
  100. //            e.prints();
  101. //        }
  102.     }
  103.     public static int[][] CreateImage(){
  104.         int[][] arry={
  105.                 {0,0,0,0,1,1,0,0},//N1所对应的行,0
  106.                 {0,0,0,0,1,0,1,0},//N2所对应的行,1
  107.                 {0,0,0,0,0,0,1,1},//N3所对应的行,2
  108.                 {0,0,0,0,0,1,1,1},//N4所对应的行,3
  109.                 {1,1,0,0,0,0,0,0},//R1所对应的行,4
  110.                 {1,0,0,1,0,0,0,0},//R2所对应的行,5
  111.                 {0,1,1,1,0,0,0,0},//R3所对应的行,6
  112.                 {0,0,1,1,0,0,0,0} //R4所对应的行,7
  113.         };
  114.         return arry;
  115.     }
  116.     //初始化
  117.     public static void initialization(List<Router> routers,int[][] arry){
  118.         for (int i=4;i<=7;i++){
  119.             for (int j=0;j<4;j++){
  120.                 if(arry[i][j]==0){
  121.                     continue;
  122.                 }
  123.                 if(arry[i][j]==1){
  124.                     routers.get(i-4).add(new RouterTable(j+1,1,"直连"));
  125.                 }
  126.             }
  127.         }
  128.     }
  129.     //用于更新
  130.     public static void exchenge(Router e1,Router e2){
  131.       //先让e1去更新e2
  132.         // 深拷贝
  133.         List<RouterTable> Copye2 = e2.deepCopyRouterTables();
  134.         String next=e2.getId()+"";
  135.         //根据rip协议,让其距离加1,下一跳变为e1
  136.         for (RouterTable table :Copye2){
  137.             table.setDistance(table.getDistance()+1);
  138.             table.setNextRouter(next);
  139.         }
  140.         e1.update(Copye2);
  141.         //e2去更新e1
  142.         String next2=e1.getId()+"";
  143.         //深拷贝
  144.         List<RouterTable> copy1 = e1.deepCopyRouterTables();
  145.         //根据rip协议,让其距离加1,下一跳变为e2
  146.         for (RouterTable table :copy1){
  147.             table.setDistance(table.getDistance()+1);
  148.             table.setNextRouter(next2);
  149.         }
  150.         e2.update(copy1);
  151.     }
  152.     //用于遍历数组,查找相同路由器
  153.     public static void find(List<Router> routers,int[][] arry){
  154.         //双指针遍历
  155.         int A=0;
  156.         int B=0;
  157.         for (int i=0;i<4;i++){
  158.             boolean flage=true;
  159.             //优化操作
  160.             if(flage) {
  161.                 for (int j = 4; j < 8; j++) {
  162.                     if (arry[i][j] == 1) {
  163.                         A = j;//找到第一个1,然后继续下去找
  164.                         B=j;
  165.                          flage=false;
  166.                          break;
  167.                     }
  168.                 }
  169.             }
  170.             for (int j=B+1;j<8;j++){
  171.                 if (arry[i][j]==1)
  172.                 {
  173.                     //执行更新
  174.                     exchenge(routers.get(A-4),routers.get(j-4));
  175.                     //判断是否还有可能存在相邻
  176.                     if(j<7){
  177.                         int s=B;
  178.                         B=j;
  179.                         for (int w=B+1;w<8;w++){
  180.                             if(arry[i][w]==1){
  181.                                 exchenge(routers.get(A-4),routers.get(w-4));
  182.                                 exchenge(routers.get(s-4),routers.get(w-4));
  183.                             }else {
  184.                                 break;
  185.                             }
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.     }
  192. }
复制代码
实现效果:

主页面:


选项1:


选项2:


选项3:


结语:

   你好,我是Blue. 为资助别人少走弯路而写博客 !!!
  假如本篇文章帮到了你 不妨点个赞吧~ 我会很高兴的 😄 (^ ~ ^) 。想看更多 那就点个关注吧 我会努力带来风趣的内容 😎。
  假如你遇到了标题,本身没法办理,可以私信问我。
  
感谢订阅专栏 三连文章!!

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表