模拟网桥功能

1、写一个程序来模拟网桥功能。
模拟实现网桥的转发功能,从文件中读取帧模拟网桥从网络中收到一帧,即从两个文件中读入一系列帧,从第一个文件中读入一帧然后从第二个文件中再读入一帧,如此下去。对每一帧,显示网桥是否会转发,及显示转发表内容。
要求:Windows或Linux环境下运行,程序应在单机上运行。
分析:用程序模拟网桥功能,可以假定用两个文件分别代表两个网段上的网络帧数据。而两个文件中的数据应具有帧的特征,即有目的地址,源地址和帧内数据。程序交替读入帧的数据,就相当于网桥从网段中得到帧数据。
对于网桥来说,能否转发帧在于把接收到的帧与网桥中的转发表相比较。判断目的地址后才决定是否转发。由此可见转发的关键在于构造转发表。这里转发表可通过动态生成。


程序流程图如下:
在这里插入图片描述


文件内容格式如下:
在这里插入图片描述
源代码如下:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct ForwardItem    //转发项
{
	char mac[15];       //mac地址
	int port;               //端口号
};
struct ForwardTable         //转发表
{
	ForwardItem forwarditem[100];         //转发表中最大可以保存100条记录
	int count;                                        //转发项的条数
};
int main()
{
	ifstream port1, port2;
	port1.open("port11.txt", ios::in);             //打开文件port1.txt
	if (!port1)       //若打开文件port1.txt失败,则退出程序
	{
		cout << "打开文件port11.txt失败" << endl;  
		exit(1);            
	}
	port2.open("port22.txt", ios::in);     //打开文件port2.txt
	if (!port2)     //若打开文件port2.txt失败,则退出程序
	{
		cout << "打开文件port22.txt失败" << endl;
		exit(1);
	}
	int time = 0;           //time等于偶数时,从文件一读取数据;time等于奇数时,从文件二读取数据
	int flag1 = 1, flag2 = 1;  //flag1和flag2用来标记文件是否已被读完
	int port;
	char source[15], destination[15];
	ForwardTable forwardtable;
	forwardtable.count = 0;
	string temp;
	getline(port1, temp);       //读取文件开头的一行文字
	getline(port2, temp);
	while (1)
	{
		if (!(time %2) && flag1 == 1)   //time等于偶数且文件一中还存在未读数据
		{
			if (port1 >> destination >> source )  //从文件一中读取数据
			{
				flag1 = 1;
				port = 1;
			}
			else
			{
				flag1 = 0;
				if (port2 >> destination >> source)
					flag2 = 1;
				else flag2 = 0;
			}
		}
		if ((time % 2) && flag2 == 1)   //time等于奇数且文件一中还存在未读数据
		{
			if (port2 >> destination >> source) //从文件二中读取数据
			{
				flag2 = 1;
				port = 2;

			}
			else
			{
				flag2 = 0;
				if (port1 >> destination >> source)
					flag1 = 1;
				else flag1 = 0;
			}
		}
		time++;
		if (flag1 || flag2)      
		{
			cout << "当前转发表为:" << endl;
			cout << "mac" << "             " << "port" << endl;
			for (int i = 0; i < forwardtable.count; i++)   //打印当前转发表
			{
				cout << forwardtable.forwarditem[i].mac << "\t" << forwardtable.forwarditem[i].port << endl;
			}
			cout << "端口" << port << "收到源mac为" << source << ",目的mac为" << destination << "的帧" << endl;
		
			int i;
			for (i = 0; i < forwardtable.count; i++)    //查找转发表
			{
				if (!strcmp(forwardtable.forwarditem[i].mac, destination))    //查找成功
				{
					cout << "查找转发表成功!" ;
					if (forwardtable.forwarditem[i].port == port)   //源mac和目的mac所在的端口相同
						cout << "源mac和目的mac所在的端口相同,网桥不转发" << endl;
					else
						cout << "源mac和目的mac所在的端口不同,往端口" << forwardtable.forwarditem[i].port << "进行转发" << endl;
					break;
				}
			}
			if (i == forwardtable.count)      //无法找到该转发地址
			{
				cout << "无法找到该转发地址!" ;
				cout << "向所有的端口转发这个帧(除了端口" << port << ")" << endl;
			}
			int count;
			for (count = 0; count < forwardtable.count; count++)    //查看转发表中是否存在源mac项
			{
				if (!strcmp(forwardtable.forwarditem[count].mac, source))
				{
					break;
				}
			}
			if (count == forwardtable.count)  //如果转发表中不存在源mac项,就进行自学习
			{
				cout << "转发表中不存在源mac项,进行自学习" << endl;
				strcpy_s(forwardtable.forwarditem[forwardtable.count].mac, source);
				forwardtable.forwarditem[forwardtable.count].port = port;
				forwardtable.count++;
				cout << "更新后的转发表为:" << endl;
				cout << "mac" << "             " << "port" << endl;
				for (int j = 0; j < forwardtable.count; j++)
				{
					cout << forwardtable.forwarditem[j].mac << "\t" << forwardtable.forwarditem[j].port << endl;
				}
			}
			cout << "----------------------------------------------------------------------" << endl;
		}
		else break;
	}
}

如果对你们有帮助的话,麻烦给个赞哈

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值