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 packets
uint16_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->B
AddressValue remoteAddress
(InetSocketAddress (interfaces[1].GetAddress (0), port));
clientHelper.SetAttribute("Remote",remoteAddress);
clientApps.Add(clientHelper.Install(nodeAdjacencyList[0].Get(0)));
//A->C
remoteAddress=AddressValue(InetSocketAddress (interfaces[3].GetAddress (1), port));
clientHelper.SetAttribute("Remote",remoteAddress);
clientApps.Add(clientHelper.Install(nodeAdjacencyList[0].Get(0)));
//A->D
remoteAddress=AddressValue(InetSocketAddress (interfaces[4].GetAddress (1), port));
clientHelper.SetAttribute("Remote",remoteAddress);
clientApps.Add(clientHelper.Install(nodeAdjacencyList[0].Get(0)));
//B->C
remoteAddress=AddressValue(InetSocketAddress (interfaces[3].GetAddress (1), port));
clientHelper.SetAttribute("Remote",remoteAddress);
clientApps.Add(clientHelper.Install(nodeAdjacencyList[1].Get(0)));
//B->D
remoteAddress=AddressValue(InetSocketAddress (interfaces[4].GetAddress (1), port));
clientHelper.SetAttribute("Remote",remoteAddress);
clientApps.Add(clientHelper.Install(nodeAdjacencyList[1].Get(0)));
//C->D
remoteAddress=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的朋友有帮助。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有