NS3仿真 csma和RIPv2(附源码)

本文记录了使用NS3进行的两个网络仿真实验,包括CSMA的性能验证,通过绘制网络吞吐量与数据到达率的关系图,以及RIPv2路由协议的收敛性和恢复能力验证,通过路由表观察其性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

做了两个NS3仿真实验
分别是关于csma和ripv2的
在此记录一下

读者可参考
不可照抄剽窃

如有建议
欢迎交流

1、csma

原理就不做介绍了,网上搜搜就有

实验目的是

  • 验证CSMA的性能
  • 画出网络吞吐量S随网络数据到达率G之间的关系图

直接上代码

// Network topology
//
//                            Lan1
//                      ================
//                      |    |    |    |
//       n0   n1   n2   n3   n4   n5   n6
//       |    |    |    |     
//       ================        
//           Lan0         
// n0是源头,在n6收数据,每个节点抓包

#include <iostream>
#include <fstream>
#include <ns3/flow-monitor-helper.h>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "ns3/ipv4-global-routing-helper.h"

using namespace ns3;
NS_LOG_COMPONENT_DEFINE("CsmaMulticastExample");

int main(int argc, char *argv[])
{
   
   
 LogComponentEnable("CsmaMulticastExample", LOG_LEVEL_INFO);
 Config::SetDefault("ns3::CsmaNetDevice::EncapsulationMode", StringValue("Dix")); //封装
 CommandLine cmd;
 cmd.Parse(argc, argv);

 // 创建节点
 NS_LOG_INFO("Create nodes.");
 NodeContainer c;
 c.Create(7);
 NodeContainer c0 = NodeContainer(c.Get (0), c.Get (1), c.Get (2), c.Get (3));
 NodeContainer c1 = NodeContainer(c.Get (3), c.Get (4), c.Get (5), c.Get (6));
 
 // 网络拓扑
 NS_LOG_INFO("Build Topology.");
 CsmaHelper csma;
 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
 NetDeviceContainer nd0 = csma.Install(c0); 
 NetDeviceContainer nd1 = csma.Install(c1);

 // 协议栈
 NS_LOG_INFO("Add IP Stack.");
 InternetStackHelper internet;
 internet.Install(c);

 // IP地址
 NS_LOG_INFO("Assign IP Addresses.");
 Ipv4AddressHelper ipv4Addr;
 ipv4Addr.SetBase("10.1.1.0", "255.255.255.0");
 ipv4Addr.Assign(nd0);
 ipv4Addr.SetBase("10.1.2.0", "255.255.255.0");
 ipv4Addr.Assign(nd1);
 Ipv4Address multicastSource("10.1.1.1");
 Ipv4Address multicastGroup("225.1.2.4");
 
 // 路由
 NS_LOG_INFO("Configure multicasting.");
 Ipv4StaticRoutingHelper multicast;
 Ptr<Node> multicastRouter = c.Get(3); 
 Ptr<NetDevice> inputIf = nd0.Get(3); 
 NetDeviceContainer outputDevices;
 outputDevices.Add(nd1.Get(0));
 multicast.AddMulticastRoute(multicastRouter, multicastSource, multicastGroup, inputIf, outputDevices);
 Ptr<Node> sender = c.Get(0); 
 Ptr<NetDevice> senderIf = nd0.Get(0);
 multicast.SetDefaultMulticastRoute (sender, senderIf);
 //Ptr<Node> sender1 = c.Get(3);
 //Ptr<NetDevice> senderIf1 = nd1.Get(3);
 //multicast.SetDefaultMulticastRoute (sender, senderIf);

 // 应用
 NS_LOG_INFO("Create Applications.");
 uint16_t multicastPort = 9;
 OnOffHelper onoff("ns3::UdpSocketFactory", Address(InetSocketAddress(multicastGroup, multicastPort)));
 onoff.SetConstantRate(DataRate("255b/s"));
 onoff.SetAttribute("PacketSize", UintegerValue(128));
 ApplicationContainer app = onoff.Install(c0.Get(0));
 app.Start(Seconds (1.));
 app.Stop(Seconds (10.));
 PacketSinkHelper sink("ns3::UdpSocketFactory", 
        InetSocketAddress(Ipv4Address::GetAny(), multicastPort)); 
 ApplicationContainer sinkC = sink.Install(c1.Get(3));
 sinkC.Start(Seconds (1.0));
 sinkC.Stop(Seconds (12.0));

 // 跟踪
 NS_LOG_INFO("Configure Tracing.");
 AsciiTraceHelper ascii;
 csma.EnableAsciiAll(ascii.CreateFileStream("csma.tr"));
 csma.EnablePcapAll("csma", true);
 
 // 启动
 NS_LOG_INFO("Run Simulation.");
 Simulator::Run();
 Simulator::Destroy();
 NS_LOG_INFO("Done.");
}

在跟踪里设置了trace文件
通过trace文件可以计算吞吐量S
修改发包速率不断进行实验

最终应得到类似结果如下
在这里插入图片描述

2、RIPv2

RIP是最基本的路由协议,也不多做介绍了

实验目的是

  • 验证RIP路由协议的收敛性
  • 验证RIP路由协议的路由恢复能力

同样直接上代码

// 网络拓扑
//        SRC
//         |<=== source network
//         A------C
//        /  \    |
//       /    \   |
//      B      \  |
//       \      \ |
//        \      \|
//         E------D
//         |<=== target network
//        DST
//
//
//
// BE之间 cost 10,其他 cost 1
// ABCDE是 RIP router,src 和 dst交换数据包
// 程序运行190s,100s时断开DE

#include <fstream>
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-routing-table-entry.h"

using namespace ns3;
NS_LOG_COMPONENT_DEFINE("RipRouting");

// 打断路由连接的函数
void TearDownLink(Ptr<Node> nodeA, Ptr<Node> nodeB, uint32_t interfaceA, uint32_t interfaceB)
{
   
   
 nodeA->GetObject<Ipv4>()->SetDown
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值