7、ns-3建立拓扑
建立總線型拓撲。
/* -*- 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
*/
#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"
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
int
main (int argc, char *argv[])
{
//定義變量,用于決定是否開啟2個UdpApplication的Logging組件;默認true開啟。
bool verbose = true;
uint32_t nCsma = 3;//LAN有3個node。
CommandLine cmd;
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse (argc,argv);
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
nCsma = nCsma == 0 ? 1 : nCsma;
/**************************網絡拓撲部分****************************/
//創建使用P2P鏈路的2個node
NodeContainer p2pNodes;
p2pNodes.Create (2);
//創建另一個NodeContainer類對象,用于總線(CSMA)網絡
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);//將之前P2P的NodeContainer的第二個節點(索引1)添加到CSMA的Nodeontainer,獲得CSMA device;這個node將會有2個device
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
//安裝P2P網卡設備到P2P網絡節點,同first.cc
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
//類似于P2PHelper,CsmaHelper幫助創建和連接CSMA設備及信道
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
//數據率由channel屬性指定,而非Device屬性,因為CSMA不允許同一信道有多個不同數據率的設備。
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
//安裝網絡協議
InternetStackHelper stack;
stack.Install (p2pNodes.Get (0));//P2P鏈路中的第一節點
stack.Install (csmaNodes);
//P2P鏈路中的第二節點包含在csmaNodes中。
Ipv4AddressHelper address;//2個網段的IP地址類對象
address.SetBase ("10.1.1.0", "255.255.255.0");//安排P2P網段的地址
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
address.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);
/**************************網絡拓撲部分結束****************************/
/**************************應用程序部分****************************/
UdpEchoServerHelper echoServer (9);
//將Server服務安裝在CSMA網段的最后一個節點上,nCsma是可變的,所以不能用3.
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 (p2pNodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
/**************************應用程序部分結束****************************/
/********************調用全局路由Helper幫助建立網絡路由*********************/
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//全局路由管理器根據節點產生的鏈路通告為每個節點建立路由表
pointToPoint.EnablePcapAll ("second");//second是前綴名,不用擔心命名問題
csma.EnablePcap ("second", csmaDevices.Get (1), true);//開啟csmahelper類對象的pcap
//Netevice不用取得ID,可以直接使用,但是Node需要進一步查找ID
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
運行結果:
前面兩個都是
后面一個是
由于要查找誰有該ip所以發送arp消息。
使用可視化:./waf --run second --vis
轉載于:https://www.cnblogs.com/tempal/p/3798938.html
總結
以上是生活随笔為你收集整理的7、ns-3建立拓扑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比较常用的几个正则表达式
- 下一篇: 织梦添加搜索框里的提示消息