网络拓扑结构如下:
Wifi 10.1.3.0
AP
* * * *
| | | | 10.1.1.0
n5 n6 n7 n0 -------------- n1 n2 n3 n4
point-to-point | | | |
================
LAN 10.1.2.0
Wifi 10.1.3.0
AP
* *
* *
* *
* *
* * Wifi 10.1.1.0
STA 1 STA 2
10.1.1.2 10.1.1.3
| |
| |
+-----------------+
CSMA
以下是代码的主要部分及其含义:
引入所需的头文件,包括不同的ns-3模块和类。定义日志组件和主函数。
解析命令行参数,包括nCsma(CSMA节点数量)、nWifi(WiFi节点数量)、verbose(是否打印详细信息)和tracing(是否启用数据包跟踪)等。
创建节点容器和设备容器,用于存储网络拓扑中的节点和设备。
创建点对点连接,并配置数据速率和延迟等属性。
创建CSMA总线连接,并配置数据速率和延迟等属性。
创建WiFi连接,并配置物理层和MAC层属性,包括信道、SSID和设备类型等。
配置节点的移动性模型和位置。
安装Internet协议栈(InternetStack)。
配置IP地址和子网掩码,并为节点分配IP接口。
创建UDP回显服务器和客户端应用程序,并设置其属性,如端口、数据包大小和发送间隔等。
配置全局路由表。
配置数据包跟踪(如果启用)。
运行和销毁模拟器。
该示例代码配置了一个简单的网络拓扑,包括通过点对点连接和CSMA总线连接的节点,以及通过无线WiFi连接的节点。它还配置了UDP回显服务器和客户端应用程序,以在网络中发送和接收数据。通过模拟器运行该代码,可以模拟网络中节点之间的通信和数据传输行为,并收集数据包跟踪(如果启用)以进行分析。
#include "ns3/applications-module.h" // 引入应用程序模块
#include "ns3/core-module.h" // 引入核心模块
#include "ns3/csma-module.h" // 引入CSMA模块
#include "ns3/internet-module.h" // 引入Internet模块
#include "ns3/mobility-module.h" // 引入移动性模块
#include "ns3/network-module.h" // 引入网络模块
#include "ns3/point-to-point-module.h" // 引入点对点模块
#include "ns3/ssid.h" // 引入SSID类
#include "ns3/yans-wifi-helper.h" // 引入YansWifiHelper类
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("ThirdScriptExample"); // 定义日志组件
int main(int argc, char* argv[]) {
bool verbose = true; // 是否打印详细信息
uint32_t nCsma = 3; // CSMA节点数量
uint32_t nWifi = 3; // WiFi节点数量
bool tracing = false; // 是否启用数据包跟踪
CommandLine cmd(__FILE__); // 创建命令行对象
cmd.AddValue("nCsma", "Number of \"extra\" 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.AddValue("tracing", "Enable pcap tracing", tracing);
cmd.Parse(argc, argv); // 解析命令行参数
if (nWifi > 18) { // 若WiFi节点数量超过18个,输出错误信息并返回
std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl;
return 1;
}
if (verbose) {
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); // 设置日志级别为INFO
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO); // 设置日志级别为INFO
}
NodeContainer p2pNodes; // 创建点对点连接的节点容器
p2pNodes.Create(2); // 创建两个节点
PointToPointHelper pointToPoint; // 创建点对点连接助手
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps")); // 设置数据速率
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms")); // 设置延迟
NetDeviceContainer p2pDevices; // 创建点对点连接的设备容器
p2pDevices = pointToPoint.Install(p2pNodes); // 安装点对点连接
NodeContainer csmaNodes; // 创建CSMA总线连接的节点容器
csmaNodes.Add(p2pNodes.Get(1)); // 将第二个点对点连接的节点添加到CSMA节点容器中
csmaNodes.Create(nCsma); // 创建其他CSMA节点
CsmaHelper csma; // 创建CSMA总线连接助手
csma.SetChannelAttribute("DataRate", StringValue("100Mbps")); // 设置数据速率
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560))); // 设置延迟
NetDeviceContainer csmaDevices; // 创建CSMA总线连接的设备容器
csmaDevices = csma.Install(csmaNodes); // 安装CSMA总线连接
NodeContainer wifiStaNodes; // 创建WiFi连接的STA节点容器
wifiStaNodes.Create(nWifi); // 创建WiFi STA节点
NodeContainer wifiApNode = p2pNodes.Get(0); // 获取点对点连接的第一个节点作为WiFi AP节点
YansWifiChannelHelper channel = YansWifiChannelHelper::Default(); // 创建WiFi信道助手,默认信道模型
YansWifiPhyHelper phy;
phy.SetChannel(channel.Create()); // 设置物理层信道
WifiMacHelper mac;
Ssid ssid = Ssid("ns-3-ssid"); // 设置SSID
WifiHelper wifi;
NetDeviceContainer staDevices; // 创建STA设备容器
// 设置STA设备的无线MAC类型和参数
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
staDevices = wifi.Install(phy, mac, wifiStaNodes); // 在STA节点上安装WiFi设备
// 在接入点上安装WiFi设备
NetDeviceContainer apDevices;
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
apDevices = wifi.Install(phy, mac, wifiApNode);
// 为WiFi站点设置移动模型
MobilityHelper mobility;
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"));
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue(Rectangle(-50, 50, -50, 50)));
mobility.Install(wifiStaNodes); // 在WiFi站点上安装移动模型
// 为WiFi接入点设置移动模型
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(wifiApNode);
// 在所有节点上安装Internet协议栈
InternetStackHelper stack;
stack.Install(csmaNodes);
stack.Install(wifiStaNodes);
stack.Install(wifiApNode);
// 为CSMA设备分配IP地址
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign(csmaDevices);
// 为WiFi设备分配IP地址
address.SetBase("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer staInterfaces;
staInterfaces = address.Assign(staDevices);
address.Assign(apDevices);
// 创建UDP回显服务器应用程序
UdpEchoServerHelper echoServer(9);
// 在接入点上安装UDP回显服务器应用程序
ApplicationContainer serverApps = echoServer.Install(wifiApNode.Get(0));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
// 创建UDP回显客户端应用程序
UdpEchoClientHelper echoClient(staInterfaces.GetAddress(0), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(1));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
// 在所有站点上安装UDP回显客户端应用程序
ApplicationContainer clientApps = echoClient.Install(wifiStaNodes);
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
// 如果指定了跟踪,启用pcap跟踪
if (tracing)
{
pointToPoint.EnablePcapAll("third");
csma.EnablePcapAll("third");
phy.EnablePcap("third", apDevices.Get(0));
}
Simulator::Stop(Seconds(10.0));
// 如果启用了详细输出,启用Ascii输出
if (verbose)
{
pointToPoint.EnableAsciiAll(std::cout);
csma.EnableAsciiAll(std::cout);
phy.EnableAscii("third", apDevices.Get(0));
}
Simulator::Run();
Simulator::Destroy();
return 0;
}
在网络拓扑中,点对点节点容器、CSMA节点和Wi-Fi节点是不同类型的节点,具有不同的特点和功能。
点对点节点容器: 点对点节点容器用于管理点对点连接的节点。点对点连接是一种直接连接两个节点的网络连接方式,通过专用的物理或逻辑链路将两个节点连接在一起。在点对点连接中,通信只发生在连接的两个节点之间,不涉及其他节点的中转或共享。点对点节点容器提供了对点对点连接节点的集中管理和配置,可以指定连接的节点之间的参数和属性。
CSMA节点: CSMA(Carrier Sense Multiple Access)节点是使用CSMA协议进行通信的节点。CSMA是一种共享介质的数据通信协议,允许多个节点在同一信道上竞争发送数据。CSMA节点通常用于共享媒体的网络环境,如以太网,其中多个节点共享同一物理介质进行通信。
Wi-Fi节点: Wi-Fi节点是使用Wi-Fi技术进行通信的节点。Wi-Fi是一种无线网络技术,允许设备通过无线信号进行通信。Wi-Fi节点通常用于无线局域网(WLAN)环境,其中设备可以通过无线接入点(如Wi-Fi路由器)连接到网络。
区别总结如下:
- 点对点节点容器用于管理点对点连接的节点,而CSMA节点和Wi-Fi节点是不同类型的通信协议和网络技术下的节点。
- CSMA节点适用于共享介质的网络环境,如以太网,而Wi-Fi节点适用于无线局域网环境。
- CSMA节点和Wi-Fi节点都可以与其他节点进行通信,但它们的通信方式和规则可能有所不同。
此处在使用流量监控器时,发现收发的速率无法显示,这个问题待解决
评论区