ns3+netanim仿真点对点有限网络first脚本

存档使用,ns3及netanim安装过程不赘述。

编译前的准备工作

sudo vi myfirst.cc 在myfirst.cc里面,加上头文件:

#include "ns3/netanim-module.h"

run前面加上三句代码( 第二三行是描绘两个节点的坐标,也可以不要):

AnimationInterface anim("myfirst.xml");
anim.SetConstantPosition(nodes.Get(0), 1.0, 2.0);
anim.SetConstantPosition(nodes.Get(1), 2.0, 3.0);

Simulator::Run();
Simulator::Destroy();
return 0

编译方法

ns3编译有两种方法(示例编译first)

  • 在ns-3.27(或其他的版本)目录下运行first模拟脚本(需要在编译时启动enable-examples选项)

./waf clean

./waf -d debug --enable-examples --enable-tests configure

./waf --run first

  • 将脚本放到scratch目录下

cp examples/tutorial/first.cc scratch/myfirst.cc //将脚本复制到scratch目录下

./waf --run scratch/myfirst

运行结果

'build' finished successfully (46m21.851s)
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9
  • 客户机在2秒的时候向IP为10.1.1.2的节点的第九号端口(port)发送了1024字节大小的数据包。
  • 10.1.1.2的这个服务器在2.00369秒的时候收到了来自10.1.1.1的端口为49153的1024字节大小的数据包。
  • 10.1.1.2的服务器在2.00369秒向10.1.1.1的49153号端口发送了1024字节的一个数据包。
  • 2.00737秒客户机(10.1.1.1)接到了来自10.1.1.2(服务器)的9号端口的一个1024字节的数据包。

仿真示例

  • 在ns-allinone-3.27/ns-3.27目录下用ls命令查看是否生成一个名字为myfirst.xml的文件
  • 在ns-allinone-3.27/netanim-3.108目录下重新启动NetAnim软件 ./NetAnim
  • 出现仿真界面后找到刚刚生成的myfirst.xml文件并Open

first.cc代码解读

参考资料 https://blog.csdn.net/qq_31676673/article/details/87992757

原文地址:https://www.cnblogs.com/poziiey/p/12631434.html