NS3-第一个脚本

cd ns3/examples/tutorial

打开first.cc

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
//文件中的第一行是EMACS模式线。这告诉Emacs关于我们在源代码中使用的格式约定约定(编码样式)。
/*
 * 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"
//头文件位于build目录

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse (argc, argv);
  
  Time::SetResolution (Time::NS); //将时间分辨率设置为一个纳秒
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);//脚本的接下来的两行用于启用一个内置于echo客户端和echo服务器应用程序中的两个日志记录组件:

  NodeContainer nodes;
  nodes.Create (2);//创建将代表模拟中计算机的NS-3节点对象
//NodeContainer拓扑帮助程序提供了一种可靠的方式来创建,管理和访问我们创建的任何节点对象,以便运行模拟。上面的第一行只是声明我们呼叫节点的NodeContainer。第二行调用节点对象上的“创建方法”,并询问容器创建两个节点。如doxygen中所述,容器呼叫到NS-3系统适用于创建两个节点对象,并在内部存储指向这些对象的指针。

  PointToPointHelper pointToPoint;//实例化,

  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));


  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);//NodeContainer当做参数,执行PointTopoint.install(节点)调用后,我们将有两个节点,每个节点都有一个安装的点对点网络设备和它们之间的单个点对点信道。两个设备将被配置为在具有两个毫秒传输延迟的信道上每秒以每秒的五个兆比特的数据传输数据。

  InternetStackHelper stack;
  stack.Install (nodes);
//我们现在有节点和设备配置,但​​我们的节点上没有任何协议栈。接下来的两行代码将照顾这一点。安装协议栈

  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");//拓扑帮助程序来管理IP地址的分配。唯一的用户可见API是在执行实际地址分配时设置基本IP地址和网络掩码(其在辅助程序内的较低级别)。

  Ipv4InterfaceContainer interfaces = address.Assign (devices);//执行实际的地址分配

//NS-3系统的另一个核心抽象是应用程序。在此脚本中,我们使用两个核心NS-3类应用程序的专业化,称为UDPechOServerApplication和UdpechoclientApplication。正如我们在先前的解释中,我们使用Helper对象来帮助配置和管理底层对象。在这里,我们使用udpechoservelper和udpechoclienthelper对象使我们的生活更容易。
  UdpEchoServerHelper echoServer (9);//使用setAttribute将“端口”属性设置为另一个值
  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));



  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);//服务器的IP地址和端口
//需要设置五个属性
//对于echo客户端,需要设置五个不同属性
  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::Stop (Seconds (11.0));//将在11秒内计划显式停止,但是实际上并不影响,因为该程序会在10s的时候结束
  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

Toplology Helpers

1、NodeContainter

2、PointToPointHelper

3、NetDeviceContainer

4、InternetStackHelper

5、Ipv4AdressHelper

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值