RIP协议的距离向量算法--C++

RIP协议的距离向量算法--C++


#include<fstream>
#include<iostream>
#include<string>
#include<sstream>
#include<list>
using namespace std;

//路由表中数据
class Table{  
public:
string destination_id;
int distance;
string next_stop;
};

//路由类
class Route{                         
public:
Route();
string open_file(ifstream& infile);
void show(list<Table>& p);
void change();
void update();
private:
string route1;
string route2;
list<Table> piece1;
list<Table> piece2;
};

//类中的函数
//构造函数
Route::Route(){
string temp;
Table r_temp;
ifstream infile1,infile2;
istringstream strm;
cout<<"enter the current route name: ";
route1=open_file(infile1);
while(getline(infile1,temp)){
   strm.str(temp);
   strm>>r_temp.destination_id>>r_temp.distance>>r_temp.next_stop;
   piece1.push_back(r_temp);
   strm.clear();
}
cout<<"                this is the currnet route contents "<<endl;
show(piece1);
cout<<"enter the neighbor route name: ";
route2=open_file(infile2);
while(getline(infile2,temp)){
   strm.str(temp);
   strm>>r_temp.destination_id>>r_temp.distance>>r_temp.next_stop;
   piece2.push_back(r_temp);
   strm.clear();
}
cout<<"                this is the neighbor route contents "<<endl;
show(piece2);
}

//打开文件函数
string Route::open_file(ifstream& infile){
    string str;
cin>>str;
infile.open((str+".txt").c_str());
if(!infile){
   cerr<<"file open error!"<<endl;
   exit(1);
}
return str;
}

//显示每条信息
void Route::show(list<Table>& p){           
for(list<Table>::iterator it=p.begin();it!=p.end();it++)
   cout<<(*it).destination_id<<" "<<(*it).distance<<" "
    <<(*it).next_stop<<endl;
}

//改变piece2
void Route::change(){
   for(list<Table>::iterator it=piece2.begin();it!=piece2.end();it++){
    ((*it).distance)++;
    (*it).next_stop=route2;
   }
   cout<<"                this is the changed neighbor route contents "<<endl;
   show(piece2);
}

//更新piece1
void Route::update(){
int count=0;                  //计数器,用来表示id2是否出现与id1中
list<Table>::iterator it1=piece1.begin();
list<Table>::iterator it2=piece2.begin();
for(;it2!=piece2.end();it2++){
   for(it1=piece1.begin();it1!=piece1.end();it1++){
    if((*it1).destination_id==(*it2).destination_id){
     count++;
     if(((*it1).next_stop)==((*it2).next_stop)){
      (*it1).distance=(*it2).distance;
      (*it1).next_stop=(*it2).next_stop;
     }
     if(((*it1).next_stop!=(*it2).next_stop)&&((*it1).distance>(*it2).distance)){
       (*it1).distance=(*it2).distance;
       (*it1).next_stop=(*it2).next_stop;
     }
    }
   }
   if(count==0)
    piece1.push_back(*it2);
   count=0;
}
cout<<"                this is the updated current route contents "<<endl;
show(piece1);
}
int main(){
Route route;
route.change();
route.update();
return 0;
}

然后在同一文件加下添加两个txt文件,可以命名为Rc和Rn。里面分别存入如下信息

Rc:

net1 1 R1
net0 9 R4
net4 5 R4
net5 8 Rn
net6 6 R0
net7 9 R4

Rn:

net2 3 R2
net1 1 R5
net3 6 R2
net4 10 R3
net5 5 R2
net6 6 R1

内容只是用来测试的。运行程序时只需要输入文件名,即Rc,Rn即可。


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用C写的rip协议 这是其中的广播request程序片段: void RouteInit() { int i,optval=0,length,error; routeNum = 0; // init local socket address and ip address GetLocalIP(); // init route table items for(i = 0; i < MAX_NUM; i++) { SetRouteEntry(&routeTable[i].routeInfo,"0.0.0.0",0,0); routeTable[i].isvalid = 0; routeTable[i].timer = 0; routeTable[i].statue = 0; inet_aton("0,0,0,0",&routeTable[i].sourceIPAddr); } // init request packet SetRoutePacket(&reqPacket,REQUEST); SetRouteEntry(&reqPacket.routeEntry[0],"0.0.0.0",0,16); // init response packet SetRoutePacket(&resPacket,RESPONSE); recvSockAddr.sin_family = AF_INET; recvSockAddr.sin_port = htons(PORT); recvSockAddr.sin_addr.s_addr = htonl(INADDR_ANY); sendSockAddr.sin_family = AF_INET; sendSockAddr.sin_port = htons(PORT); // inet_aton("240.255.255.255",&sendSockAddr.sin_addr); sendSockAddr.sin_addr.s_addr = htonl(INADDR_ANY); EntryInit(); sock = socket(AF_INET,SOCK_DGRAM,0); if(sock<0) { printf("cannot create a socket!\n"); exit(1); } if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&optval,sizeof(int)) != 0) { printf("cannot broadcast!\n"); close(sock); exit(1); } if(bind(sock,(struct sockaddr*)&recvSockAddr,sizeof(recvSockAddr))<0) { printf("cannot bind to port\n"); close(sock); exit(1); } length=sizeof recvSockAddr; getsockname(sock,(struct sockaddr*)&recvSockAddr,&length); printf("Port %d is opened. Listen for packet...\n",ntohs(recvSockAddr.sin_port)); FD_ZERO(&fdSet); FD_SET(sock,&fdSet); error = sendto(sock,&reqPacket,4+sizeof(struct ROUTE_ENTRY),0,(struct sockaddr*)(&sendSockAddr),sizeof(struct sockaddr)); if(error<0) { PrintEntry(&reqPacket.routeEntry[0]); printf("broadcast request packet failed! %d,%d,%d\n",error,sock,fdSet); } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值