ns3入门案例1 first.cc
1、目錄結(jié)構(gòu)
example:1、根文件下自帶示例結(jié)構(gòu),作為良好的參考資源
? ? ? ? ? ? ? ? ? 2、src環(huán)境下中各模塊中example作為資源
build: 編譯后文件以及可執(zhí)行文件
src:各模塊源代碼
2、新代碼運(yùn)行
將新腳本放在scratch文件夾中,該目錄默認(rèn)在waf編譯環(huán)境內(nèi)。可以通過(guò)直接編譯./waf運(yùn)行
first代碼解析
拓?fù)?#xff1a;點(diǎn)對(duì)點(diǎn)網(wǎng)絡(luò)---最簡(jiǎn)單
1 頭文件
腳本通過(guò)各個(gè)模塊提供的API進(jìn)行網(wǎng)絡(luò)模擬,每個(gè)模塊的API放在“模塊名-module.h”下。其中core與network為必須模塊。另外,腳本中使用非ns3庫(kù)中函數(shù),也需要在這一步添加
#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"2 名字空間
將ns3項(xiàng)目與非ns3項(xiàng)目分離。使用標(biāo)準(zhǔn)庫(kù)函數(shù)需要添加std名字空間,如
using namespace ns3;3?NS_LOG_COMPONENT_DEFINE?
腳本使用宏定義打印輔助信息
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");4 int main()函數(shù)
讀取命令行參數(shù)、設(shè)置模擬單元、開(kāi)啟log組件
Time::SetResolution (Time::NS);LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);5、網(wǎng)絡(luò)拓?fù)鋭?chuàng)建
基本設(shè)置節(jié)點(diǎn):Node
ns3中節(jié)點(diǎn)、信道、與節(jié)點(diǎn)中連接信道的網(wǎng)絡(luò)設(shè)備分別對(duì)應(yīng)于Node、Channel、NetDevice三個(gè)類(英文名字可以看出)。其中,信道與網(wǎng)絡(luò)設(shè)備有著與之對(duì)應(yīng)的多個(gè)子類。該網(wǎng)絡(luò)中使用助手類構(gòu)建網(wǎng)絡(luò)(--Helper)。代碼設(shè)置節(jié)點(diǎn)。設(shè)置PPP【點(diǎn)對(duì)點(diǎn)】的屬性,并通過(guò)Install()在節(jié)點(diǎn)中安裝設(shè)備。Install()返回NetDevice對(duì)象。
NodeContainer nodes;nodes.Create (2);PointToPointHelper pointToPoint;pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer devices;devices = pointToPoint.Install (nodes);6、安裝TCP/IP協(xié)議族
安裝協(xié)議,其中包括:TCP和UDP。網(wǎng)絡(luò)層的IP與路由協(xié)議
InternetStackHelper stack;stack.Install (nodes);Ipv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer interfaces = address.Assign (devices);通過(guò)InternetStackHelper助手安裝協(xié)議棧stack,并通過(guò)Intall(nodes)安裝在節(jié)點(diǎn)中
IP地址通過(guò)AddressHelper設(shè)置并通過(guò)Assign()函數(shù)安裝在節(jié)點(diǎn)中設(shè)備中。
7 安裝應(yīng)用程序
可以有不同的應(yīng)用程序協(xié)議分發(fā)模塊,first采用UdpEcho應(yīng)用程序。
利用服務(wù)器助手,初始化監(jiān)聽(tīng)9號(hào)端口。利用服務(wù)器助手將其安裝在其中的一個(gè)節(jié)點(diǎn)中,編號(hào)為1,同樣使用Install()函數(shù),并設(shè)置在1s后啟動(dòng),10s后結(jié)束。
UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));?客戶端助手接收來(lái)自1編號(hào)的IP地址,并從9號(hào)端口接收。MaxPackets、Interval、PacketSize分別對(duì)應(yīng)echoClient的三個(gè)屬性。為最大發(fā)送分組數(shù)、發(fā)送間隔與發(fā)送包裹大小(負(fù)載)。客戶端安裝在結(jié)點(diǎn)0中,在2s時(shí)開(kāi)始,10s時(shí)結(jié)束。
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));?8、數(shù)據(jù)生成
first未涉及
9、啟動(dòng)與結(jié)束
Simulator::Run ();Simulator::Destroy ();return 0;執(zhí)行之前定義的操作。Run()按順序執(zhí)行;Destroy()執(zhí)行清除操作。
?
以上為first中所有操作,對(duì)于整體ns3把握還不完善。需要理解過(guò)程后進(jìn)一步通過(guò)案例分析。
總結(jié)
以上是生活随笔為你收集整理的ns3入门案例1 first.cc的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Sublime Text提示Unable
- 下一篇: ns3 入门案例2:third.cc