1) 將編寫的腳本復制到ns-3.27/scratch目錄下(可以在ubuntu窗口界面直接復制)
進入ns3目錄: /ns-3.27
$ cp examples/tutorial/first.cc scratch/myfirst.cc將腳本復制到scratch目錄下
2) 構建(編譯)
$ ./waf
3) 運行
$ ./waf --run scratch/myfirst
(可能會有運行權限問題,可在root下運行)
ns3中first.cc例子注釋
-
//first.cc: 2個節點間的點到點通信
-
-
//頭文件包含
-
-
-
-
-
-
-
//ns3命名空間
-
using namespace ns3;
-
-
//日志定義
-
NS_LOG_COMPONENT_DEFINE ( "FirstScriptExample");
-
-
//主函數
-
int
-
main (int argc, char *argv[])
-
{
-
//時間分辨率
-
Time::SetResolution (Time::NS);
-
//使日志組件生效:組件--UdpEchoClientApplication--級別INFO
-
LogComponentEnable ( "UdpEchoClientApplication", LOG_LEVEL_INFO);
-
LogComponentEnable ( "UdpEchoServerApplication", LOG_LEVEL_INFO);
-
-
//********生成網絡節點********
-
// 可以簡單看成部分掏空通信部分內容的計算機,可以加入協議棧、應用及外設的網卡等。
-
//節點容器類(包含許多方法):是一個helper幫助類,能夠一次操作多個節點。
-
//如:利用其對象(變量)作為設備helper類對象的參數,可以一次在安裝設備到多個節點。
-
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);//為每個節點安裝協議棧,IP層
-
//ipv4地址幫助類,屬於拓撲幫助類
-
Ipv4AddressHelper address;
-
address.SetBase ( "10.1.1.0", "255.255.255.0");
-
//ipv4接口容器類,為了以后引用方便,給網絡設備容器類配置地址。結果存在ipv4接口容器類的對象中。
-
Ipv4InterfaceContainer interfaces = address.Assign (devices);
-
-
//********安裝應用層********//
-
// UDP服務器設置
-
UdpEchoServerHelper echoServer (9);
-
-
ApplicationContainer serverApps = echoServer.Install (nodes.Get ( 1));
-
serverApps.Start (Seconds ( 1.0));
-
serverApps.Stop (Seconds ( 10.0));
-
// UDP客戶機
-
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));
-
//沒有加入Simulation::Stop(Seconds(11.0));仿真結束時間
-
//因為first.cc例子的事件隊列中事件會自動操作完;
-
//對於一直有事件產生的仿真(類似操作系統),必須設置仿真結束時間
-
Simulator::Run ();
-
Simulator::Destroy ();
-
return 0;
-
}
二、CommandLine命令行參數
仿真一般是為了收集各種不同條件下的數據,常常需要改變一些變量。NS-3提供了Command Line參數接口,可以在運行時對腳本中的變量進行設置,免去了每次更改變量后要重新編譯和構建腳本的麻煩。
1) 修改已有屬性變量
在腳本中添加語句
int main (int argc, char *argv[])
{
...
CommandLine cmd;
cmd.Parse (argc, argv);//將命令行輸入的參數作為類CommandLine的參數進行分析
...
}
這樣可以在shell中使用某些附加參數如PrintHelp:
$~/ns-3.2.1 > ./waf --run "scratch/example --PrintHelp"
這條命令將會列出example當前可用的命令參數:
Entering directory '/home/craigdo/repos/ns-3-dev/build'
Compilation finished successfully
--PrintHelp: Print this help message.
--PrintGroups: Print the list of groups.
--PrintTypeIds: Print all TypeIds.
--PrintGroup=[group]: Print all TypeIds of group.
--PrintAttributes=[typeid]: Print all attributes of typeid.
--PrintGlobals: Print the list of globals.
從輸出中(倒數第二行)我們知道可以打印某些類的屬性:
$~/ns-3.2.1 > ./waf --run "scratch/example --PrintAttributes=ns3::PointToPointNetDevice"
這條命令將會列出類型為PointToPointNetDevice的設備的屬性:
--ns3::PointToPointNetDevice::DataRate=[32768bps]:
The default data rate for point topoint links
知道了屬性名稱,我們也可以使用命令更改這個屬性:前提須把腳本中賦值語句注釋/刪除
$~/ns-3.2.1>./waf--run"scratch/example --ns3::PointToPointNetDevice::DataRate=5Mbps"
2) 添加自己的變量
使用CommandLine::AddValue添加自己的變量,通過鈎掛自己的變量將其與命令行相關聯,使之成為CommandLine可以使用的參數,
在腳本中main函數開始添加
CommandLinecmd;
cmd.AddValue("nPackets","Number of packets to echo", nPackets); //(屬性名稱,屬性說明,變量)
cmd.Parse(argc,argv);
這樣在shell中我們可以在命令中更改這個屬性:
$~/ns-3.2.1 > ./waf --run "scratch/example --nPackets=2"