美文网首页
NS-3 second.cc

NS-3 second.cc

作者: Cabcab | 来源:发表于2018-12-08 11:45 被阅读0次

加过注释后如下

/* -*- 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
//n0-n1点对点通信,n1,n2,n3,n4 通过局域网通信

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");//定义日志名字(不能重复)

int 
main (int argc, char *argv[])
{
  //变量声明放在最前
  bool verbose = true;//两个日志组件是否启用
  uint32_t nCsma = 3;//csma网络中有多少节点

  CommandLine cmd;
  //一个命令行的类
  //可以在shell中使用某些附加参数如PrintHelp
  //./waf --run "scratch/example --PrintHelp"
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);//属性名称,属性说明,变量
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
  //如果通过AddValue添加自己的变量,那么可以解决每次改变参数都需要在代码中进行改变的问题
  //声明一个命令行的类可以在运行的时候改变参数,声明需要放在代码最前面
  //即./waf --run命令改变参数。
  //eg.
  /*uint32_t nPackets = 1;
   *CommandLine cmd; //声明一个命令行类
   *cmd.AddValue("nPackets","说明信息",nPackets); //加一个类似 --PrintHelp 的附加信息 --nPackets
  */
  //./waf --run "scratch/myfirst"m 则默认nPackets=1
  //./waf --run "scratch/myfirst --nPackets=2" 则默认nPackets=2

  cmd.Parse (argc,argv);//Parse 函数完成解析命令行参数。

  if (verbose)
    {
      LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
      LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
    }

  nCsma = nCsma == 0 ? 1 : nCsma;//如果nCsma=0,定义nCsma=1,否则nCsma不变

  //网络拓扑部分开始
  //point to point链路创建
  NodeContainer p2pNodes;
  p2pNodes.Create (2);

  //csma网络节点创建
  NodeContainer csmaNodes;
  csmaNodes.Add (p2pNodes.Get (1));//p2p节点1加入csma网络中
  csmaNodes.Create (nCsma);//csma内产生nCsma个节点

  //设置传输速率5Mbps和信道延迟2ms
  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));//p2pNodes中的0节点安装,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);//服务器端口设置为9
  //将server服务器安装在csma网络的n4节点。假定nCsma=
  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;
}

相关文章

  • NS-3 second.cc

    加过注释后如下

  • NS-3 简介

    ns-3模拟器是一个离散事件网络模拟器,主要用于研究和教育用途。 ns-3项目始于2006年,是一个开发ns-3的...

  • 在ns-3中如何动态调整瓶颈链路带宽

    ns-3的tutorial和安装完ns-3后,目录example下给的例子,都是预先设定好channel的band...

  • NS-TRACING

    TRACING The *ns-3 *tracing system is built on the concept...

  • NS3学习--dynamic-global-routing

    一、学习工具: 1. 参考资源: a.《ns-3网络模拟器基础及应用》 b. NS-3中文手册 c. NS3官网...

  • Internet Stack

    Internet stack aggregation ns-3源代码目录src / internet提供TCP /...

  • NS初学-second.cc

    和第一个实例大致相同,增加了Csma信道,首先创建两个节点安装P2P信道,将其中一个节点上添加csma信道,并给c...

  • NS3-ENERGY FRAMEWORK

    为无线网写的ENERGY模型 The ns-3 Energy Framework is composed of 3...

  • ns-3资料链接整理

    示例代码 ns-3官网: rip-simple-network.cc simple-global-routing....

  • NS-3安装

    Ubuntu 16.04安装顺利,Ubuntu 18.04不成功。NS-3和Ubuntu 16.04更配哦 下载安...

网友评论

      本文标题:NS-3 second.cc

      本文链接:https://www.haomeiwen.com/subject/kmqqhqtx.html