【学习笔记】ns3 官方例程first.cc的理解

Thou

官方文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"

using namespace ns3; //使用 ns3 的命名空间

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); //定义一个名为FirstScriptExample的日志组件

int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv); //接受 shell 参数

Time::SetResolution (Time::NS); //更改全局的 Time 对象时间分辨率为纳秒,所有时间都将转换为 ns
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); //日志组件记录 Udp 的服务器和客户端,记录等级为 INFO

NodeContainer nodes; //创建一个Node容器nodes
nodes.Create (2); //nodes容器创建两个Node

PointToPointHelper pointToPoint; //创建一个PtP NetDevice的 Helper
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); //将该 PtP 网络设备的速率设置为5Mbps,信道时延为 2ms

NetDeviceContainer devices; //创建一个 NetDevice 容器
devices = pointToPoint.Install (nodes); //将 PtP NetDevice 安装到 nodes 中,并将这组 PtP 的网络设备赋值给 devices

InternetStackHelper stack; //定义一个 internet 协议栈
stack.Install (nodes); //将该协议栈安装到 nodes 容器中的所有 node (的 NetDevice??)上

Ipv4AddressHelper address; //定义一个 ipv4 的 Helper,方便快速分配ipv4 address
address.SetBase ("10.1.1.0", "255.255.255.0"); //ipv4 网段为 10.1.1.0,子网掩码为 255.2555.255.0

Ipv4InterfaceContainer interfaces = address.Assign (devices); //为 NetDevice 分配 ip 地址并赋值给 interfaces

UdpEchoServerHelper echoServer (9); //设置 udp 服务器的端口为 9

ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); //在 node1 上安装 udp 服务器应用程序
serverApps.Start (Seconds (1.0)); //udp 服务器在 1s 时打开
serverApps.Stop (Seconds (10.0)); //在 10s 时关闭

UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9); //udp 客户端访问 node1 的 ip,端口为 9
echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); //运行传输的最大包数为 1
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); // 间隔 1s
echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); //每个包 1024 字节

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0)); //在 node0 上安装 udp 客户端应用程序
clientApps.Start (Seconds (2.0)); //udp 客户端在 2s 时打开
clientApps.Stop (Seconds (10.0)); //在 10s 时关闭

//以下是固定模式
Simulator::Run (); //模拟器开始运行
Simulator::Destroy (); //摧毁模拟器
return 0;
}

运行结果:

udp 客户端在2s 启动时向 udp 服务器发送了一个 1024 字节的数据包

尝试更改:

1
2
3
4
echoClient.SetAttribute ("MaxPackets", UintegerValue (5)); //运行传输的最大包数为 5
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); // 间隔 1s
echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); //每个包 1024 字节

运行结果:

udp 客户端在2s 启动时开始向 udp 服务器发送 1024 字节的数据包,每隔 1s 发送一个

  • 标题: 【学习笔记】ns3 官方例程first.cc的理解
  • 作者: Thou
  • 创建于 : 2023-10-19 06:52:45
  • 更新于 : 2024-12-19 15:27:27
  • 链接: https://blog.txgde.space/2023/10/19/【学习笔记】ns3 官方例程first.cc的理解/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
【学习笔记】ns3 官方例程first.cc的理解