ns全称是network simulator,从上个世纪发展到现在,一共有三个版本,其中ns2是ns1的改进版本,把ns1中的脚本tcl改进成具有面向对象特性的otcl脚本,在ns2中,开发者需要同时使用c++和otcl来编写仿真场景。而ns3与ns2关系并不大,虽然同是使用c++开发的,但是ns3摈弃了otcl的使用,开发者只需要使用c++就可写出自己的仿真场景,然而由于ns3是2006才开始开发的,所以有些ns2的模块并没有在ns3中继承,但是ns3也有ns2没有的新时代的模块,例如wimax,lte。总而言之,ns3入门的门槛较低,但是功能目前可能没有ns2丰富。

废话不多说了,下面开始讲使用ns3搭建的一个简单的点对点网络。

首先,该网络拓扑图如下,一共六个节点,各个节点均配置好协议栈。

实验要模拟A访问B、C、D,B访问C、D,C访问D。

下面是各条链路的带宽:

A-E:300kbps

B-E:20Mbps

E-F:100Mbps

F-C:20Mbps

F-D:100Mbps

然后,设置为B、C、D节点安装tcp的server,为A、B、C安装tcp的client的application,被设置tcp不做拥塞控制,发包速度大于链路最大带宽。

代码如下:

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>#include "ns3/core-module.h"
#include "ns3/network-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"using namespace ns3;
using namespace std;NS_LOG_COMPONENT_DEFINE ("BottleNeckTcpScriptExample");int
main (int argc, char *argv[])
{Time::SetResolution (Time::NS);//设置时间单位为纳秒LogComponentEnable ("BottleNeckTcpScriptExample", LOG_LEVEL_INFO);LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_INFO);
//    LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);LogComponentEnable ("PacketSink", LOG_LEVEL_INFO);Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (1024));Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("50Mb/s"));CommandLine cmd;cmd.Parse (argc,argv);NodeContainer nodes;nodes.Create (6);//创建六个节点//各条边的节点组合vector<NodeContainer> nodeAdjacencyList(5);nodeAdjacencyList[0]=NodeContainer(nodes.Get(0),nodes.Get(4));nodeAdjacencyList[1]=NodeContainer(nodes.Get(1),nodes.Get(4));nodeAdjacencyList[2]=NodeContainer(nodes.Get(4),nodes.Get(5));nodeAdjacencyList[3]=NodeContainer(nodes.Get(5),nodes.Get(2));nodeAdjacencyList[4]=NodeContainer(nodes.Get(5),nodes.Get(3));vector<PointToPointHelper> pointToPoint(5);pointToPoint[0].SetDeviceAttribute ("DataRate", StringValue ("300Kbps"));//网卡最大速率pointToPoint[0].SetChannelAttribute ("Delay", StringValue ("2ms"));pointToPoint[1].SetDeviceAttribute ("DataRate", StringValue ("20Mbps"));//网卡最大速率pointToPoint[1].SetChannelAttribute ("Delay", StringValue ("2ms"));pointToPoint[2].SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));//网卡最大速率pointToPoint[2].SetChannelAttribute ("Delay", StringValue ("2ms"));pointToPoint[3].SetDeviceAttribute ("DataRate", StringValue ("20Mbps"));//网卡最大速率pointToPoint[3].SetChannelAttribute ("Delay", StringValue ("2ms"));pointToPoint[4].SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));//网卡最大速率pointToPoint[4].SetChannelAttribute ("Delay", StringValue ("2ms"));vector<NetDeviceContainer> devices(5);for(uint32_t i=0; i<5; i++){devices[i] = pointToPoint[i].Install (nodeAdjacencyList[i]);}InternetStackHelper stack;stack.Install (nodes);//安装协议栈,tcp、udp、ip等Ipv4AddressHelper address;vector<Ipv4InterfaceContainer> interfaces(5);for(uint32_t i=0; i<5; i++){ostringstream subset;subset<<"10.1."<<i+1<<".0";address.SetBase(subset.str().c_str (),"255.255.255.0");//设置基地址(默认网关)、子网掩码interfaces[i]=address.Assign(devices[i]);//把IP地址分配给网卡,ip地址分别是10.1.1.1和10.1.1.2}// Create a packet sink on the star "hub" to receive these packetsuint16_t port = 50000;ApplicationContainer sinkApp;Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);sinkApp.Add(sinkHelper.Install(nodeAdjacencyList[1].Get(0)));sinkApp.Add(sinkHelper.Install (nodeAdjacencyList[4].Get(1)));sinkApp.Add(sinkHelper.Install(nodeAdjacencyList[3].Get(1)));sinkApp.Start (Seconds (0.0));sinkApp.Stop (Seconds (30.0));OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());clientHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));clientHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));ApplicationContainer clientApps;//A->BAddressValue remoteAddress(InetSocketAddress (interfaces[1].GetAddress (0), port));clientHelper.SetAttribute("Remote",remoteAddress);clientApps.Add(clientHelper.Install(nodeAdjacencyList[0].Get(0)));//A->CremoteAddress=AddressValue(InetSocketAddress (interfaces[3].GetAddress (1), port));clientHelper.SetAttribute("Remote",remoteAddress);clientApps.Add(clientHelper.Install(nodeAdjacencyList[0].Get(0)));//A->DremoteAddress=AddressValue(InetSocketAddress (interfaces[4].GetAddress (1), port));clientHelper.SetAttribute("Remote",remoteAddress);clientApps.Add(clientHelper.Install(nodeAdjacencyList[0].Get(0)));//B->CremoteAddress=AddressValue(InetSocketAddress (interfaces[3].GetAddress (1), port));clientHelper.SetAttribute("Remote",remoteAddress);clientApps.Add(clientHelper.Install(nodeAdjacencyList[1].Get(0)));//B->DremoteAddress=AddressValue(InetSocketAddress (interfaces[4].GetAddress (1), port));clientHelper.SetAttribute("Remote",remoteAddress);clientApps.Add(clientHelper.Install(nodeAdjacencyList[1].Get(0)));//C->DremoteAddress=AddressValue(InetSocketAddress (interfaces[4].GetAddress (1), port));clientHelper.SetAttribute("Remote",remoteAddress);clientApps.Add(clientHelper.Install(nodeAdjacencyList[3].Get(1)));clientApps.Start(Seconds(1.0));clientApps.Stop (Seconds (3601.0));Ipv4GlobalRoutingHelper::PopulateRoutingTables ();//嗅探,记录所有节点相关的数据包for(uint32_t i=0; i<5; i++)pointToPoint[i].EnablePcapAll("bottleneckTcp");Simulator::Run ();Simulator::Destroy ();return 0;
}

