NS_3中第二个示例程序second.cc和第三个示例程序third.cc的注解

本文介绍了NS3网络模拟器的两个示例程序,分别构建了包含点对点通信、CSMA局域网及Wi-Fi网络的混合网络拓扑,并实现了不同节点间的UDP回显服务。示例详细展示了网络设备的配置、移动模型设置、协议栈安装等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:https://blog.youkuaiyun.com/finded/article/details/45745859


1. second.cc


<span style="font-size:18px;">#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  
//n0和n1实现点到点通信,n1~n4是个csma有线局域网。网络可以实现n0经由n1,和n4进行通信。  
  
using namespace ns3;  
  
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");//定义日志组件  
  
int   
main (int argc, char *argv[])  
{  
  bool verbose = true;//定义bool变量用于决定是否开启日志组件,默认开启  
  uint32_t nCsma = 3;//变量,用于定义csma网络中有多少个node  
  
  CommandLine cmd;  
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);  
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);  
//命令行参数,设置是否开启logging  
  cmd.Parse (argc,argv);  
  
  if (verbose)  
    {  
      LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);  
      LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);  
    }  
  
  nCsma = nCsma == 0 ? 1 : nCsma;  
//********网络拓扑部分********//  
//p2p链路的两个节点创建  
  NodeContainer p2pNodes;  
  p2pNodes.Create (2);  
  
  //创建csma网络节点  
  NodeContainer csmaNodes;  
  csmaNodes.Add (p2pNodes.Get (1));//将p2p节点1加到csma网络中  
  csmaNodes.Create (nCsma);  
  
  //设置p2p传输速率和信道延迟  
  PointToPointHelper pointToPoint;  
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));  
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));  
  //安装p2p网卡到p2p网络节点  
  NetDeviceContainer p2pDevices;  
  p2pDevices = pointToPoint.Install (p2pNodes);  
  
  //设置csma传输速率和信道延迟,注意传输速率有channel指定,而非device属性。(CSMA不允许同一信道上有多个不同数据率的设备)  
  CsmaHelper csma;  
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));  
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));  
  
  NetDeviceContainer csmaDevices;  
  csmaDevices = csma.Install (csmaNodes);  
  //安装协议  
  InternetStackHelper stack;  
  stack.Install (p2pNodes.Get (0));//p2p的n0节点,n1节点在csma网络中安装协议栈  
  stack.Install (csmaNodes);  
  
  Ipv4AddressHelper address;  
  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");  
  Ipv4InterfaceContainer csmaInterfaces;  
  csmaInterfaces = address.Assign (csmaDevices);  
    
  //********应用程序部分********//  
  UdpEchoServerHelper echoServer (9);//服务器端口号  
  //将server服务器安装在csma网络的n4节点。假定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);//设定远程服务器的IP地址和端口号  
  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));  
    
  //********建立网络路由********//  
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();  
    
  //********开启pcap追踪********//  
  //开启p2pHelper,csmaHelper类对象的pcap  
  pointToPoint.EnablePcapAll ("second");  
  csma.EnablePcap ("second", csmaDevices.Get (1), true);  
  
  Simulator::Run ();  
  Simulator::Destroy ();  
  return 0;  
}</span><span style="font-size:24px;">  
</span>  


2. third.cc


<span style="font-size:18px;">#include "ns3/core-module.h"  
#include "ns3/point-to-point-module.h"  
#include "ns3/network-module.h"  
#include "ns3/applications-module.h"  
#include "ns3/wifi-module.h"  
#include "ns3/mobility-module.h"  
#include "ns3/csma-module.h"  
#include "ns3/internet-module.h"  
  
// Default Network Topology网络拓扑  
//  
//   Wifi 10.1.3.0  
//                 AP  
//  *    *    *    *  
//  |    |    |    |    10.1.1.0  
// n5   n6   n7   n0 -------------- n1   n2   n3   n4  
//                   point-to-point  |    |    |    |  
//                                   ================  
//                                     LAN 10.1.2.0  
//n0,n5-n7:wifi网络,n0为AP,n5,n6,n7为无线STA; n0和n1:p2p通信; n1-n4:csma网络。示例中实现了节点n7和n4通信。  
  
using namespace ns3;  
  
NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");  
  
