NS-3脚本的编译运行

原文地址:http://hi.baidu.com/czlaner/blog/item/77a0a4ce9903a90493457e06.html

-----------------------------------------------------------------------------------------------------------------

一、环境支持
如上文(NS-3入门[1]概念引入 )所述,编译/运行NS-3脚本需要保证Linux环境的设置(gcc、waf、tcpdump等),详细的必要软件包安装过程参见http://www.nsnam.org/wiki/index.php/Installation

二、NS-3C++脚本的编写
如前所述,NS-3的脚本使用C++语言(也支持python),使用四种类型的网络构件(Node、NetDevice、Channel、Application)。一个简单的脚本一般有以下步骤:
1、创建节点Node(使用类NodeContainer::Create()方法)
2、使用链路Helper类来帮助设置链路(包括PointToPointHelper、CsmaHelper、WifiHelper等类型)。 Helper类虽然不属于上述四类的网络构件,但它却极大地方便了拓扑的搭建,它可以帮助我们处理实际中诸如在两个终端安装网卡、连网线、Modern、 配置上网方式、链路属性等底层工作,简化了仿真过程,使我们可以更专注于仿真的目的
3、安装IP协议栈(使用类InternetStackHelper::Install()方法)
4、设置IP地址(使用类Ipv4AddressHelper::SetBase()/Assign()方法)
5、在节点Node上安装应用程序(目前支持UdpServerServer、UdpEchoClient、PacketSink等)
6、设置仿真时间、启动仿真
===================================
一个简单的脚本(来自NS-3 Tutorial)及其解释
=========================================================================
#include "ns3/core-module.h"
#include "ns3/simulator-module.h
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("Example");   //定义名称为“Example”的日志模块

int
main (int argc, char *argv[])
{
   //以下两个语句启用UdpEcho应用程序的日志记录,其级别为LOG_LEVEL_INFO。关于NS-3的日志系统将在后续篇章进行介绍。
  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);

  NodeContainer nodes;   //1、创建两个节点
  nodes.Create (2);  

  PointToPointHelper pointToPoint;  //2、创建P2P类型的Helper
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));   //使用Helper设置链路属性
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);  //使用Helper将网卡安装到节点

  InternetStackHelper stack;  //3、安装IP协议栈
  stack.Install (nodes);

  Ipv4AddressHelper address;  //4、分配IP地址
  address.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer interfaces = address.Assign (devices);  //分配到网卡


  UdpEchoServerHelper echoServer (9);   //5.1、安装UdpServer应用服务,9表示服务端口
  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));
  serverApps.Start (Seconds (1.0));   //6.1、Server启动时间
  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);   //5.2、安装UdpClient应用服务,需要指明服务器IP以及服务端口
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
  clientApps.Start (Seconds (2.0));   //6.2、Client启动时间
  clientApps.Stop (Seconds (10.0));

  Simulator::Run ();   //6.3、启动仿真
  Simulator::Destroy ();
  return 0;
}
=========================================================================

三、编译与运行
当我们装好NS-3的运行环境之后,在NS-3的程序目录下会有一个scratch目录,其性质类似于VC/VC++环境下的Debug目录。
将上述脚本文件保存为example.cc,复制到scratch下面,然后在NS-3目录下使用命令waf完成编译,然后运行。例如:
      $~/NS-3.2.1 > ./waf
      $~/NS-3.2.1 > ./waf --run scratch/example
可以看到程序输出:
    Entering directory ‘~/NS-3.2.1/build’
    Compilation finished successfully
    Sent 1024 bytes to 10.1.1.2
    Received 1024 bytes from 10.1.1.1
    Received 1024 bytes from 10.1.1.2

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值