日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ns3 入门案例2:third.cc

發布時間:2023/12/2 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ns3 入门案例2:third.cc 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼分析

1 頭文件

#include "ns3/core-module.h" #include "ns3/point-to-point-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/mobility-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/yans-wifi-helper.h" #include "ns3/ssid.h"

2 名字空間

3 打印內容

NS_LOG_COMPONENT_DEFINE (“ThirdScriptExample”);

4 主函數(變量聲明)

與案例1不同,此處定義并使用一些命令行參數。

bool verbose = true;uint32_t nCsma = 3;uint32_t nWifi = 3;bool tracing = false;CommandLine cmd;cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);cmd.AddValue ("tracing", "Enable pcap tracing", tracing); `

以nWifi為例,意義為默認無線節點數目,設置大小為3,但可以通過命令行傳入參數
若運行時采用以下方式,則把參數傳進里面。

./waf --run "third --nWifi=18"

5 創建網絡拓撲

(1)案例1涉及PPP網絡,【設置鏈路速率、時延】代碼如下

NodeContainer p2pNodes;p2pNodes.Create (2);PointToPointHelper pointToPoint;pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer p2pDevices;p2pDevices = pointToPoint.Install (p2pNodes);

(2)CSMA網絡
CSMA可以連接多個節點,節點之間競爭使用信道
(PPP連接兩個節點,專屬信道)

NodeContainer csmaNodes;csmaNodes.Add (p2pNodes.Get (1));//此處為雙接口節點csmaNodes.Create (nCsma);CsmaHelper csma;csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));NetDeviceContainer csmaDevices;csmaDevices = csma.Install (csmaNodes);

(3)Wifi網絡
由一個接入點(AP)與nWiFi個移動節點組成,AP為雙模節點,安裝WiFi與PPP兩個設備。WiFi協議包括鏈路層(WiFIMac)與物理層(WifiPhy)。WifiNetDevice只起到連接上下層協議的作用。
1)

NodeContainer wifiStaNodes;wifiStaNodes.Create (nWifi);NodeContainer wifiApNode = p2pNodes.Get (0);//雙模節點 //默認模型YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();//配置Channel類YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();//配置wifiPhy類phy.SetChannel (channel.Create ());

設置Channel和WifiPhy
2)

WifiHelper wifi;wifi.SetRemoteStationManager ("ns3::AarfWifiManager");WifiMacHelper mac;Ssid ssid = Ssid ("ns-3-ssid");mac.SetType ("ns3::StaWifiMac",//移動節點"Ssid", SsidValue (ssid),"ActiveProbing", BooleanValue (false));NetDeviceContainer staDevices;staDevices = wifi.Install (phy, mac, wifiStaNodes);//安裝移動節點mac.SetType ("ns3::ApWifiMac","Ssid", SsidValue (ssid));NetDeviceContainer apDevices;//AP結點apDevices = wifi.Install (phy, mac, wifiApNode);//安裝AP節點

服務集標志符(SSID)
其中,設置主要用WifiHelper設置,助手類
3)設置移動模型
設置笛卡爾坐標系,其中AP節點坐標設為原點
主要應用助手類MobilityHelper

MobilityHelper mobility;//使用分布器GridPositionAllocator形成初始分布mobility.SetPositionAllocator ("ns3::GridPositionAllocator","MinX", DoubleValue (0.0),"MinY", DoubleValue (0.0),"DeltaX", DoubleValue (5.0),"DeltaY", DoubleValue (10.0),"GridWidth", UintegerValue (3),"LayoutType", StringValue ("RowFirst")); //利用RandomWalk2MobilityModel設置移動軌跡mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel","Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));mobility.Install (wifiStaNodes); // 為AP節點設置移動模型,設置原點mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");mobility.Install (wifiApNode);

6 安裝TCP/IP協議

InternetStackHelper stack;stack.Install (csmaNodes);stack.Install (wifiApNode);stack.Install (wifiStaNodes);Ipv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer p2pInterfaces;p2pInterfaces = address.Assign (p2pDevices);address.SetBase ("10.1.2.0", "255.255.255.0");Ipv4InterfaceContainer csmaInterfaces;csmaInterfaces = address.Assign (csmaDevices);address.SetBase ("10.1.3.0", "255.255.255.0");address.Assign (staDevices);address.Assign (apDevices);UdpEchoServerHelper echoServer (9);

7 安裝應用程序

ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);echoClient.SetAttribute ("MaxPackets", UintegerValue (1));echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));echoClient.SetAttribute ("PacketSize", UintegerValue (1024));ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));

具體解釋可以看案例1

8 設置路由

之前一個節點設置兩個網絡設備(PPP CSMA),而網絡屬于兩個不同的子網,需要連接兩個字網的節點具有路由功能。才可以進行數據包的轉發(PPP與CSMA之間)
此處設置路由協議為全局路由,采用最短路徑優先(OSPF算法),為每個節點生成路由表。對于IPV4,此處調用Ipv4GlobalRoutingHelper的PopulateRoutingTable()函數。

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

9 數據追蹤

分析網絡性能的重要前提是獲取實驗數據,ns3以pcap或ASCII文本形式保存在指定文件中,這部分代碼往往最后編輯。

pointToPoint.EnablePcapAll ("third");//EnablePcapAll()用于收集信道上所有鏈路層分組收發記錄,格式Pcap,文件名前綴為third,命名規則為“前綴-節點號-網絡設備”phy.EnablePcap ("third", apDevices.Get (0));csma.EnablePcap ("third", csmaDevices.Get (0), true);

10 啟動與結束

Simulator::Run ();Simulator::Destroy ();return 0;

運行程序

在ns3目錄下運行

./waf --run third

總結

以上是生活随笔為你收集整理的ns3 入门案例2:third.cc的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。