NS3教程系列:模拟P2P网络

定义和设计两个有线节点之间具有适当带宽和延迟的 p2p 网络

所需平台:

1. NS3

2. Tracemetrics 测量吞吐量(安装方法见文末)

3. NetAnim 显示动画效果

4. Gnuplot 绘制图像特征(安装方法见文末)

问题阐述:

设计两个有线节点,它们可以通过P2P网络相互连接,处理数据速率为 50mbps,延迟为 5ms第一个节点充当服务器,第二个节点充当客户端,它们在 20 秒的总模拟时间内交换至少 10 个数据包。最大数据包大小为 1024 字节和 512 字节。绘制上述数据包大小的每个节点的吞吐量

实验步骤:

1. 将目录中first.cc复制

ns-allinone-3.38/ns-3.38/examples/tutorial/

粘贴到

ns-allinone-3.38/ns-3.38/examples/scratch

2.用任意编辑器进行代码修改(我就用记事本了):
根据要求分别对延迟时间、最大数据包、总时间和报数量进行修改,


完整代码如下:

/*
Design two wired nodes that can connect to each other through a point-to-point network that handles a data rate of 50mbps and a delay of 5ms. 
The first node acts as a server and the second node acts as a client and they exchange at least 10 packets in a total simulation time of 20 seconds. 
The maximum packet size is 1024 bytes and 512 bytes. Plot the throughput of each node for the above packet sizes. (Use any charting solution you are comfortable with).
 */

#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"

#include "ns3/netanim-module.h"

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1
//    point-to-point
//

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("FirstScriptExample");

int
main(int argc, char* argv[])
{
    CommandLine cmd(__FILE__);
    cmd.Parse(argc, argv);

    Time::SetResolution(Time::NS);
    LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
    LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);

    NodeContainer nodes;
    nodes.Create(2);

    PointToPointHelper pointToPoint;
    pointToPoint.SetDeviceAttribute("DataRate", StringValue("50Mbps"));
    pointToPoint.SetChannelAttribute("Delay", StringValue("5ms"));

    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(8080);

/* nodes.Get(0) is the first node and the second node is
   nodes.Get(1)
   */
    ApplicationContainer serverApps = echoServer.Install(nodes.Get(0));
    serverApps.Start(Seconds(1.0));
    serverApps.Stop(Seconds(20.0));

    UdpEchoClientHelper echoClient(interfaces.GetAddress(0), 8080);
    echoClient.SetAttribute("MaxPackets", UintegerValue(10));
    echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
    echoClient.SetAttribute("PacketSize", UintegerValue(512));

    ApplicationContainer clientApps = echoClient.Install(nodes.Get(1));
    clientApps.Start(Seconds(2.0));
    clientApps.Stop(Seconds(20.0));

//For ascii trace
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll(ascii.CreateFileStream("512.tr"));

//NetAnim
AnimationInterface anim("p2p.xml");

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

3. 打开终端进入ns-3.38目录,执行

./ns3 run /scratch/first.cc

4.  分别将数据包容量修改为1024和512,对应的文件名也修改为1024.tr与512.tr,运行后会得到文件

5. 进入目录:

cd
cd ns-allinone-3.38/netanim-3.109

运行NetAnim

./NetAnim

6. 点击左上角的黄色小文件夹,导入刚刚生成的xml文件 (注意文件生成在了ns-3.38的目录下)

7. 点击绿色图标就可以运行,观察有线p2p网络的数据传输过程 

 8. 借助软件Tracemetrics观察吞吐量

cd
cd tracemetrics
java -jar tracemetrics.jar

 点击File-choose file选择ns-3,38目录中1024.tr或512.tr(我选1024.tr),点击“执行分析”

9. 在这里我们就可以看到所需要的各种信息:

 

10.由1024.tr和512.tr文件分析得到的吞吐量绘制对比图形  
编写一个文档记录几点吞吐量

cd

nano throughput.txt

0 957.28 492.27
1 957.28 492.27

创建绘图文件gun.plt,绘图代码如下

set terminal pdf
set output "throughput.pdf"
set title "Throughput Vs Packet Size"
set xlabel "Node Number"
set ylabel "Throughout in bps"
plot "throughput.txt" using 1:2 with linespoints title "1024","throughput.txt" using 1:3 with linespoints tiltle "512"

11. 执行指令 

gnuplot gnu.plt


 效果如上
如果没有安装gnuplot可以直接

sudo apt install gnuplot

11. 关于Tracemetrics安装
TraceMetrics download | SourceForge.net

下载安装包后解压到主目录下,我将其重命名为“tracemetrics”

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值