欢迎来到天天文库
浏览记录
ID:10125248
大小:111.08 KB
页数:6页
时间:2018-06-11
《透明网桥的自学习和转发帧算法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、算法描述如下:自学习:当网桥收到一转发帧时,先查找自己的转发表中是否有源地址,若没有则添加此项。转发帧:查找自己转发表中是否有目的地址,若没有则将此帧从其他端口转发出去。若有,则将转发表中记录的目的地址端口和此帧进入网桥时通过的端口进行比较,若相等则丢弃此帧(因为目的主机已经收到此帧了),若不相等,则将此帧通过转发表记录的目的地址端口转发出去。源码如下:(此代码在突出算法思想的情况下设计的尽量简单,有的情况没有考虑,或者简单处理了。)12#include3usingnamespacestd;4#defineMax_
2、Data100//转发表数据项数量5structData//数据项结构6{7charAdd;//地址8intport;//端口9};10structSendTable//转发表结构11{12Datadata[Max_Data];13intwrite;//写指针,指向下一个要写的位置14}sendTable;15voidinitSendTable()16{17for(inti=0;i3、endTable.write=0;23}24intindex;//记录匹配项25boolFind(Datadata)//查找转发表,若找到返回ture,找不到返回false26{27for(inti=0;i4、.data[sendTable.write].Add=data.Add;6sendTable.data[sendTable.write].port=data.port;7sendTable.write=(sendTable.write+1)%Max_Data;8}9voidOutSendTable()10{11cout<<"********SendTable********"<<"";12for(inti=0;i5、ndTable.data[i].Add<<""<6、7cin>>source>>port;28sourceData.Add=source;29sourceData.port=port;30cout<<"InputdestinationAddress:";31cin>>destination;32destinationData.Add=destination;33destinationData.port=0;//由于目的地址不需要输入端口号,此处将其置034if(!Find(sourceData))//查找转发表,若找不到则将源地址添加如转发表35AddSendTable(sour7、ceData);36if(!Find(destinationData))//查找转发表,若找不到则将此帧从所有其他端口发送给别的网桥37cout<<"SendthisDatatootherbridgethroughotherport";38else39{40if(sendTable.data[index].port==sourceData.port)//若收到此帧的端口和目的地址再转发表存储的端口相同,说明源地址和目的地址处在同一网段内,目的主机已经收到此帧,须将其丢弃41cout<<"ThisDataalreadyreceiv8、ed,sogiveitup.";42else//若端口不同,则通过查找到的端口将此帧发出43cout<<"SendthisDatathroughport"<
3、endTable.write=0;23}24intindex;//记录匹配项25boolFind(Datadata)//查找转发表,若找到返回ture,找不到返回false26{27for(inti=0;i4、.data[sendTable.write].Add=data.Add;6sendTable.data[sendTable.write].port=data.port;7sendTable.write=(sendTable.write+1)%Max_Data;8}9voidOutSendTable()10{11cout<<"********SendTable********"<<"";12for(inti=0;i5、ndTable.data[i].Add<<""<6、7cin>>source>>port;28sourceData.Add=source;29sourceData.port=port;30cout<<"InputdestinationAddress:";31cin>>destination;32destinationData.Add=destination;33destinationData.port=0;//由于目的地址不需要输入端口号,此处将其置034if(!Find(sourceData))//查找转发表,若找不到则将源地址添加如转发表35AddSendTable(sour7、ceData);36if(!Find(destinationData))//查找转发表,若找不到则将此帧从所有其他端口发送给别的网桥37cout<<"SendthisDatatootherbridgethroughotherport";38else39{40if(sendTable.data[index].port==sourceData.port)//若收到此帧的端口和目的地址再转发表存储的端口相同,说明源地址和目的地址处在同一网段内,目的主机已经收到此帧,须将其丢弃41cout<<"ThisDataalreadyreceiv8、ed,sogiveitup.";42else//若端口不同,则通过查找到的端口将此帧发出43cout<<"SendthisDatathroughport"<
4、.data[sendTable.write].Add=data.Add;6sendTable.data[sendTable.write].port=data.port;7sendTable.write=(sendTable.write+1)%Max_Data;8}9voidOutSendTable()10{11cout<<"********SendTable********"<<"";12for(inti=0;i5、ndTable.data[i].Add<<""<6、7cin>>source>>port;28sourceData.Add=source;29sourceData.port=port;30cout<<"InputdestinationAddress:";31cin>>destination;32destinationData.Add=destination;33destinationData.port=0;//由于目的地址不需要输入端口号,此处将其置034if(!Find(sourceData))//查找转发表,若找不到则将源地址添加如转发表35AddSendTable(sour7、ceData);36if(!Find(destinationData))//查找转发表,若找不到则将此帧从所有其他端口发送给别的网桥37cout<<"SendthisDatatootherbridgethroughotherport";38else39{40if(sendTable.data[index].port==sourceData.port)//若收到此帧的端口和目的地址再转发表存储的端口相同,说明源地址和目的地址处在同一网段内,目的主机已经收到此帧,须将其丢弃41cout<<"ThisDataalreadyreceiv8、ed,sogiveitup.";42else//若端口不同,则通过查找到的端口将此帧发出43cout<<"SendthisDatathroughport"<
5、ndTable.data[i].Add<<""<6、7cin>>source>>port;28sourceData.Add=source;29sourceData.port=port;30cout<<"InputdestinationAddress:";31cin>>destination;32destinationData.Add=destination;33destinationData.port=0;//由于目的地址不需要输入端口号,此处将其置034if(!Find(sourceData))//查找转发表,若找不到则将源地址添加如转发表35AddSendTable(sour7、ceData);36if(!Find(destinationData))//查找转发表,若找不到则将此帧从所有其他端口发送给别的网桥37cout<<"SendthisDatatootherbridgethroughotherport";38else39{40if(sendTable.data[index].port==sourceData.port)//若收到此帧的端口和目的地址再转发表存储的端口相同,说明源地址和目的地址处在同一网段内,目的主机已经收到此帧,须将其丢弃41cout<<"ThisDataalreadyreceiv8、ed,sogiveitup.";42else//若端口不同,则通过查找到的端口将此帧发出43cout<<"SendthisDatathroughport"<
6、7cin>>source>>port;28sourceData.Add=source;29sourceData.port=port;30cout<<"InputdestinationAddress:";31cin>>destination;32destinationData.Add=destination;33destinationData.port=0;//由于目的地址不需要输入端口号,此处将其置034if(!Find(sourceData))//查找转发表,若找不到则将源地址添加如转发表35AddSendTable(sour
7、ceData);36if(!Find(destinationData))//查找转发表,若找不到则将此帧从所有其他端口发送给别的网桥37cout<<"SendthisDatatootherbridgethroughotherport";38else39{40if(sendTable.data[index].port==sourceData.port)//若收到此帧的端口和目的地址再转发表存储的端口相同,说明源地址和目的地址处在同一网段内,目的主机已经收到此帧,须将其丢弃41cout<<"ThisDataalreadyreceiv
8、ed,sogiveitup.";42else//若端口不同,则通过查找到的端口将此帧发出43cout<<"SendthisDatathroughport"<
此文档下载收益归作者所有