ns-3构建简单点对点网络

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搭建的一个简单的点对点网络。

首先,该网络拓扑图如下,一共六个节点,各个节点均配置好协议栈。ns-3构建简单点对点网络_第1张图片

实验要模拟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 
#include 
#include 
#include 

#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 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 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 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 interfaces(5);
    for(uint32_t i=0; i<5; i++)
    {
        ostringstream subset;
        subset<<"10.1."<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的朋友有帮助。

你可能感兴趣的:(网络仿真)