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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

NS3入门--second.cc

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

哦吼!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"//仿真如下網絡,包含兩種網絡:point-to-point網絡和CSMA網絡,分別有兩個和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.主函數 int main (int argc, char *argv[]) {bool verbose = true;uint32_t nCsma = 3;CommandLine cmd;//讀取命令行參數中的nCsma參數。指CSMA設備的多少cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);//讀取命令行參數verbose,如果verbose為真,告訴應用打印組件信息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)建拓撲網絡NodeContainer p2pNodes;p2pNodes.Create (2); // 創(chuàng)建兩個point-to-point型節(jié)點n0,n1NodeContainer csmaNodes;csmaNodes.Add (p2pNodes.Get (1)); //添加p2p節(jié)點n1到csma網絡中csmaNodes.Create (nCsma); //創(chuàng)建nCsma個csma節(jié)點,n1,n2,n3...PointToPointHelper pointToPoint; //設置信道屬性//設置傳輸速度為5MbpspointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//設置信道延遲為2mspointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer p2pDevices; // 創(chuàng)建p2p網絡設備p2pDevices = pointToPoint.Install (p2pNodes); //將信道屬性裝載到p2p節(jié)點上,并生成網絡設備CsmaHelper csma;//傳輸速率100Mbpscsma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));//信道延遲為6560nscsma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));NetDeviceContainer csmaDevices; //創(chuàng)建csma網絡設備csmaDevices = csma.Install (csmaNodes); //連接節(jié)點與信道//6.為每個節(jié)點安裝協議棧InternetStackHelper stack;stack.Install (p2pNodes.Get (0));stack.Install (csmaNodes);Ipv4AddressHelper address;//為p2p網絡分配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網絡分配網絡地址,10.1.2.0起,10.1.2.254結束address.SetBase ("10.1.2.0", "255.255.255.0");Ipv4InterfaceContainer csmaInterfaces;csmaInterfaces = address.Assign (csmaDevices);//7.安裝應用程序UdpEchoServerHelper echoServer (9); //監(jiān)聽9號端口//用install方法將echoServer安裝在n4上,Application在第一秒開始云頂,并接受9號端口的數據,在第十秒停止。ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));//配置客戶端屬性UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);//最大發(fā)送分組個數1echoClient.SetAttribute ("MaxPackets", UintegerValue (1));//設置分組間隔1sechoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));//分組字節(jié)大小1024echoClient.SetAttribute ("PacketSize", UintegerValue (1024));//將echoClient安裝在節(jié)點n0上。Application在模擬啟動第2s開始運行并接受9號端口數據,在第10s停止ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));// 8.設置全局路由Ipv4GlobalRoutingHelper::PopulateRoutingTables ();//9.數據追蹤//以下函數的作用是收集這個信道上所有節(jié)點的鏈路層分組收發(fā)記錄。記錄文件格式是pcap,“second”是文件名前綴pointToPoint.EnablePcapAll ("second");//記錄了一個有線節(jié)點中CSMA網絡設備的分組收發(fā)信息csma.EnablePcap ("second", csmaDevices.Get (1), true);//10.啟動與結束Simulator::Run ();Simulator::Destroy ();return 0; }

參考:https://space.bilibili.com/421689634?from=search&seid=8865101383551524201

總結

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

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