没有合适的资源?快使用搜索试试~ 我知道了~
ns-3实例代码注释大全

温馨提示


试读
23页
该文档,为ns-3.2.6中的默认文档/examples/tutorial/目录下的五篇文档注释,非常详细!基本都达到来逐字逐句的注释,很适合初学者学习使用!
资源推荐
资源详情
资源评论

















Scratch-simulator.cc
/* -*- Mode:C++; c-le-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ns3/core-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("ScratchSimulator");
int
main (int argc, char *argv[])
{
NS_LOG_UNCOND ("Scratch Simulator");
Simulator::Run ();
Simulator::Destroy ();
}
rst.cc(仿真的网络脚本简单:在两个节点间创建一个简单的点到点通信)
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or
modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
1307 USA
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");//日志
int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
//以下两行是使两个日志组件生效的,内建在 Echo Client 和 Echo Server 的应用中
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
//创建 ns-3 节点对象,它们在仿真中代表计算机
NodeContainer nodes;//声明一个名为 nodes 的节点容器类
nodes.Create (2); //创建两个节点
//实现网络节点的物理连接
PointToPointHelper pointToPoint; //初始化 PointToPointHelper
的对象
pointToPoint
pointToPoint.SetDeviceAttribute ("DataRate", StringValue
("5Mbps"));//创建一个 PointToPointNetDevice 对象时使用“5Mbit/s”作为数据速率
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//
点到点信道的传输时延为 2ms
//完成设备和信道的配置

NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);//两个设备会被配置在一个有 2ms 传
输时延的信道上以 5Mbit/s 的速率传输数据。
//为计算机安装协议栈
InternetStackHelper stack;// 类 InternetStackHelper 会为每一个节点容器
中的节点安装一个网络协议栈主要是 IP 层。
stack.Install (nodes);
Ipv4AddressHelper address;// Ipv4AddressHelper 为节点上的设备设置 IP 地址
address.SetBase ("10.1.1.0", "255.255.255.0");//设置基 IP 地址和子网掩
码(声明一个地址助手对象,并且告诉它应该从 10.1.1.0 开始以子网为 255.255.255.0 分
配地址。地址分配默认是从 1 开始单调增长,所以在这个基础上第一个分配的是 10.1.1.1,
接着是 10.1.1.2)
Ipv4InterfaceContainer interfaces = address.Assign (devices);//完成
了真正的地址配置。在 ns-3 中使用 Ipv4Interface 对象将一个 IP 地址同一个设备联系起
来。
//安装服务器端应用程序、设置端口号
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get
(1));
//式 echo 服务应用在 1s 时开始生效并在 10s 时停止(失效)
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
//设置客户端应用层
客户端应用的设置与服务器端类似。也有一个 UdpEchoClientHelper 来管理
UdpEchoClientApplication。然而,对于 echo 客户端,需要设置 5 个不同的属性。首先
2 个属性是在 UdpEchoClientHelper 的构建过程中被设置的。按照助手构造函数的格式,
本文把“RemoteAddress”和“RemotePort”属性传递给了助手(实际上是作为助手构造函数
的 2 个必须参数传递的)。
回忆一下使用 Ipv4InterfaceContainer 来追踪配置给设备的 IP 地址。在界面容器中位置
零的界面对象将会和节点容器中位置零的节点对象对应。同样在界面容器中位置一的界面对象
将会和节点容器中位置一的节点对象对应。所以,在上面的第一行代码中,本文创建了一个助
手并告诉它设置客户端的远端地址为服务器节点的 IP 地址。同样告诉它准备发送第二个数据
分组到端口 9。
“MaxPackets”属性告诉客户端所允许它在模拟期间能发送的最大数据分组个数。
“Interval”属性告诉客户端在 2 个数据分组之间要等待多长时间,而“PacketSize”属性告
诉客户端它的数据分组应该承载多少数据。本例中让客户端发送一个 1 024 byte 的数据分组。
正如 echo 服务端一样,告诉 echo 客户端何时来开始一个 1 024 byte 的数据分组。正如
echo 服务端一样,告诉 echo 客户端何时来开始和停止,但是这里本文使客户端在服务端生效
1 s 后才开始(在模拟器中时间 2 s 的时候)。
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);

echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get
(0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
//启动模拟器(这是用全局变量来运行模拟器)
当 Simulator::Run 被调用时,系统会开始遍历预设事件的列表并执行。首先它会在 1.0
s 时运行事件,这个事件会使 echo 服务端应用生效。接下来仿真器会运行在 t=2.0 s 时的事
件,即让 echo 客户端应用开始。同样地,这个事件可能会预定更多的其他事件。在 echo 客
户端应用中,开始事件的执行会通过给服务端传送一个数据分组来开始仿真的数据传送阶段。
发送一个数据分组给服务端会引发一系列更多的事件。这些事件会被预设在此事件之后,并根
据已经在脚本中设定的时间参数来执行数据分组的应答。
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
scend.cc
拓扑结构如图所示。
second.cc 的拓扑结构
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or
modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License

* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
1307 USA
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
int
main (int argc, char *argv[])
{
bool verbose = true;// 定义变量,用于诀定是否开启 2 个 UdpApplication 的
Logging 组件;默认 true 开启
uint32_t nCsma = 3; //LAN 中另有 3 个 node
CommandLine cmd; //命令行
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices",
nCsma);
cmd.AddValue ("verbose", "Tell echo applications to log if true",
verbose);
//命令行参数设置是否开启 logging
cmd.Parse (argc,argv);
if (verbose)
{
剩余22页未读,继续阅读
资源评论

- dake892019-03-30NS-3的例程解析,只有first、second和third三个有注释HuaCode2019-03-30抱歉,当时做项目的时候只是学习并注释了前三篇文章,后面做项目的时候发现前面三篇的作用比较大,后的两篇参考内容不多,就没有进行注释。

HuaCode
- 粉丝: 286
- 资源: 24
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基础算法-python爬楼梯问题
- 某音Web端参数X-Bogus获取算法(逆向分析)
- 头歌答案 C语言程序设计实践 实验二 数据类型与基本操作(1)
- java高校实习生管理系统设计和实现springboot+vue毕业设计源码+数据库mysql代码.rar
- springboot+vue.js辽B代驾管理系统java毕业设计源码+数据库代码.rar
- OceanBase OBCA初级考试认证资料
- java可信捐赠管理系统的设计与开发springboot+vue毕业设计源码+数据库代码.rar
- vue基于Springboot的网上宠物店系统的设计与实现java毕业设计源码+数据库代码.rar
- OceanBase OBCA 部分题目
- vue基于springboot的七彩云南文化旅游网站的设计与实现java毕业设计源码代码+数据库.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
