4、Ns3的实例 first.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

*/

//在编译过程每个ns-3的include文件被放在build目录下一个叫ns3的目录中,防止和include文件名的冲突。ns3/core-module.h与src/core目录下的ns-3模块相对应。当编译时,waf会根据配置在ns-3目录下的公共头文件放到build/debug或者build/optimized目录下。Waf也会自动产生一个模块include文件来加载所有的公共头文件。

#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-3.19/doc/html/index.html页面的搜索看定义,主要是生成日志。直接在浏览器中打开路径:home/tempal/eclipse/ns-allinone-3.19/ns-3.19/doc/html/index.html,可以搜索。

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int

main (int argc, char *argv[])

{

Time::SetResolution (Time::NS);

//将“Udpcho”应用程序的客户端和服务器的日志级别设为“INOF”级。

LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);

LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

//生成网络节点,NodeContainer是ns-3的一个Helper类,可以一次操作多个节点。

NodeContainer nodes;

nodes.Create (2);

//ns-3中将对应的物理实体抽象为网络设备和信道2个概念。下面的语句就实现了网络节点物理连接。其中PointToPointHelper 设置网络设备和信道属性,并通过Install方法把设备安装到节点中。信道和网络设备是对应的,比如以太网和无线信道就不能一起使用。

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));

pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

//设备容器

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes);//会有2个节点,每一个节点在安装了点到点网络设备,在它们之间是一个点到点信道。2个节点会被配置在一个有2ms传输延时的信道上以5Mbit/s的速率传输数据

//为计算机安装协议栈

InternetStackHelper stack;

stack.Install (nodes);

Ipv4AddressHelper address;

address.SetBase ("10.1.1.0", "255.255.255.0");//ns3中地址分配默认是从1开始并单调增长。

Ipv4InterfaceContainer interfaces = address.Assign (devices);

//安装服务器端应用程序、设置端口号

UdpEchoServerHelper echoServer (9);

//这里有个C++隐式转换,以转换以node.Get(1)作为结果输入。将会在管理节点Nodeontainer容器索引号为1的节点上安装一个UdpEchoServerApplication,安装会放回一个容器,这个容器中包含了指向所有被组手创建的应用指针。

ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

//设置客户端的端口为9

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 ();

Simulator::Destroy ();

return 0;

}

运行eclipse,输入first,可以得到以下的结果截图。

clip_image002

posted @ 2014-06-20 11:19  Tempal  阅读(1363)  评论(0编辑  收藏  举报