基于Linux环境下的NS3入门第一例子
2016-11-01 16:58
232 查看
安装完基于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脚本,可以
3.运行脚本程序
要运行自己的脚本,你所需要做的仅仅是把你的脚本放到scratch目录下,通过waf,这样你的脚本就会被编译。
现在使用waf命令来编译自己的第一个实例脚本:
注意这里是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.首先需要明确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::XX NS_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);//分配服务器端口为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 (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.分析仿真结果
相关文章推荐
- php的入门第一个例子(包括linux的环境安装与包括页面跳转,以及curl命令的调用)
- Linux下基于eclipse的arm开发环境的建立
- 基于Mapserver的WebGIS下Java环境的配置与Mapserver的JSP例子
- 基于Linux的环境变量的定制 http://www.yuanma.org/data/2006/0803/article_1301.htm
- 关于二级指针的使用测试小例子,仅供测试--参数传递的时候用指针最好,因为压栈的时候指针,只是压入一个地址的值,最多4个字节(32位机上),提供的3个测试例子,基于VC6.0的环境。
- 在进行嵌入式开发之前,首先要建立一个交叉编译环境,这是一套编译器、连接器和libc库等组成的开发环境。文章通过一个具体的例子说明了这些嵌入式交叉编译开发工具的制作过程。 随着消费类电子产品的大量开发和应用和Linux操作系统的不断健壮和强大,嵌入式系统越来
- 基于solaris9平台手工建库(Linux环境下)
- Ubuntu9.10下基于eclipse搭建arm-linux交叉编译环境
- Symbian OS开发入门(一) :基于VS.Net 2003的Symbian开发环境的准备
- 基于Linux的环境变量的定制
- 基于VMware环境Linux服务器集群方案--LVS+Keepalived (1)
- linux脚本编程快速入门-例子+基本语法
- 搭建基于Linux的Informix数据库开发环境
- Linux环境下防火墙配置初级入门 (1)
- 嵌入式Linux实战篇--交叉编译环境的建立(基于ubuntu 8.10)
- Symbian OS开发入门(一) :基于VS.Net 2003的Symbian开发环境的准备
- 搭建基于Linux 具有高可用性的集群环境
- Silverlight入门教程(基于Asp.net运行环境示例)[示例代码下载]
- 基于ARM-linux环境下的音频系统开发
- 基于VMware环境Linux服务器集群方案--LVS+Keepalived (2)