抽象概念
- Node(节点):相当于现实世界中连接到网络的计算设备。
- Application(应用程序):相当于现实世界中计算机上运行的软件。
- Channel(信道):基本的通信子网。
- Device(网络设备):相当于硬件设备和软件驱动的总和。
日志
NS_LOG_COMPONENT_DEFINE("FirstScriptExample");
创建节点
NodeContainer nodes;
nodes.Crete(2);
此时只是创建了节点,但什么都没有做。
创建连接
初始化PointToPointHelper
创建点到点的连接,使用PointToPointHelper
类,需要使用此类来配置和连接ns3的PointToPointNetDevice
和PointToPointChannel
对象。
PointToPointHelper pointToPoint; // 初始化PointToPoint对象
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps")); // 创建一个Device使用"5Mbps"作为数据速率
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms")); // "2ms"作为每一个被创建的点到点信道传输延时值
创建Device
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
上述代码完成设备和信道的配,每一个节点安装了点到点网络设备,两个设备被配置在了信道上。
安装协议栈
InternetStackHelper stack;
stack.Install(nodes);
当上述代码被执行后,会为每个节点容的节点安装一个网络协议栈(TCP,UDP,IP等)。
配置IP地址
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0"); // 实则IP和子网掩码
IP默认从1开始并单调增长,即第一个分配的地址为10.1.1.1,接着是10.1.1.2。
Ipv4InterfaceContainer interfaces = address.Assign(devices);
上面的代码真正完成了地址配置,使用Ipv4Interface
对象将一个IP地址同一个设备关联起来。
设置UDP回显服务应用
回显服务端
UdpEchoServerHelper echoServer(9); // 声明对象,9是RemotePort
ApplicationContainer serverApps = echoServer.Install(nodes.Get(1)); // 安装到节点上,安装后返回一个容器,容器中包含了指向所有被生成器创建的应用指针
serverApps.Start(Seconds(1.0)); // 在1s时开始
serverApps.Stop(Seconds(10.0)); // 在10s时停止
回显客户端
UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9); // 参数中第一项是RemoteAddress,第二项是RemotePort,这两个参数必须传递
echoClient.SetAttribute("MaxPackets", UintegerValue(1)); // 模拟期间允许发送的最大数据包个数
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.))); // 两个数据包之间等待的时间
echoClient.SetAttribute("PacketSize", UintegerValue(1024)); // 告诉客户端它的数据包应该承载多少数据
ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0)); // 在服务端生效后1s后生效
clientApps.Stop(Seconds(10.0));
运行模拟器
使用全局函数Simulator::Run
来运行。
前面的
serverApps.Start(Seconds(1.0));
···
clientApps.Stop(Seconds(10.0));
都是预设时间,只有当Simulator::Run
被调用时,系统才会开始遍历预设事件执行。
销毁对象
使用Simulator::Destroy
来完成。
全部代码
#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;
int main(int argc, char *argv[]) {
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
UdpEchoServerHelper echoServer (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(5));
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;
}
可视化模拟
使用NetAnim
首先要引入头文件
#include "ns3/netanim-module.h" // 可视化
然后在代码中添加如下代码
AnimationInterface::SetConstantPosition (nodes.Get (1), 10, 30); //位置
AnimationInterface::SetNodeDescription (wifiApNode, "AP"); // Optional名字
AnimationInterface::SetNodeColor (csmaNodes, 0, 0, 255); // Optional节点颜色
AnimationInterface::SetBoundary (0, 0, 35, 35); // Optional范围
AnimationInterface anim ("wireless-animation.xml"); // Mandatory名字
anim.EnablePacketMetadata (); // Optional
anim.EnableIpv4RouteTracking ("routingtable-wireless.xml", Seconds (0), Seconds (5), Seconds (0.25)); //Optional
之后进入home/tarballs/ns-allinone-3.36.1l/netanim-3.108
目录,使用./NetAnim
打开可视化软件,这时在目录home/tarballs/ns-allinone-3.36.1l/ns-3.36.1
下回生成名为wireless-animation.xml
的文件,使用NetAnim打开此文件即可开始播放动画。