本人也属于刚刚学习NS3的网络仿真,如有错误的地方,希望各位大神可以批评指正。
下面我将介绍自己搭建的一个P2P拓扑WiFi的脚本和一些在搭建过程中遇到的问题。
我是用的是NS3.34版本,基于例子third.cc历程进行更改。以下是代码
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/applications-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/internet-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/ssid.h"
#include "ns3/netanim-module.h"
#include "ns3/mobility-module.h"
// Default Network Topology
//
// 10.1.1.0S
// n0 -------------- n1
// point-to-point
//
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
bool tracing = false;
CommandLine cmd (__FILE__);
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
//创建两个p2p节点
NodeContainer p2pNodes;
p2pNodes.Create (2);
//为节点创建P2P类型的链路,并配置链路属性
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
//安装链路,生成网卡
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
//创建四个WiFi节点
NodeContainer wifiStaNodes;
wifiStaNodes.Create (4);
//将P2P的第0个节点,设置成为wifiAp节点
NodeContainer wifiApNode = p2pNodes.Get (0);
//默认传播延迟模型
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
//使用默认的PHY层配置和信道模型
YansWifiPhyHelper phy;
phy.SetChannel (channel.Create ());
//方法告诉助手类使用哪种速率控制算法,此处为AARF算法
WifiHelper wifi;
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
WifiMacHelper mac;
//创建IEEE 802.11服务集标识符(SSID)对象,用来设置MAC层的“SSID”属性值
Ssid ssid = Ssid ("ns-3-ssid");
//安装AP节点
mac.SetType ("ns3::ApWifiMac", //AP节点
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices = wifi.Install (phy, mac, wifiApNode);
//安装移动节点
mac.SetType ("ns3::StaWifiMac", //移动节点
"Ssid", SsidValue (ssid),
"ActiveProbing", BooleanValue (false));//助手类创建的MAC将不会发送探测请求
NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes);
//移动模块
MobilityHelper mobility;
//网格布局
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0), //起始X轴坐标
"MinY", DoubleValue (0.0), //起始Y轴坐标
"DeltaX", DoubleValue (5.0), //X轴上的节点间距
"DeltaY", DoubleValue (10.0), //Y轴上节点间距
"GridWidth", UintegerValue (5), //一行最多节点数
"LayoutType", StringValue ("RowFirst")); //布局类型
//wifiSta节点静止
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiStaNodes);
//P2P节点静止
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (p2pNodes);
//安装TCP/IP协议栈
InternetStackHelper stack;
stack.Install (p2pNodes);
stack.Install (wifiStaNodes);
//为网络设备分配IP地址
Ipv4AddressHelper address;
//安排P2P网段的地址
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
//安排WIFI网段的地址
address.SetBase ("10.1.3.0", "255.255.255.0");
address.Assign (staDevices);
address.Assign (apDevices);
//服务端设置端口号
UdpEchoServerHelper echoServer (9);
//在P2P节点1中安装服务端程序
ApplicationContainer serverApps = echoServer.Install ( p2pNodes.Get (1));
serverApps.Start (Seconds (2.0));
serverApps.Stop (Seconds (10.0));
//配置客户端程序属性
UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9); //绑定服务器地址,地址用interface.GetAddress方法获取
echoClient.SetAttribute ("MaxPackets", UintegerValue (100)); //发送多少个包
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); //发送间隔
echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); //包大小
ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (1)); //安装到哪个节点
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//仿真结束时间
Simulator::Stop (Seconds (10.0));
if (tracing)
{
phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
pointToPoint.EnablePcapAll ("third");
phy.EnablePcap ("third", apDevices.Get (0));
}
//生成动画
AnimationInterface anim("third.xml");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
我在编写代码的时候遇到了几个问题,1、wifi模块需要配置移动模型,移动模型可以是静止的,但是不配置的话数据会没有办法传输。2、是NS3的版本问题,我的参考资料比较老,有些代码在新版本里已经没有定义了,由于我也不会改模块所以只能查资料也无法确定是否正确。如:YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 如果使用的话就会报错因此我改成了: YansWifiPhyHelper phy; phy.SetChannel (channel.Create ());。 3、构建WiFi需要一个wifiAp节点,作为无线接入点,没有Ap节点也是不行的。
附上两个比较好的参考文章:(21条消息) NS3学习之整体介绍_特立独行的一只miao的博客-优快云博客