标签:des style blog http tar ext
建立总线型拓扑。
/* -*- 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
标签:des style blog http tar ext
原文地址:http://www.cnblogs.com/tempal/p/3798938.html