NS3入门--second.cc
生活随笔
收集整理的這篇文章主要介紹了
NS3入门--second.cc
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
哦吼!Second,second.cc!
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License version 2 as* published by the Free Software Foundation;** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/ // 1.頭文件 #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/applications-module.h" #include "ns3/ipv4-global-routing-helper.h"//仿真如下網(wǎng)絡(luò),包含兩種網(wǎng)絡(luò):point-to-point網(wǎng)絡(luò)和CSMA網(wǎng)絡(luò),分別有兩個和4個節(jié)點。其中在節(jié)點n1上安裝兩種NetDevice // Default Network Topology // // 10.1.1.0 // n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0//2.命名空間using namespace ns3;//3.定義日志模塊 NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");//4.主函數(shù) int main (int argc, char *argv[]) {bool verbose = true;uint32_t nCsma = 3;CommandLine cmd;//讀取命令行參數(shù)中的nCsma參數(shù)。指CSMA設(shè)備的多少cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);//讀取命令行參數(shù)verbose,如果verbose為真,告訴應(yīng)用打印組件信息cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);cmd.Parse (argc,argv);if (verbose){//打印UdpEchoClientApplication組件信息LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);//打印UdpEchoServerApplication組件信息LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);}//若nCsma為0,取1. 否則取其本身nCsma = nCsma == 0 ? 1 : nCsma;//5.創(chuàng)建拓?fù)渚W(wǎng)絡(luò)NodeContainer p2pNodes;p2pNodes.Create (2); // 創(chuàng)建兩個point-to-point型節(jié)點n0,n1NodeContainer csmaNodes;csmaNodes.Add (p2pNodes.Get (1)); //添加p2p節(jié)點n1到csma網(wǎng)絡(luò)中csmaNodes.Create (nCsma); //創(chuàng)建nCsma個csma節(jié)點,n1,n2,n3...PointToPointHelper pointToPoint; //設(shè)置信道屬性//設(shè)置傳輸速度為5MbpspointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//設(shè)置信道延遲為2mspointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer p2pDevices; // 創(chuàng)建p2p網(wǎng)絡(luò)設(shè)備p2pDevices = pointToPoint.Install (p2pNodes); //將信道屬性裝載到p2p節(jié)點上,并生成網(wǎng)絡(luò)設(shè)備CsmaHelper csma;//傳輸速率100Mbpscsma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));//信道延遲為6560nscsma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));NetDeviceContainer csmaDevices; //創(chuàng)建csma網(wǎng)絡(luò)設(shè)備csmaDevices = csma.Install (csmaNodes); //連接節(jié)點與信道//6.為每個節(jié)點安裝協(xié)議棧InternetStackHelper stack;stack.Install (p2pNodes.Get (0));stack.Install (csmaNodes);Ipv4AddressHelper address;//為p2p網(wǎng)絡(luò)分配IP地址,起始地址為10.1.1.0,終止地址為10.1.1.254address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer p2pInterfaces;p2pInterfaces = address.Assign (p2pDevices);// 為csma網(wǎng)絡(luò)分配網(wǎng)絡(luò)地址,10.1.2.0起,10.1.2.254結(jié)束address.SetBase ("10.1.2.0", "255.255.255.0");Ipv4InterfaceContainer csmaInterfaces;csmaInterfaces = address.Assign (csmaDevices);//7.安裝應(yīng)用程序UdpEchoServerHelper echoServer (9); //監(jiān)聽9號端口//用install方法將echoServer安裝在n4上,Application在第一秒開始云頂,并接受9號端口的數(shù)據(jù),在第十秒停止。ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));//配置客戶端屬性UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);//最大發(fā)送分組個數(shù)1echoClient.SetAttribute ("MaxPackets", UintegerValue (1));//設(shè)置分組間隔1sechoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));//分組字節(jié)大小1024echoClient.SetAttribute ("PacketSize", UintegerValue (1024));//將echoClient安裝在節(jié)點n0上。Application在模擬啟動第2s開始運行并接受9號端口數(shù)據(jù),在第10s停止ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));// 8.設(shè)置全局路由Ipv4GlobalRoutingHelper::PopulateRoutingTables ();//9.數(shù)據(jù)追蹤//以下函數(shù)的作用是收集這個信道上所有節(jié)點的鏈路層分組收發(fā)記錄。記錄文件格式是pcap,“second”是文件名前綴pointToPoint.EnablePcapAll ("second");//記錄了一個有線節(jié)點中CSMA網(wǎng)絡(luò)設(shè)備的分組收發(fā)信息csma.EnablePcap ("second", csmaDevices.Get (1), true);//10.啟動與結(jié)束Simulator::Run ();Simulator::Destroy ();return 0; }參考:https://space.bilibili.com/421689634?from=search&seid=8865101383551524201
總結(jié)
以上是生活随笔為你收集整理的NS3入门--second.cc的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 详解在Linux中怎么使用cron计划任
- 下一篇: docker 容器上编译 go 程序提示