int   
main (int argc, char *argv[])  
{  
  bool verbose = true;//用于是否启用logging组件  
  uint32_t nCsma = 3;//设置nCsma和nWifi的额外STA个数  
  uint32_t nWifi = 3;  
  
  CommandLine cmd;  
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);  
  cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);  
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);  
    
  
  cmd.Parse (argc,argv);  //CommandLine cmd; cmd.Parse (argc,argv);这两句代表以命令行形式接收命令参数,运行程序在shell中输入./waf --run examples/tutorial/third --vis可以让系统调用Visualizer模块(由于是命令行调用,脚本中不需要加入Visualizer.h这个头文件)让PyViz实时演示脚本运行过程
  
  if (nWifi > 18)  
    {  
      std::cout << "Number of wifi nodes " << nWifi <<   
                   " specified exceeds the mobility bounding box" << std::endl;  
      exit (1);  
    }  
  
  if (verbose)  
    {  
      LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);  
      LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);  
    }  
 //创建p2p链路2个节点  
  NodeContainer p2pNodes;  
  p2pNodes.Create (2);  
  
  PointToPointHelper pointToPoint;  
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));  
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));  
  
  NetDeviceContainer p2pDevices;  
  p2pDevices = pointToPoint.Install (p2pNodes);  
  
  //创建csma网络的4个节点。默认nCsma=3  
  NodeContainer csmaNodes;  
  csmaNodes.Add (p2pNodes.Get (1));  
  csmaNodes.Create (nCsma);  
  
  CsmaHelper csma;  
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));  
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));  
  
  NetDeviceContainer csmaDevices;  
  csmaDevices = csma.Install (csmaNodes);  
    
  //*********创建wifi网络的4个节点,默认STA=3;**********//  
  NodeContainer wifiStaNodes;  
  wifiStaNodes.Create (nWifi);//创建STA  
  NodeContainer wifiApNode = p2pNodes.Get (0);//设置wifi的AP  
  
  //配置PHY和Channel通道助手  
  YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();  
  YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();  
  phy.SetChannel (channel.Create ());//Channel对象与PHY对象关联  
  
  //MAC层配置==============================  
  WifiHelper wifi = WifiHelper::Default ();  
  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");//使用AARF速率控制算法  
  
  NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();//non-Qos的MAC  
  //配置wifi网络STA网络设备的MAC类型和基础设施网络的SSID  
  Ssid ssid = Ssid ("ns-3-ssid");//IEEE 802.1服务集标识符对象  
  mac.SetType ("ns3::StaWifiMac", //设置为特定种类MAC层,non-AP  
               "Ssid", SsidValue (ssid),//设置SSID  
               "ActiveProbing", BooleanValue (false));//设置ActiveProbing,false意味着助手创建的mac将不会发送探测请求。  
  
  NetDeviceContainer staDevices;//STA 网络设备  
  staDevices = wifi.Install (phy, mac, wifiStaNodes);//wifi的STA安装PHY层和MAC层  
    
  //配置wifi网络AP网络设备的MAC类型和基础设施网络的SSID  
  mac.SetType ("ns3::ApWifiMac",  
               "Ssid", SsidValue (ssid));  
  
  NetDeviceContainer apDevices;  
  apDevices = wifi.Install (phy, mac, wifiApNode);//与STA共同的PHY特性  
    
  //********移动模型********//  
  MobilityHelper mobility;  
  
  mobility.SetPositionAllocator ("ns3::GridPositionAllocator", //网格位置分配器  
                                 "MinX", DoubleValue (0.0),  
                                 "MinY", DoubleValue (0.0),  
                                 "DeltaX", DoubleValue (5.0),  
                                 "DeltaY", DoubleValue (10.0),  
                                 "GridWidth", UintegerValue (3),  
                                 "LayoutType", StringValue ("RowFirst"));  
  
  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", //以随机游走的方式移动  
                             "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));  
  mobility.Install (wifiStaNodes); //安装到wifi网络的STA  
  
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");  
  mobility.Install (wifiApNode);//安装到wifi网络的AP  
  
  //安装协议栈  
  InternetStackHelper stack;  
  stack.Install (csmaNodes);  
  stack.Install (wifiApNode);  
  stack.Install (wifiStaNodes);  
  
  Ipv4AddressHelper address;  
  
  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");  
  Ipv4InterfaceContainer csmaInterfaces;  
  csmaInterfaces = address.Assign (csmaDevices);  
  
  address.SetBase ("10.1.3.0", "255.255.255.0");  
  address.Assign (staDevices);  
  address.Assign (apDevices);  
    
  //安装应用  
  UdpEchoServerHelper echoServer (9);  
  
  ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));//csma网络的最后节点作为服务器  
  serverApps.Start (Seconds (1.0));  
  serverApps.Stop (Seconds (10.0));  
  
  UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);//设置客户机的远程服务器IP地址和端口号  
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));  
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));  
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));  
  
  ApplicationContainer clientApps =   
    echoClient.Install (wifiStaNodes.Get (nWifi - 1));//在随后建立的wifi STA上安装客户机  
  clientApps.Start (Seconds (2.0));  
  clientApps.Stop (Seconds (10.0));  
  
  //建立网络路由  
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();  
  
  Simulator::Stop (Seconds (10.0));//无线接入点会一直产生信标,模拟器时间列表一直非空,仿真无法结束  
  //  
  pointToPoint.EnablePcapAll ("third");  
  phy.EnablePcap ("third", apDevices.Get (0));  
  csma.EnablePcap ("third", csmaDevices.Get (0), true);  
  
  Simulator::Run ();  
  Simulator::Destroy ();  
  return 0;  
}  
</span>  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值