希望对所有学习ns-3的朋友有帮助。

ns-3构建简单点对点网络相关推荐

  1. 点对点网络与广播式网络的区别

    目录 一.点对点网络与广播式网络的区别 二.结点和节点 一.点对点网络与广播式网络的区别 写下本文的原因是笔者在学习计算机网络第一章时看到了下面王道书上的一句话,我不理解这句话想表达的是谁需要&quo ...

  2. 如何利用扬声器构建深度学习网络?

    简 介: 来自于康纳尔大学的这篇研究论文给出了 一个利用物理系统实现深层网络学习和推理的框架.本文对于文章举例的三个系统不属于线性时不变系统进行分析.除了其中SHG系统比较复杂之外,其它两个系统(三极 ...

  3. 1.8 简单卷积网络示例-深度学习第四课《卷积神经网络》-Stanford吴恩达教授

    ←上一篇 ↓↑ 下一篇→ 1.7 单层卷积网络 回到目录 1.9 池化层 简单卷积网络示例 (A Simple Convolution Network Example) 上节课,我们讲了如何为卷积网络 ...

  4. 服务器点对点直连,点对点网络连接怎么建立有什么作用

    点对点技术,是无中心服务器.依靠用户群(peers)交换信息的互联网体系,它的作用在于,减低以往网路传输中的节点,以降低资料遗失的风险.很多用户不知道怎么建立点对点网络连接?其实方法很简单,下面就由小 ...

  5. 如何使用TensorFlow构建简单的图像识别系统(第2部分)

    by Wolfgang Beyer 沃尔夫冈·拜尔(Wolfgang Beyer) 如何使用TensorFlow构建简单的图像识别系统(第2部分) (How to Build a Simple Ima ...

  6. 网络爬虫数据挖掘_我如何构建无服务器网络爬虫以大规模挖掘温哥华房地产数据...

    网络爬虫数据挖掘 by Marcello Lins 通过Marcello Lins 我如何构建无服务器网络爬虫以大规模挖掘温哥华房地产数据 (How I built a serverless web ...

  7. “东数西算”取得新进展,发改委等四部门发文构建国家算力网络体系 | 产业区块链发展周报...

    摘要 产业动态: 人民银行启动金融数据综合应用试点 区块链服务网络(BSN)将于7月31日进行季度版本迭代更新 区块链技术公司AgUnity为百万农村农民和组织之间建立支持区块链的最后一英里链接 古根 ...

  8. 简单局域网网络故障排查和处置

    简单局域网网络故障排查和处置 一.了解基本网络构成 1. IP传输通信图 2.有线网络 (一)物理层面 (1)网线 (2)网线的制作 (3)网卡接口 (4)光纤 (5)光纤接口 (6)收发器 (7)集 ...

  9. 高防cdn的构建简单吗?高防cdn有什么优势?

    经常有人在网上咨询这样的问题,很多人对于高防CDN的概念不是很理解,在具体搭建的过程中会出现一些问题,下面小编就来给大家介绍下高防CDN. 一.高防CDN是什么意思? 高防CDN是为了更好的服务网络而 ...

最新文章

  1. 计算机和网络历史地位,所谓“运营商的文字游戏”,其实是计算机和网络发展的客观历史导致的...
  2. 怎么通过media foundation将图像数据写入虚拟摄像头_不知道怎么挑手机?性价比神机绝对适合你...
  3. 北京年会和关于数据仓库板块的思考
  4. python opencv cv.waitKey(1) 0xFF 的作用
  5. 9种常用的数据分析方法
  6. 无线硬盘的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  7. oracle获取字符串长度函数length()和hengthb()
  8. Ubuntu Vmware虚拟机网络配置(一)
  9. 电脑显示your pc android,新版DeX模式不再强调手机变PC,而是让Android融合PC
  10. USB数据线串联电阻知识总结
  11. 面试积累——嵌入式软件工程师面试题(非常经典)
  12. 我的权限控制(JBX + struts + hibernate + ORACLE)
  13. MATLAB学习笔记之矩阵和数组1.1(mathematic)
  14. Graham Scan算法
  15. Linux下批量把GDK编码的文章转化为UTF-8编码的文章以及“iconv: 未知 xxx 处的非法输入序列”错误处理
  16. 服务器多系统ssd寿命检测,检测 SSD 剩余寿命 寿终正寝之前还能挽回数据
  17. 荣耀v10图片是html格式,荣耀V10真机上手图赏 参数配置分析详解
  18. 微擎+微赞(微官网)100套精华模板完美完整版
  19. Linux最常用的关机命令介绍!
  20. YTU OJ 2914 Problem A xiaoping学构造函数

热门文章

  1. 《C语言名题精选百则----8》
  2. Echarts数据转换transform
  3. 64G刷32G的emuelec整合包 首次扩容不成功后的再次扩容办法
  4. 石油大 金币 二分答案
  5. 随机生成植物生长及舞动算法
  6. 操作系统实验六:Linux下的C语言编程
  7. 一个爬进正方教务系统的爬虫的诞生
  8. 一款好用的程序员切图标注神器
  9. 深度学习论文笔记:Fast R-CNN
  10. 大写日期转换成阿拉伯数字的算法