ns3的入门教程<2>(数据网络技术实验)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

接着上一次的报告继续,实现总线结构网络环境进行仿真

一、硬件实现

在硬件实现方面需要看下在ns3文件夹下的 /usr/local/workspace/ns-allinone-3.28.1/ns-3.28.1/examples/tutorial文夹下的文件,下面有:
first.cc, second.cc, third.cc,等文件,可以很好对ns3的硬件仿真有很好的认识。,我在这里就不再过多的阐述了,直接上总线结构的仿真代码,并进行分析。


#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"
#include "ns3/netanim-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/packet-sink.h"
// Default Network Topology

//       10.1.1.0
//       n0    n1   n2   n3   n4   n5   n6   n7   n8
//       |     |    |    |    |    |    |    |    |
//       ==========================================
//                         LAN
//
//       CSMA networks,only n0  as client , n8 as server based on UDP pro


using namespace ns3;

int 
main (int argc, char *argv[])
{
  bool verbose = true;
  uint32_t nCsma = 8;			//设置通信节点的数量,共8个通信节点

  CommandLine cmd;
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);

  cmd.Parse (argc,argv);

  nCsma = nCsma == 0 ? 1 : nCsma;
  
  //创建通信节点,共8个通信节点
  NodeContainer csmaNodes;
  csmaNodes.Create (nCsma);			

  //创建总线信道,信道的带宽为10Mbps,信道延迟为2ms
  CsmaHelper csma;			
  csma.SetChannelAttribute ("DataRate", StringValue ("10Mbps"));
  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
  csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
  
  //在所有创建的节点上安装通信设备
  NetDeviceContainer csmaDevices;
  csmaDevices = csma.Install (csmaNodes);
  
  //在所有创建的节点上安装通信协议栈
  InternetStackHelper stack;
  stack.Install (csmaNodes);

  //给所有创建的节点分配IP地址
  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer csmaInterfaces;
  csmaInterfaces = address.Assign (csmaDevices);
}

到此,基于总线结构的网络仿真已经实现到网络层,只需要在其上添加其传输层的协议,就可以实现网络仿真。

二、通信实现

1.传输层UDP通信实现

  uint16_t port = 9;   // Discard port (RFC 863)

  // Create a similar flow from n0 to n7, starting at time 1.0 seconds
  //这里的IP地址是指定收的节点的IP地址
  OnOffHelper onoff ("ns3::UdpSocketFactory", 
        Address (InetSocketAddress (Ipv4Address ("10.1.1.8"), port)));
   
  onoff.SetConstantRate (DataRate ("4Mb/s"));         //set node datarate 
    
  ApplicationContainer app = onoff.Install (csmaNodes.Get (0));
    // Start the application
    app.Start (Seconds (1.0));
    app.Stop (Seconds (10.0));

    // Create an optional packet sink to receive these packets
    PacketSinkHelper sink ("ns3::UdpSocketFactory",
           Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
    app = sink.Install (csmaNodes.Get (7));
    app.Start (Seconds (0.0));


    // Create a similar flow from n1 to n6, starting at time 1.0 seconds
    onoff.SetAttribute ("Remote", 
           AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.7"), port)));
    app = onoff.Install (csmaNodes.Get (1));
    app.Start (Seconds (1));
    app.Stop (Seconds (10.0));

    app = sink.Install (csmaNodes.Get (6));
    app.Start (Seconds (0.0));

    // Create a similar flow from n2 to n5, starting at time 1.0 seconds
    onoff.SetAttribute ("Remote", 
            AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.6"), port)));
    
    app = onoff.Install (csmaNodes.Get (2));
    app.Start (Seconds (1));
    app.Stop (Seconds (10.0));

    app = sink.Install (csmaNodes.Get (5));
    app.Start (Seconds (0.0));

    // Create a similar flow from n3 to n4, starting at time 1.0 seconds
    onoff.SetAttribute ("Remote", 
           AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.5"), port)));
    
    app = onoff.Install (csmaNodes.Get (3));
    app.Start (Seconds (1));
    app.Stop (Seconds (10.0));

    app = sink.Install (csmaNodes.Get (4));
    app.Start (Seconds (0.0));


  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  
  //抓取网络通信过程中的通信包
  csma.EnablePcapAll ("CSAM_4pair_4Mbps_UDP", false);

  FlowMonitorHelper flowmon;
  flowmon.InstallAll ();

  Simulator::Stop (Seconds (10));


  Packet::EnablePrinting ();
  
  //将网络仿真过程可视化,并且设置网络节点的位置。
  AnimationInterface anim("CSAM_4pair_4Mbps_UDP.xml");
  anim.SetConstantPosition(csmaNodes.Get(0),10,10);
  anim.SetConstantPosition(csmaNodes.Get(1),10,20);
  anim.SetConstantPosition(csmaNodes.Get(2),10,30);
  anim.SetConstantPosition(csmaNodes.Get(3),20,10);
  anim.SetConstantPosition(csmaNodes.Get(4),20,20);
  anim.SetConstantPosition(csmaNodes.Get(5),20,30);
  anim.SetConstantPosition(csmaNodes.Get(6),30,10);
  anim.SetConstantPosition(csmaNodes.Get(7),30,20);
  
  //仿真开始
  Simulator::Run ();
  
  //仿真流量监控
  flowmon.SerializeToXmlFile ("CSAM_4pair_4Mbps_UDP.flowmon", true, true);
  
  //仿真结束
  Simulator::Destroy ();
  return 0;

2.代码分析

2.1 其实在NS3中有很多可以实现UDP的类,但是为什么偏偏选择使用onoff类呢,主要有以下两个个原因:

  1. 可以控制流量,可以设置发送速率的大小,满足题目的要求。
  2. 便于UDP和TCP之间的转化,只需要修改 “ns3::UdpSocketFactory” 为**“ns3::TcpSocketFactory”** 就可以实现传输层协议的更换。

2.2 其次有基于 netanim 的仿真实现,其动态效果如下所示。
在这里插入图片描述

三、 总结

实验的全部代码可到我的资源中心下载

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值