ns-3 third.cc

本文档详细介绍了如何使用ns-3模拟工具创建一个包含PPP、CSMA和Wi-Fi网络的混合场景。从引入必要的头文件、命名空间、定义日志组件开始,逐步构建网络拓扑,包括PPP的点对点连接、CSMA的多节点网络以及Wi-Fi网络的配置,如Channel、WifiPhy、WifiMac设置,并安装TCP/IP协议族、应用程序和路由。最后,讨论了数据追踪和模拟的启动与结束。
摘要由CSDN通过智能技术生成

示例脚本:【examples/tutorial/third.cc】

用ns-3脚本搭建一个Wi-Fi无线网络

模拟了一个混合场景,包含PPP,CSMA和Wi-Fi
在这里插入图片描述

1. 头文件

头文件命名规范:"<模块名>-module.h"
头文件所在目录:【build/ns3】

#include "ns3/core-module.h"			// 定义了ns-3的核心功能(如模拟事件、事件调度),必须包括
#include "ns3/point-to-point-module.h"	// PPP模块
#include "ns3/network-module.h"			// 定义了ns-3的基本网络组件(如网络结点、分组和地址等),必须包括
#include "ns3/applications-module.h"	// 定义了应用层的分组收发模型(如贪婪模型、ON/OFF模型等)
#include "ns3/wifi-module.h"			// Wi-Fi模块
#include "ns3/mobility-module.h"		// 移动模块
#include "ns3/csma-module.h"			// CSMA模块
#include "ns3/internet-module.h"		// 定义了TCP/IP协议栈

2. 命名空间

using namespace ns3;

3. NS_LOG_COMPONENT_DEFINE

下面这行代码的作用是允许在该脚本中使用Log系统中的宏定义打印辅助信息,如打印调试信息的NS_LOG_DEBUG和打印错误信息的NS_LOG_ERROR宏等

NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");

尽管这是一个可选步骤,但建议添加,因为这些辅助信息对调试代码和了解模拟流程有用

4. main()函数中的准备工作

从这一步开始,后续操作均在main()函数中完成

  bool verbose = true;	// Tell echo applications to log if true
  uint32_t nCsma = 3;	// Number of "extra" CSMA nodes/devices
  uint32_t nWifi = 3;	// Number of wifi STA devices / 无线结点数量
  bool tracing = false;	// Enable pcap tracing

  // 读取命令行参数
  CommandLine cmd;
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
  cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
  cmd.AddValue ("tracing", "Enable pcap tracing", tracing);

  cmd.Parse (argc,argv);

在这里插入图片描述

  // The underlying restriction of 18 is due to the grid position
  // allocator's configuration; the grid layout will exceed the
  // bounding box if more than 18 nodes are provided.
  if (nWifi > 18)
    {
   
      std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl;
      return 1;
    }

  // 开启Log组件,打印指定Lo
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itsdandy

感谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值