哦吼!Second,second.cc!

/* -*- 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网络,分别有两个和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.定义日志模块
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");//4.主函数
int
main (int argc, char *argv[])
{bool verbose = true;uint32_t nCsma = 3;CommandLine cmd;//读取命令行参数中的nCsma参数。指CSMA设备的多少cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);//读取命令行参数verbose,如果verbose为真,告诉应用打印组件信息cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);cmd.Parse (argc,argv);if (verbose){//打印UdpEchoClientApplication组件信息LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);//打印UdpEchoServerApplication组件信息LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);}//若nCsma为0,取1. 否则取其本身nCsma = nCsma == 0 ? 1 : nCsma;//5.创建拓扑网络NodeContainer p2pNodes;p2pNodes.Create (2);  // 创建两个point-to-point型节点n0,n1NodeContainer csmaNodes;csmaNodes.Add (p2pNodes.Get (1)); //添加p2p节点n1到csma网络中csmaNodes.Create (nCsma);    //创建nCsma个csma节点,n1,n2,n3...PointToPointHelper pointToPoint;  //设置信道属性//设置传输速度为5MbpspointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//设置信道延迟为2mspointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer p2pDevices;  // 创建p2p网络设备p2pDevices = pointToPoint.Install (p2pNodes);  //将信道属性装载到p2p节点上,并生成网络设备CsmaHelper csma;//传输速率100Mbpscsma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));//信道延迟为6560nscsma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));NetDeviceContainer csmaDevices;  //创建csma网络设备csmaDevices = csma.Install (csmaNodes);  //连接节点与信道//6.为每个节点安装协议栈InternetStackHelper stack;stack.Install (p2pNodes.Get (0));stack.Install (csmaNodes);Ipv4AddressHelper address;//为p2p网络分配IP地址,起始地址为10.1.1.0,终止地址为10.1.1.254address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer p2pInterfaces;p2pInterfaces = address.Assign (p2pDevices);// 为csma网络分配网络地址,10.1.2.0起,10.1.2.254结束address.SetBase ("10.1.2.0", "255.255.255.0");Ipv4InterfaceContainer csmaInterfaces;csmaInterfaces = address.Assign (csmaDevices);//7.安装应用程序UdpEchoServerHelper echoServer (9);  //监听9号端口//用install方法将echoServer安装在n4上,Application在第一秒开始云顶,并接受9号端口的数据,在第十秒停止。ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));//配置客户端属性UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);//最大发送分组个数1echoClient.SetAttribute ("MaxPackets", UintegerValue (1));//设置分组间隔1sechoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));//分组字节大小1024echoClient.SetAttribute ("PacketSize", UintegerValue (1024));//将echoClient安装在节点n0上。Application在模拟启动第2s开始运行并接受9号端口数据,在第10s停止ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));// 8.设置全局路由Ipv4GlobalRoutingHelper::PopulateRoutingTables ();//9.数据追踪//以下函数的作用是收集这个信道上所有节点的链路层分组收发记录。记录文件格式是pcap,“second”是文件名前缀pointToPoint.EnablePcapAll ("second");//记录了一个有线节点中CSMA网络设备的分组收发信息csma.EnablePcap ("second", csmaDevices.Get (1), true);//10.启动与结束Simulator::Run ();Simulator::Destroy ();return 0;
}

参考:https://space.bilibili.com/421689634?from=search&seid=8865101383551524201

NS3入门--second.cc相关推荐

  1. NS3入门--first.cc

    因为实验的需要,被迫从OMNet++转战到NS3,哎,OMNet还没闹明白呢-这大概就是先帝创业未半而中道崩殂吧!呸呸呸,今天看一下NS3中examples中tutorial的第一个例程first.c ...

  2. ns3入门(1)——第一个案例first.cc

    ns3入门(1)--第一个案例first.cc 一.安装ns3 安装没啥可说的,参考一下别人的网站吧. 说下我的环境吧,ubuntu16.04,仅此. 1.安装eclipse https://blog ...

  3. NS3入门:第一个程序first.cc

    运行 环境配置 在VMware虚拟环境当中运行first.cc 首先安装虚拟机,然后配置环境:参考文章 NS3详细安装教程 安装完成之后,进入终端,运行以下代码 首先进入文件夹当中: cd tarba ...

  4. NS3 入门环境搭建

    NS3 入门环境搭建3.30版本 环境:Windows10 + Ubuntu18.04双系统 环境:Windows10 + Ubuntu18.04双系统 1.添加源 sudo vim /etc/apt ...

  5. ns3 入门案例2:third.cc

    代码分析 1 头文件 #include "ns3/core-module.h" #include "ns3/point-to-point-module.h" # ...

  6. ns3入门案例1 first.cc

    1.目录结构 example:1.根文件下自带示例结构,作为良好的参考资源 2.src环境下中各模块中example作为资源 build: 编译后文件以及可执行文件 src:各模块源代码 2.新代码运 ...

  7. ns3中first.cc~fourth.cc代码注释

    参考博客: ubuntu 18.04安装NS-3教程(详细安装过程) ns-3脚本初识--点对点有线网络:first脚本 ns-3中的数据跟踪与采集--Tracing系统综述及fourth脚本 --- ...

  8. Java安全入门(二)——CC链1 分析+详解

    组件介绍 Apache Commons 当中有⼀个组件叫做 Apache Commons Collections ,主要封装了Java 的 Collection(集合) 相关类对象,它提供了很多强有⼒ ...

  9. NS-3教程(2):NS-3简单介绍

    本文主要介绍一些NS-3入门性质的知识. 简介 什么是NS-3? ns-3是一个离散事件驱动网络模拟器,主要作为研究和教学使用.ns-3是基于GNU GPLv2下的免费的开源软件.ns-3意图最终能够 ...

最新文章

  1. asp.net mysql 创建变_[ASP.net教程]EF Core使用CodeFirst在MySql中创建新数据库以及已有的Mysql数据库如何使用DB First生成域模型...
  2. 【Java Web开发指南】Spring一些基础问题整理
  3. 【计算理论】可判定性 ( 对角线方法 | 使用对角线方法证明 通用任务图灵机 语言 不可判定 )
  4. HDU Problem - 3763 CD(二分)
  5. MongoDB的高级语法
  6. asp.net 强制性单一登陆现实
  7. python if else格式_Python if else条件语句详解
  8. centos7安装gitlab_Docker常用镜像安装:MySql Redis GitLab maven私服等
  9. 互联网如何“武装”农民?
  10. android自动化测试抖音,全自动化的抖音启动速度测试
  11. VirtualBox常用命令
  12. HLW8032做220V电量采集方案测试
  13. Android:LiveData postValue导致数据丢失问题,及其原因
  14. matlab 与 mathmatica 编程对比(Vyi个人学习笔记)
  15. 2018年cocos2d-x面试题目总结
  16. 1002-电影观后感---乔布斯传
  17. 华为云ESC产品突飞猛进,引领行业成长
  18. 用python的scipy中的odeint来解常微分方程中的一些细节问题(适用于小白)
  19. 并行计算复习————第一篇 并行计算硬件平台:并行计算机
  20. 国外对于击落F-22反应报道:中国式的幽默

热门文章

  1. (二)k-means算法原理以及python实现
  2. Lucene学习总结之四:Lucene索引过程分析
  3. Scala入门到精通——第二十六节 Scala并发编程基础
  4. Embedding在网易严选搜索推荐中的应用
  5. 【Java】SAX解析characters 错误截取问题的解决
  6. 微信小程序自定义组件方案
  7. LintCode-落单的数 III
  8. CSS魔法堂:深入理解line-height和vertical-align
  9. 鼠标右键添加项目 注册表 注意
  10. IT第三阶段?“智慧的运算”的未解之谜