ns3的入门教程<3>(数据网络技术实验)
作者:互联网
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
ns3的入门教程<3>
前言
接着上一次的报告继续,实现星型结构网络环境进行仿真
一、硬件实现
在硬件实现方面需要看下在ns3文件夹下的 /usr/local/workspace/ns-allinone-3.28.1/ns-3.28.1/examples/tutorial文夹下的文件,下面有:
first.cc, second.cc, third.cc,等文件,可以很好对ns3的硬件仿真有很好的认识。,我在这里就不再过多的阐述了,直接上星型结构的仿真代码,并进行分析。
// Network topology
//
// n0 n1 n2 n3
// | | | |
// -------------------
// Switch
// -------------------
// | | | |
// n4 n5 n6 n7
//
//
// - CBR/UDP flows from n0 to n1
// - based on UDP
// - Tracing of queues and packet receptions to file "csma-bridge.tr"
#include <iostream>
#include <fstream>
#include "para.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/bridge-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("CsmaBridgeExample");
int
main (int argc, char *argv[])
{
//
// Users may find it convenient to turn on explicit debugging
// for selected modules; the below lines suggest how to do this
//
CommandLine cmd;
cmd.Parse (argc, argv);
NS_LOG_INFO ("Create nodes.");
//设置通信节点的数量,共8个通信节点
NodeContainer terminals;
terminals.Create (8);
//创建网桥,即网络中心
NodeContainer csmaSwitch;
csmaSwitch.Create (1);
//创建网桥,信道的带宽为10Mbps,信道延迟为2ms
NS_LOG_INFO ("Build Topology");
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", DataRateValue (10000000));
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
// Create the csma links, from each terminal to the switch
//在网桥上安装通信设备
NetDeviceContainer terminalDevices;
NetDeviceContainer switchDevices;
//将网桥与通信节点相连接
for (int i = 0; i < 8; i++)
{
NetDeviceContainer link = csma.Install (NodeContainer (terminals.Get (i), csmaSwitch));
terminalDevices.Add (link.Get (0));
switchDevices.Add (link.Get (1));
}
// Create the bridge netdevice, which will do the packet switching
Ptr<Node> switchNode = csmaSwitch.Get (0);
BridgeHelper bridge;
bridge.Install (switchNode, switchDevices);
// Add internet stack to the terminals
InternetStackHelper internet;
internet.Install (terminals);
// We've got the "hardware" in place. Now we need to add IP addresses.
//
NS_LOG_INFO ("Assign IP Addresses.");
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
ipv4.Assign (terminalDevices);
到此,基于星型结构的网络仿真已经实现到网络层,只需要在其上添加其传输层的协议,就可以实现网络仿真。
二、通信实现
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类呢,主要有以下两个个原因:
- 可以控制流量,可以设置发送速率的大小,满足题目的要求。
- 便于UDP和TCP之间的转化,只需要修改 “ns3::UdpSocketFactory” 为**“ns3::TcpSocketFactory”** 就可以实现传输层协议的更换。
2.2 其次有基于 netanim 的仿真实现,其动态效果如下所示。
三、 总结
实验的全部代码可到我的资源中心下载
标签:Get,Seconds,app,入门教程,Install,数据网络,ns3,csmaNodes 来源: https://blog.csdn.net/qq_42935317/article/details/113453832