内部网关协议RIP 距离向量算法
来源:互联网 发布:nginx 无reload 编辑:程序博客网 时间:2024/05/16 03:39
网络拓扑结构:
A、B、C、D为路由器,1、2、3、4、5为网络
采用邻接表
胡乱一写,代码相当冗余,也没对某些方法进行封装
采用邻接表
胡乱一写,代码相当冗余,也没对某些方法进行封装
#include "stdio.h"#include<windows.h>typedef struct RouNode{ char router; struct RouNode *nextrou;} RouNode;void main(){ //声明并初始化邻接表 RouNode rounode[4]; RouNode *next; RouNode *A1 = new RouNode(); RouNode *A2 = new RouNode(); RouNode *A3 = new RouNode(); RouNode *B1 = new RouNode(); RouNode *B2 = new RouNode(); RouNode *C1 = new RouNode(); RouNode *C2 = new RouNode(); RouNode *C3 = new RouNode(); RouNode *D1 = new RouNode(); RouNode *D2 = new RouNode(); rounode[0].router = 'A'; rounode[0].nextrou = B1; B1->nextrou = D1; B1->router = 'B'; D1->router = 'D'; D1->nextrou = C3; C3->router = 'C'; C3->nextrou = NULL; rounode[1].router = 'B'; rounode[1].nextrou = A1; A1->router = 'A'; A1->nextrou = C1; C1->router = 'C'; C1->nextrou = NULL; rounode[2].router = 'C'; rounode[2].nextrou = B2; B2->router = 'B'; B2->nextrou = D2; D2->router = 'D'; D2->nextrou = A3; A3->router = 'A'; A3->nextrou = NULL; rounode[3].router = 'D'; rounode[3].nextrou = A2; A2->router = 'A'; A2->nextrou = C2; C2->router = 'C'; C2->nextrou = NULL; int A[6][3] = {3,1,'*',1,1,'*',2,1,'*',0,0,0,0,0,0,0,0,0}; int B[6][3] = {3,1,'*',5,1,'*',0,0,0,0,0,0,0,0,0,0,0,0}; int C[6][3] = {5,1,'*',4,1,'*',2,1,'*',0,0,0,0,0,0,0,0,0}; int D[6][3] = {1,1,'*',4,1,'*',0,0,0,0,0,0,0,0,0,0,0,0}; int x[6][3] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int y[6][3] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; while(1) { for(int i = 0; i < 4; i++) { if(rounode[i].router == 'A') { for(int m = 0; m < 6; m++) { x[m][0] = A[m][0];x[m][1] = A[m][1];x[m][2] = A[m][2]; } } if(rounode[i].router == 'B') { for(int m = 0; m < 6; m++) { x[m][0] = B[m][0];x[m][1] = B[m][1];x[m][2] = B[m][2]; } } if(rounode[i].router == 'C') { for(int m = 0; m < 6; m++) { x[m][0] = C[m][0];x[m][1] = C[m][1];x[m][2] = C[m][2]; } } if(rounode[i].router == 'D') { for(int m = 0; m < 6; m++) { x[m][0] = D[m][0];x[m][1] = D[m][1];x[m][2] = D[m][2]; } } // printf("%c\n",rounode[i].router); next = rounode[i].nextrou; while(next) { if(next->router == 'A') { for(int m = 0; m < 6; m++) { y[m][0] = A[m][0];y[m][1] = A[m][1];y[m][2] = A[m][2]; } } if(next->router == 'B') { for(int m = 0; m < 6; m++) { y[m][0] = B[m][0];y[m][1] = B[m][1];y[m][2] = B[m][2]; } } if(next->router == 'C') { for(int m = 0; m < 6; m++) { y[m][0] = C[m][0];y[m][1] = C[m][1];y[m][2] = C[m][2]; } } if(next->router == 'D') { for(int m = 0; m < 6; m++) { y[m][0] = D[m][0];y[m][1] = D[m][1];y[m][2] = D[m][2]; } } //邻居的路由表给自己,并更新 int m = 0; int location = 0; while(y[m][0] != 0) { int n = 0;bool have = false; // printf("y中%d\n",y[m][0]); while(x[n][0] != 0) { // printf("x中%d\n",x[n][0]); if(x[n][0] == y[m][0]) { have = true; location = n; } n++; } if(!have) { //路由器中不存在此记录 x[n][0] = y[m][0]; x[n][1] = y[m][1] + 1; x[n][2] = next->router; // printf("添加成功\n"); } else { //路由器中有此记录,判断下一跳是否为邻居 if(x[location][2] == next->router) { //下一跳为邻居 x[location][1] = y[m][1] + 1; } else { //下一跳不为邻居,判断距离 if(x[location][1] > y[m][1] + 1) { x[location][1] = y[m][1] + 1; x[location][2] = next->router; } } } m++; } next = next->nextrou; //转换之后,将x赋给A、B、C、D if(rounode[i].router == 'A') { for(int m = 0; m < 6; m++) { for(int n = 0; n < 3; n++) { A[m][n] = x[m][n]; } } } if(rounode[i].router == 'B') { for(int m = 0; m < 6; m++) { for(int n = 0; n < 3; n++) { B[m][n] = x[m][n]; } } } if(rounode[i].router == 'C') { for(int m = 0; m < 6; m++) { for(int n = 0; n < 3; n++) { C[m][n] = x[m][n]; } } } if(rounode[i].router == 'D') { for(int m = 0; m < 6; m++) { for(int n = 0; n < 3; n++) { D[m][n] = x[m][n]; } } } } } int t = 0; int n1 = 0,n2 = 0,n3 = 0,n4 = 0; printf("----------------\n路由器A的路由表:\n"); t = 0; while(C[t][0] != 0) { printf("---%d---%d---%c---\n",A[t][0],A[t][1],A[t][2]); t++;n1++; } printf("----------------\n路由器B的路由表:\n"); t = 0; while(C[t][0] != 0) { printf("---%d---%d---%c---\n",B[t][0],B[t][1],B[t][2]); t++;n2++; } printf("----------------\n路由器C的路由表:\n"); t = 0; while(C[t][0] != 0) { printf("---%d---%d---%c---\n",C[t][0],C[t][1],C[t][2]); t++;n3++; } printf("----------------\n路由器D的路由表:\n"); t = 0; while(C[t][0] != 0) { printf("---%d---%d---%c---\n",D[t][0],D[t][1],D[t][2]); t++;n4++; } if(n1==n2 && n2==n3 && n3==n4 && n4==5) { break; } Sleep(2000); }}
- 内部网关协议RIP 距离向量算法
- 内部网关协议RIP
- 内部网关协议RIP
- RIP协议的距离向量算法--C++
- RIP协议与向量距离算法
- RIP距离向量算法
- RIP距离向量算法
- 网络层--内部网关协议RIP
- 计算机网络(六) 网关--内部网关协议RIP和OSPF
- 介绍Rip协议和OSPF协议(内部网关协议)
- 计算机网络笔记---网络层---内部网关协议RIP
- (others)RIP内部网关协议下的路由表
- python实现基于向量算法的的路由协议(RIP)
- IP 路由 内部网关协议 常见的三种协议RIP、OSPF、EIGRP学习认识
- 计算机网络(谢)——第三章读书笔记4.5(内部网关协议RIP)
- 内部网关协议OSPF
- (OSPF)内部网关协议
- 动态路由距离矢量路由协议RIP
- 如何检测浏览器是否支持html5或者其部分元素
- oracle 10g控制文件存储信息
- 引入IT服务管理工具对于高校信息化建议的意义
- android 处理图片的工具类
- 修改 SQL Server 登录模式 设置 SQL Server 服务器
- 内部网关协议RIP 距离向量算法
- NASA提供的中国区域TM影像下载
- To read list
- Linux网络体系结构
- android 动画研究
- 布隆过滤器在网页去重中的应用
- 有点意思....有空常看看
- 个人数据库与文件数据库在SQL WHERE子句写法的区别
- 透明网桥的自学习算法