基于Linux环境下的NS3入门第一例子

来源:互联网 发布:java常见异常类型 编辑:程序博客网 时间:2024/05/18 00:27

安装完基于Linux环境下的NS3后,可以先尝试熟悉其仿真流程,下面以NS3自带example/tutorial/first.cc为第一个例子介绍:

1.首先需要明确NS3仿真环境里几个重要概念:

节点,有NodeContainer类描述,它提供了用于管理计算设备的各种方法;

信道和网络设备:信道指的是数据流流过打媒介,网络设备指的是用于上网打硬件及网卡驱动程序,网络设备、信道。在真实的世界中,这些东西大致相当于网卡和网线。需要说明的是这两样东西紧密的联系在一起而不能够把它们交互地使用(比如以太网设备和无线信道就不能一起使用)。在这个脚本中使用了PointToPointHelper来配置和连接网络设备PointToPointNetDevice和信道PointToPointChannel对象。

应用程序:用Application类来描述。这个类提供了管理仿真过程中用户层应用的各种方法。开发者应当用面向对象的方法自定义和创建新的应用。在本教程中,我们会使用Application类的两个实例:UdpEchoClientApplication和UdpEchoServerApplication。这些应用程序包含了一个client应用和一个server应用来发送和回应仿真网络中的数据包。

其他打协议栈可由InternetStackHelper类描述,IP地址管理可由Ipv4AddresssHelper类和Ipv4InterfaceContaner类描述

2.编辑脚本程序,这里使用的NS3自带例子,所以可以直接进入ns-3.xx/examples/tutorial目录会发现first.cc脚本,可以

vim first.cc
#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;//不用NS3::XXNS_LOG_COMPONENT_DEFINE ("FirstScriptExample");int main (int argc, char *argv[]){LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);NodeContainer nodes;nodes.Create (2);//创建2个node节点PointToPointHelper pointToPoint;//管理NetDevice类和Channel类pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer devices;devices = pointToPoint.Install (nodes);//为2个node节点安装网络设备及分配节点间信道InternetStackHelper stack;stack.Install (nodes);//为2个节点直接加上协议栈如TCP/UDP Ipv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");//分配IPV4地址,从10.1.1.0开始Ipv4InterfaceContainer interfaces = address.Assign (devices);//为两个设备分配地址 UdpEchoServerHelper echoServer (9);//分配服务器端口为9ApplicationContainer 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));ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));Simulator::Run ();//运行Simulator::Destroy (); return 0;}


3.运行脚本程序

要运行自己的脚本,你所需要做的仅仅是把你的脚本放到scratch目录下,通过waf,这样你的脚本就会被编译。

cp examples/tutorial/first.cc scratch/myfirst.cc

现在使用waf命令来编译自己的第一个实例脚本:

sudo ./waf
现在你能够运行这个例子(注意如果你在scratch目录编译了你的程序,你必须在scratch目录外运行它):

.sudo /waf --run scratch/myfirst

注意这里是myfirst而不是myfirst.cc!

最终会看到:

Waf: Entering directory `/home/yan/NS3/ns-allinone-3.25/ns-3.25/build'
Waf: Leaving directory `/home/yan/NS3/ns-allinone-3.25/ns-3.25/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (1.879s)
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9

4.分析仿真结果


1 0
原创粉丝点击