编译
进入ns3源码目录
编译ns3
查看ns3编译版本是debug版还是优化版
(默认执行./waf build,编译生成的二进制程序放在build子目录中)
设置编译版本
export把它做成环境变量
测试安装
运行网络模拟脚本
(类似于hello world程序)
不编译直接运行脚本(–run-no-build)
e:编辑hello-simulator
编辑后重新编译
向模拟脚本中传递命令行参数
另一种写法:
(用$*代替时,会把引号去掉,产生错误)
调试脚本
(默认安装了gdb)
(调试详细内容参考gdb文档)(q退出)
第一个网络模拟脚本(点对点)
通过helper类构建网络拓扑。理解节点、设备、应用概念的抽象。
#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;
NS_LOG_COMPONENT_DEFINE("FirstScriptExample"); //定义日志组件
int
main(int argc, char *argv[])
{
Time::SetResolution(Time::NS);
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); //激活日志等级INFO
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO); //控制日志输出的内容的多少
//构建两个网络节点,利用ptp通信
NodeContainer nodes;
nodes.Create(2); //创建两个空节点
PointToPointHelper pointToPoint; //ppp助手类
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps")); //设置传输速率为5m
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes); //把ptp对应的网络设备,安装到两个节点上去,并且返回给device
InternetStackHelper stack; //安装网络协议(Internet协议)
stack.Install(nodes); //为node安装tcp/ip协议栈
Ipv4AddressHelper address; //分配IP地址
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)); //把echoServer应用程序安装到节点上(服务器端)
serverApps.Start(Seconds(1.0)); //应用程序何时启动,何时停止
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9); //(服务器的IP地址,端口号)服务器节点是1,对应接口也是1
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)); //在模拟启动后2秒向节点1的9号端口发送一个1024比特的UDP数据包,在模拟启动后10秒停止
Simulator::Run(); //开始网络模拟仿真,运行
Simulator::Destroy(); //销毁
return 0;
}
将first.cc文件(在examples/tutorial)复制到scratch文件夹中,运行脚本。
可视化: