美文网首页
ns3仿真demo(third.cc(wifi))

ns3仿真demo(third.cc(wifi))

作者: 小超超爱超超 | 来源:发表于2017-07-18 21:18 被阅读0次

ns3仿真demo(third.cc)

一. 运行third.cc 

#include ''ns3/core-module.h''

#include "ns3/point-to-point-module.h"

#include "ns3/network-module.h"

#include "ns3/applications-module.h"

#include "ns3/wifi-module.h"

#include "ns3/mobility-module.h"

#include "ns3/csma-module.h"

#include "ns3/internet-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");

bool verbose =3;

uint32_t nCsma =3;

uint32_t nWifi =3;

CommandLine cmd;

cmd.AddValue ("nCsma", "Number of \"etra\" CSMA nodes/devices", nCsma);

cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);

cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);

cmd.Parse (argc, argv);

if (verbose)

{

LogComponmentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);

LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);

}

//创建2个节点用于连接点到点链路

NodeContainer p2pNodes;

p2pNodes.Create (2);

//实例化PointToPointHelper并且设置相关默认属性,在助手生成的设备上创建5Mbit/s的发射器和2ms的延迟信道。然后在节点和信道间安装设备。

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute("DataRate", StringValue ("5Mbit/s"));

pointToPoint.SetChannelAttribute("Delay", StringValue ("2ms"));

NetDeviceContainer p2pDevicers;

p2pDevices = point ToPoint.Install (p2pNodes);

//声明NodeContainer放置已经成为CSMA网络一部分的节点。在点到点的节点容器中得到第一个节点,把它加入到CSMA设备的节点容器中,再建立一些‘多余’节点。

NodeContainer csmaNodes;

csmaNodes.Add (p2pNodes.Get (1));

csmaNodes.Create (nCsma);

//实例化CsmaHelper并设置属性,创建NetDeviceContainer与CSMA网络设备同步,安装CSMA设备到节点。

CsmaHelper csma;

csma.SetChannelAttribute("DataRte", StringValue ("100Mbit/s"));

csma.SetChannelAttribute("Delay", TimeValue (NanoSecondes (6560)));

NetDeviceContainer csmaDevices;

csmaDevices = csma.Install (csmaNodes);

//创建多个命令行参数指定节点,使点到点链路“最左边”节点作为接入点。

NodesContainer wifiStaNodes;

wifiStaNodes.Create (nwifi);

NodeContainer wifiApNode = p2pNodes.Get (0);

//构建无线设备与无线节点之间的互连通道。首先设置PHY和通道助手。

YansWifiChannelHelper channel = YansWifiChannelHelper::Defaulf();

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

//使用默认的PHY层配置和信道模型。创建通道对象并把它关联到物理层对象管理器,以保证所有由YansWifiPhyHelper创建的物理层对象共享相同的底层信道,它们共享相同的无线媒介并能相互通信和干扰。

phy.SetChannel (channel.Create ());

//选用non-Qos的MAC,用NqosWifiMacHelper对象设置MAC参数。SetRemoteStationManager的方法告诉助手使用何值速率控制算法,本例选用AARF算法。

WifiHelper wifi = WifiHelper::Default ();

wifi.SetRemoteStationManager ("ns3::AarfWifiManager");

NqosWifiMacHelper mac = NqosWifimacHelper::Default();

//配置MAC类型和基础设施网络的SSID。先创建IEEE802.11服务集标示符(SSID)对象,用来设置MAC层的“SSID”属性值。助手创建的特定种类MAC层被“ns3::StaWifiMac”类型属性所指定。使用NqosWifiMacHelper确保“QosSupported”属性创建的MAC对象设置为false。MAC实例在BSS中既是non-Qos又是non-AP。最后,“Activeprobing”属性设置为false,意味着助手创建的MAC将不会发送探测请求。

Ssid  ssid = Ssid ("ns-3-ssid");

mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid), "ActiveProving",Booleanvalue(false));

//一旦站的具体参数配置完成后,在MAC层和PHY层可以调用熟悉的安装方法来创建这些站的无线设备。

NetDeviceContainer staDevices;

staDevices = wifi.Install (phy, mac, wifiStaNodes);

//已经给所有STA节点配置了无线,现在需要配置AP(接入点)节点。改变NqosWifiMacHelper默认属性反映的AP请求。在这种情况下,NqosWifiMacHelper将创建“ns3::ApWifiMac”的MAC层,后者指定配置成AP的MAC实例要被创建。

mac.SetType("ns3::ApWifiMac","Ssid", SsidValue (ssid));

//创建单一AP共享相同的PHY层属性。

NetDeviceContainer apDevices;

apDevices = wifi.Install (phy, mac, wifiApNode);

//加入模块。希望STA节点能够移动,使AP节点固定住。使用MobilityHelper很容易做到这点。

MobilityHelper mobilty;

mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "Minx", DoubleValue(0.0),

“MinY”, DoubleValue(0.0), "Deltax", DoubleValue(5.0),

"DeltaY",DoubleValue(10.0),"GridWidth",UintegerValue(3),"LayoutType",StringValue("RowFirst"));

//RandomWalk2dMobilityModel,节点以随机游走的方式移动,令MobilityHelper在STA节点上安装移动模型。

mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",

"Bounds",RectangleValue(Rectangle(-50,50,-50,50)));

mobility.Install(wifiStaNodes);

//现在已经创建了节点、设备、信道和移动模型,但是还需要协议栈。安装协议栈可以使用InternetStackHelper。

InternetStackHelper stack;

stack.Install(csmaNodes);

stack.Install(wifiApNode);

stack.Install(wifiStaNodes);

//接下来使用Ipv4AddressHelper分配IP地址给设备接口。首先使用网络10.1.1.0创建2个点到点设备需要的2个地址。然后使用网络10.1.2.0分配地址给CSMA网络,最后网络10.1.3.0分配地址给STA设备和无线网络的AP。

Ipv4AddressHelper address;

address.SetBase("10.1.1.0", "255.255.255.0");

Ipv4InterfaceContainer p2pInterfaces;

p2pInterfaces=address.Assign(p2pDevices);

address.SetBase("10.1.2.0","255.255.255.0");

Ipv4InterfaceContainer csmaInterfaces;

csmaInterfaces=address.Assign(csmaDevices);

address.SetBase("10.1.3.0","255.255.255.0");

address.Assign(staDevices);

address.Assign(apDevices);

//在“最右端”的节点放置echo服务端程序。

UdpEchoServerHelper echoServer(9);

ApplicationContainer serverApps=echoServer.Install(csmaNodes.Get(nCsma));

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(10.0));

//然后将回显客户端放在最后创建的STA节点上,指向CSMA网络的服务器。

UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma),9);

echoClient.SetAttribute("MaxPackets",UintegerValue(1));

echoClient.SetAttribute("Interval",TimeValue(Seconds(1.0)));

echoClient.SetAttribute("PacketSize",UintegerValue(1024));

ApplicationContainer clientApps=

echoClient.Install(wifiStaNodes.Get(nWifi-1));

clientApps.Start(Seconds(2.0));

clientApps.Stop(Seconds(10.0));

Ipv4GlobalRoutingHelper::PopulateRoutingTables();

//由于已经建立了互联网络,需要启用互联网络的路由。

Ipv4GlobalRoutingHelper::PopulateRoutingTables();

//刚刚创建的模拟将永远不会“自然”停止,这是因为要求的无线接入点会产生信标。它会一直产生信标,导致模拟器事件会无限期地出现在日程中,所以必须告诉模拟器停下来。下面的代码会告诉模拟器停止,否则会进入无线循环。

Simulator::Stop(Seconds(10.0));

//现在创建了涵盖所有3个网络的跟踪。下面的代码将在作为骨干的点到点节点进行pcap跟踪,无线网络和CSMA网络上启动混杂模式跟踪。会看到最少的跟踪文件产生的流量。

pointToPoint.EnablePcapAll("third");

phy.EnablePcap("third",apDevices.Get(0));

csma.EnablePcap("third",csmaDevices.Get(0),true);

//最后需要运行模拟,清空并且退出程序。

Simulator::Run();

Simulator::Destroy();

return 0;}

1.打开目录examples/tutorial/third.cc,将third.cc复制到ns-allinone-3.26/ns-3.26文件夹中

2.运行:sudo ./waf --run third, 如下图所示。

图一

二. 问题

未发现4个跟踪文件:third-0-0.pcap, third-0-1.pcap,third-1-0.pcap,third-1-1.pcap。

三. 可视化界面

sudo ./waf --run scratch/third --vis

图二 可视化界面

相关文章

  • ns3仿真demo(third.cc(wifi))

    ns3仿真demo(third.cc) 一. 运行third.cc #include ''ns3/core-mod...

  • 基于NS3的无线链路物理层仿真实验

    一、实验目的 掌握NS3的基本仿真方法 熟悉NS3的无线链路模型 改进NS3的无线链路模型 二、实验背景 在原有的...

  • 2022-07-20

    Third.cc和移动模型 wifihelper 在thrid.cc中,对于wifi的helper和p2phelp...

  • Ubuntu下的NS3安装与入门

    NS3的开发旨在为网络研究和教育提供开放,可扩展的网络仿真平台。简而言之,NS3提供了分组数据网络如何工作和执行的...

  • NS3 Events和Simulator内容翻译

    以下内容是NS3手册翻译。 1.4 Events and Simulator ns-3是一个离散事件网络仿真器。从...

  • 用NS3进行虚拟仿真

    一.下载虚拟机 直接下载虚拟机的安装包,然后分配内存等一系列操作,后注册用户 二.安装NS3虚拟仿真软件 1.在网...

  • 无标题文章

    # NS3学习报告一 ## 一.实验结果 1. 用命令行读入并且输出了姓名和学号 2. 仿真在5s时停止输出,每一...

  • ns3使我哭泣

    小白试图装ns3 3.28版本一直报wifi module 错误 退回3.26就安装成功了 感觉是版本问题?(对的...

  • 适配Android6.0 WiFi扫描及获取WiFi列表

    适配Android6.0 WiFi扫描及获取WiFi列表 Demo链接: https://github.com/w...

  • 在ns3中安装crypto++

    希望在ns3仿真中使用一些密码学算法。以下为整理的安装过程,来源见参考文献。在ubuntu18.04可行。 安装c...

网友评论

      本文标题:ns3仿真demo(third.cc(wifi))

      本文链接:https://www.haomeiwen.com/subject/svghkxtx.html