超详细!超完整! Centos7下的NS-3安装与配置总结( 二 )


至此,配置成功 。
4 脚本运行 4.1 终端编译运行
(1)将编写的脚本(和)复制到/root//ns--3.21/ns-3.21/目录下,可以在窗口界面直接复制;然后,进入ns3目录:# cd /root//ns--3.21/ns-3.21
(2)构建(编译):# ./waf
(3)运行脚本
运行脚本:# ./waf --run /first
运行脚本:# ./waf --run /
4.2 编译运行
(1)将编写的脚本(和)复制到/root///目录下,刷新构建该工程 。
(2)右击项目,选择debug as --> debug--> 选择C/C++
修改C/C++ :build//first
然后点击apply应用于该项目 。
(3)打开文件,运行可的如下结果:
5 可视化界面
PyViz是一个用开发的在线ns-3可视化工具,不需要使用trace文件
5.1 PyViz安装
(1)首先需要检查机器上是否有epel仓库,因为会用到里面的一些包 。如果有结果则从(2)开始向下进行,如果无显示则表明本机无epel仓库需要继续执行后续操作 。
检查epel仓库:# rpm -qa | grep epel
安装epel:# yum -yepel-
查看仓库信息:# yum
(2)安装该模块依赖的软件包
# yum-devel gnome- gnome-- gnome--rsvg --kiwi -devel
(红色字体的包我没有在yum,epel源中找到,暂时不知是否会对以后操作有影响,所以暂且搁置 。做完回来发现这三个包即使没有找到未安装,最后的可视化界面也能出来,还是没有搞懂,我还是自己再研究研究吧 。)
# yum
# yum
# yummock
#-s /sbin/
# yumcmake glibc-devel glibc-devel
#
(3)下载pyviz
pyviz下载网址:
# tar -xzvf ns-3-pyviz-.tar.gz
解压后将文件夹放在/root//ns--3.21/ns-3.21/目录下,再重新编译如下命令:
# ./waf clean
# ./waf --build-=debug --- ---tests
(4)用如下命令可以测试是否安装成功:
# ./waf --pyrun src/flow-//wifi-olsr-.py --
若安装成功则会出现下图:
5.使用
(1)修改脚本
首先,到/root//ns--3.21/ns-3.21/找到和;
然后,双击打开这两个脚本,看文件中是否有以下两句,如果没有则添加到两文件的指定位置:
CommandLine cmd;cmd.Parse(argc,argv);
(2)运行脚本显示可视化图像
#./waf --run /first --vis
#./waf --run / --vis
6 总结
拖拖拉拉好长时间终于将NS-3的总体框架搭建成功了~希望对大家的学习有所帮助,有问题可以评论,我会尽力帮大家解决哒~
That is all.
7 附录 7.1
/* -*- 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, MA02111-1307USA*///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"//使用ns3 namespace(无需使用ns3::)using namespace ns3;//定义一个log模块NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");intmain (int argc, char *argv[]){CommandLine cmd;cmd.Parse(argc,argv);Time::SetResolution (Time::NS);//Enable两个log模块,将在收到和发出数据包后输出log消息LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);//NodeContainer:便于我们创建、管理和访问Node对象NodeContainer nodes;nodes.Create (2);//帮助我们配置和连接PointToPointNetDevice和PointToPointChannel//将Device的数据发送速率设为5Mbps//将Channel的时延设为2msPointToPointHelper pointToPoint;pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//使用helper的Install方法在每个节点上创建一个pointToPointNetDevice,//保存在NetDeviceContainer中,并且还创建了一个PointToPointChannelNetDeviceContainer devices;devices = pointToPoint.Install (nodes);//在两个node上安装协议栈(IP、TCP、UDP等)InternetStackHelper stack;stack.Install (nodes);//管理和分配IPv4地址//在Device上依次绑定IP地址,10.1.1.1开始,直到10.1.1.254,产生网络接口(interface)Ipv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer interfaces = address.Assign (devices);//UdpEchoServerApplication和UdpEchoClientApplication//创建一个工作在端口9的echoServerUdpEchoServerHelper echoServer (9);//使用Install方法将Application安装在#1的Node上//Application在第1秒开始运行,在第10秒停止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 (1));echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));echoClient.SetAttribute ("PacketSize", UintegerValue (1024));//使用Install方法在节点0创建echoClient//第2秒开始、第10秒停止ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));//执行仿真//按照事件的时序,从第0到第10秒执行//使用Destroy清理,资源回收Simulator::Run ();Simulator::Destroy ();return 0;}