【ns-3-dev】Examples Tutorial II second.cc
Examples
Tutorial
Second
/* -*- 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网络,分别有2个和4个节点。其中在节点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.定义一个LOG模块 */
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
/* 4.主函数 */
int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 3;
/* 使用命令行声明nCsma变量 */
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);/* 打印UdpEchoClientApplication组件信息 */
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);/* 打印UdpEchoServerApplication组件信息 */
}
nCsma = nCsma == 0 ? 1 : nCsma;
/* 5.创建网络拓扑 */
NodeContainer p2pNodes;
p2pNodes.Create (2);/* 创建2个point-to-point型节点n0,n1 */
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));/* n1节点即是point-to-point节点,又是csma节点 */
csmaNodes.Create (nCsma);/* 创建3个csma节点n2,n3,n4 */
PointToPointHelper pointToPoint;/* PPP助手类,设置PPP信道属性 */
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));/* 设置传输速率为5Mbps */
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));/* 设置信道延迟为2ms */
NetDeviceContainer p2pDevices;/* 创建P2P网络设备 */
p2pDevices = pointToPoint.Install (p2pNodes);
CsmaHelper csma;/* csma助手类,设置csma信道属性 */
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));/* 设置传输速率为100Mbps */
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));/* 设置信道延迟为6560ns */
NetDeviceContainer csmaDevices;/* 创建csma网络设备 */
csmaDevices = csma.Install (csmaNodes);/* 连接节点与信道 */
/* 6.安装协议栈和分配IP地址 */
InternetStackHelper stack;/* 为每个节点安装协议栈 */
stack.Install (p2pNodes.Get (0));
stack.Install (csmaNodes);
Ipv4AddressHelper address;/* 为P2P网络设备分配IP地址,起始地址为10.1.1.0,终止地址为10.1.1.254 */
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");/* 为csma网络设备分配IP地址,起始地址为10.1.2.0,终止地址为10.1.2.254 */
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);
/* 7.安装应用程序 */
UdpEchoServerHelper echoServer (9);/* 监听9号窗口 */
/* 使用install方法将echoServer安装在节点n4上。Application在第1秒开始运行并接受9号端口的数据,在第10秒停止 */
ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
/* 配置客户端属性 */
UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);/* 客户端类UdpEchoClientHelper在节点n4创建一个客户端应用echoClient */
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));/* 最大发送分组个数,1 */
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));/* 分组发送间隔1秒 */
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));/* 分组负载字节大小,1024比特 */
/* 使用install方法将echoClient安装在节点n0上。Application在模拟启动第2秒开始运行并接受9号端口的数据,在第10秒停止. */
ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
/* 8.设置全局路由 */
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
/* 9.数据追踪 */
pointToPoint.EnablePcapAll ("second");/* EnablePcapAll("second")函数的作用是收集这个信道上所有节点的链路层分组收发记录。记录文件的格式是pcap。“second”是文件名前缀 */
csma.EnablePcap ("second", csmaDevices.Get (1), true);/* 记录了一个有线节点中CSMA网络设备的分组收发信息 */
/* 10.启动与结束 */
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
终端运行结果
./waf --run scratch/second
Waf: Entering directory `/home/dragon/repos/ns-3-dev/build'
Waf: Leaving directory `/home/dragon/repos/ns-3-dev/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (3.900s)
At time 2s client sent 1024 bytes to 10.1.2.4 port 9
At time 2.0078s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.0078s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.01761s client received 1024 bytes from 10.1.2.4 port 9
【学习】无线网络安全 文章被收录于专栏
无线网络安全