当前位置:首页 > 编程笔记 > 正文
已解决

ns3入门基础教程

来自网友在路上 164864提问 提问时间:2023-11-06 19:08:10阅读次数: 64

最佳答案 问答题库648位专家为你答疑解惑

ns3入门基础教程

文章目录

    • ns3入门基础教程
      • ns环境配置
      • 测试ns3环境
      • ns3简单案例

ns环境配置

官方网站:https://www.nsnam.org/releases/
代码仓库:https://gitlab.com/nsnam/ns-3-dev
如果安装遇到问题,可以参考以下博文:
https://blog.csdn.net/yangzhenyu2/article/details/116205406
以及:
https://blog.csdn.net/rical730/article/details/71730163

测试ns3环境

首先编辑一个很简单的.cc文件,测试ns3环境是否安装成功,在./scrach/目录下新建一个.cc文件(first.cc),然后写入代码:

#include "ns3/core-module.h"using namespace ns3;int main(int argc, char *argv[]){std::cout<<"hello ns3"<<std::endl;Simulator::Run();Simulator::Destroy();return 0;
}

接下来进行编译:

./waf build

然后运行:

./waf --run scrach/first

运行结果:
在这里插入图片描述

ns3简单案例

首先展示代码以及运行结果,然后再给出该代码的详细讲解:

#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;
int main (int argc, char *argv[]){NodeContainer nodes;nodes.Create (2);PointToPointHelper pointToPoint;pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer devices;devices = pointToPoint.Install (nodes);InternetStackHelper stack;stack.Install (nodes);Ipv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer interfaces = address.Assign (devices);UdpEchoServerHelper echoServer (9);//echoServer(port number)ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);echoClient.SetAttribute ("MaxPackets", UintegerValue (1));echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));echoClient.SetAttribute ("PacketSize", UintegerValue (1024));ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));std::ostringstream oss("scratch/lab02");pointToPoint.EnablePcapAll (oss.str ());pointToPoint.EnableAsciiAll (oss.str ());Simulator::Run ();Simulator::Destroy ();
}

运行结果:
在这里插入图片描述
这段代码是一个C++程序,使用了NS-3网络模拟框架,用于模拟一个简单的点对点网络通信场景。以下是对代码的解读:

  1. 包含头文件:代码一开始包含了一系列NS-3库的头文件,这些头文件提供了必要的类和函数,以便创建和模拟网络拓扑和通信。

  2. 命名空间:使用using namespace ns3;将ns3命名空间引入到当前的代码作用域,以便在代码中使用NS-3库的类和函数,无需显式指定命名空间。

  3. main 函数:这是程序的主函数,它是程序的入口点。

  4. 创建节点(NodeContainer):通过NodeContainer类创建了一个名为 nodes 的节点容器,容器中包含两个节点(nodes.Create(2))。

  5. 创建点对点连接(PointToPointHelper):使用 PointToPointHelper 类创建了一个点对点连接的帮助器对象 pointToPoint,并配置了连接的数据速率和延迟属性。

  6. 安装网络设备:通过 pointToPoint.Install(nodes) 安装了点对点连接设备,并将设备存储在 devices 变量中。

  7. 安装网络协议栈(InternetStackHelper):通过 InternetStackHelper 类的 Install 方法,将网络协议栈安装到节点上。

  8. 分配 IPv4 地址:使用 Ipv4AddressHelper 类来分配 IPv4 地址。设置了基础地址为 “10.1.1.0” 和子网掩码为 “255.255.255.0”,然后通过 address.Assign(devices) 分配地址给连接设备,结果存储在 interfaces 变量中。

  9. 配置服务器应用(UdpEchoServerHelper):创建了一个 UDP 回显服务器的帮助器对象 echoServer,并指定了端口号 9。

  10. 安装服务器应用:通过 echoServer.Install(nodes.Get(1)) 将回显服务器应用安装在节点1上,然后设置了应用的启动时间和停止时间。

  11. 配置客户端应用(UdpEchoClientHelper):创建了一个 UDP 回显客户端的帮助器对象 echoClient,并配置了客户端的目标地址(使用 interfaces.GetAddress(1) 获取节点1的地址)和端口号 9。

  12. 安装客户端应用:通过 echoClient.Install(nodes.Get(0)) 将回显客户端应用安装在节点0上,并设置了应用的启动时间和停止时间。

  13. 启用网络数据包捕获和记录:使用 pointToPoint.EnablePcapAll()pointToPoint.EnableAsciiAll() 启用了对网络数据包的捕获和记录,并指定了记录文件的名称。

  14. 运行模拟:通过 Simulator::Run() 开始运行网络模拟。

  15. 销毁模拟:通过 Simulator::Destroy() 结束模拟并清理资源。

总的来说,这段代码创建了一个包含两个节点的点对点网络拓扑,其中一个节点充当服务器,另一个节点充当客户端,它们之间通过UDP回显应用进行通信。模拟运行时,网络数据包将被捕获和记录,以便进行分析和调试。

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"ns3入门基础教程":http://eshow365.cn/6-33857-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!