NS3下载安装
VMware安装 Ubuntu
官网NS3的安装教程-依赖包安装
NS3下载与解压
cd
mkdir ### // ###自定义文件名
cd ### //cd到上一步创建的文件夹下
wget http://www.nsnam.org/release/ns-allinone-3.30.tar.bz2 //获取NS3的压缩包并下载
tar xjf ns-allinone-3.30.tar.bz2 //解压
解压完成之后按照下图进行安装
时间较长,漫长的等待
成功后执行如下命令,若出现“hello simulator”,就说明安装成功了!
安装NetAnim
在 ~/ns-3/ns-allinone-3.33/ns-3.33/路径下,执行命令:
cd netanim
make clean
qmake NetAnim.pro
make
使用NetAnim
//在.cc文件头部添加:
#include “ns3/netanim-module.h”
//并在Simulartor::run()这句代码之前添加如下语句:
AnimationInterface anim(“***.xml”);
保存修改的.cc文件
在终端命令行执行以下命令:
kai@kai-vm:~/ns-3/ns-allinone-3.33/ns-3.33$ ./waf – run .cc文件的名字
kai@kai-vm:~/ns-3/ns-allinone-3.33/ns-3.33$ cd ..
kai@kai-vm:~/ns-3/ns-allinone-3.33/$ cd netanim-3.108/
kai@kai-vm:~/ns-3/ns-allinone-3.33/netanim-3.108$ ./NetAnim
然后会弹出NetAnim的界面
即可看到相关动画。
测试用例
在上述路径下的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
*/
#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"
#include "ns3/netanim-module.h"
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1
// point-to-point
//
using namespace ns3;
/**
ns3 命名空间
意味着不用为了使用ns-3的代码在所有的代码前加上ns::作用域操作符
*/
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
//命令行修改nPackets的值
uint32_t nPackets = 1;
CommandLine cmd;
cmd.AddValue("nPackets","Number of Packets to echo",nPackets);
cmd.Parse(argc,argv);
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;//生成网络节点,仿真中代表计算机
nodes.Create (2);
/***
NodeContainer是NS3的一个Helper类,能一次操作多个节点。
PointToPointHelper负责设置网络设备和信道属性,通过install方法把设备安装到节点中。
*/
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//数据速率5Mbit/s
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//每一个被创建的点到点信道的传输延时值
//设备和信道的配置
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;//协议栈,主要是IP层
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
//地址配置
Ipv4InterfaceContainer interfaces = address.Assign (devices);//地址和设备关联起来
UdpEchoServerHelper echoServer (9);//端口9
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (nPackets));
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));
//myfirst.xml文件可以使用NetAnim打开
AnimationInterface anim("myfirst.xml");
//生成Trace文件
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll(ascii.CreateFileStream("myfirst.tr"));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
运行
Ubuntu终端命令行输入: ./waf --run first
结果如下
Ubuntu终端命令行输入:./waf --run "first --nPackets=2"
运行结果如下:
由上述运行结果可以看到,修改了nPackets的值为2之后,两个节点之间发送了两次数据包
查看动画
kai@kai-vm:~/ns-3/ns-allinone-3.33/ns-3.33$ cd ..
kai@kai-vm:~/ns-3/ns-allinone-3.33$ cd netanim-3.108/
kai@kai-vm:~/ns-3/ns-allinone-3.33/netanim-3.108$ ./NetAnim
打开myfirst.xml文件