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

安装完基于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::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.分析仿真结果


  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值