NS3网络仿真(6): 总线型网络
网络 仿真 总线
2023-09-11 14:14:44 时间
快乐虾
http://blog.csdn.net/lights_joy/
欢迎转载。但请保留作者信息
在NS3提供的第一个演示样例first.py中,模拟了一个点对点的网络,接下来的一个演示样例代码模拟了一个总线型网络及CSMA协议。
# // Default Network Topology # // # // 10.1.1.0 # // n0 -------------- n1 n2 n3 n4 # // point-to-point | | | | # // ================ # // LAN 10.1.2.0
仅仅只是NS3提供的演示样例代码是C++的,我们用Python改写它。
和first.py一样,我们先构造n0-n1之间的点对点连接:
# 构建点对点连接 p2pNodes = ns.network.NodeContainer() p2pNodes.Create (2) pointToPoint = ns.point_to_point.PointToPointHelper() pointToPoint.SetDeviceAttribute ("DataRate", ns.core.StringValue ("5Mbps")) pointToPoint.SetChannelAttribute ("Delay", ns.core.StringValue ("2ms")) p2pDevices = pointToPoint.Install (p2pNodes)
再构造n1-n4之间的总线型连接:
# 构建总线连接 nCsma = 3 csmaNodes = ns.network.NodeContainer() csmaNodes.Add (p2pNodes.Get (1)) csmaNodes.Create (nCsma) csma = ns.csma.CsmaHelper() csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("100Mbps")) csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.NanoSeconds (6560))) csmaDevices = csma.Install (csmaNodes)
接下来为每一个节点安装协议栈。这里须要注意的是每一个节点仅仅能安装一次,这也是这里使用p2pNodes.Get(0)的原因:
# 安装协议栈 stack = ns.internet.InternetStackHelper() stack.Install (p2pNodes.Get(0)) stack.Install (csmaNodes)
接下来为每一个节点配置IP:
# 配置IP address = ns.internet.Ipv4AddressHelper() address.SetBase ( ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0")) p2pInterfaces = address.Assign (p2pDevices) address.SetBase ( ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0")) csmaInterfaces = address.Assign (csmaDevices)
接着配置要模拟执行的Echo服务和客户端:
# 配置应用程序 echoServer = ns.applications.UdpEchoServerHelper (9) serverApps = echoServer.Install (csmaNodes.Get (nCsma)) serverApps.Start (ns.core.Seconds (1.0)) serverApps.Stop (ns.core.Seconds (20.0)) echoClient = ns.applications.UdpEchoClientHelper (csmaInterfaces.GetAddress (nCsma), 9) echoClient.SetAttribute ("MaxPackets", ns.core.UintegerValue (5)) echoClient.SetAttribute ("Interval", ns.core.TimeValue (ns.core.Seconds (1.0))) echoClient.SetAttribute ("PacketSize", ns.core.UintegerValue (1024)) clientApps = echoClient.Install (p2pNodes.Get (0)) clientApps.Start (ns.core.Seconds (2.0)) clientApps.Stop (ns.core.Seconds (20.0))
因为我们使用了两个网段,因而须要配置路由:
# 全局路由管理器依据节点产生 的链路通告为每一个节点建立路由表 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
此后我们就能够開始仿真了:
# 開始仿真 ns.core.Simulator.Run() ns.core.Simulator.Destroy()
此时我们的脚本还没有不论什么输出,我们让NS3生成能够在NetAnim中使用的xml文件,在run前加入以下的脚本:
anim = ns.netanim.AnimationInterface('second.xml') anim.SetConstantPosition(p2pNodes.Get(0), 10, 10) anim.SetConstantPosition(csmaNodes.Get(0), 30, 10) anim.SetConstantPosition(csmaNodes.Get(1), 40, 10) anim.SetConstantPosition(csmaNodes.Get(2), 50, 10) anim.SetConstantPosition(csmaNodes.Get(3), 60, 10)
执行此脚本后能够在项目路径下生成second.xml文件,在NetAnim下打开:
再让NS3保存模拟的数据包:
pointToPoint.EnablePcapAll ("second"); csma.EnablePcap ("second", csmaDevices.Get (1), True)
用etherreal打开生成的pcap文件,有点问题:
生成的数据包校验错误。校验码全为0。这是因为NS3没有打开Checksum的缘故,在脚本中加上一行:
ns.core.GlobalValue.Bind("ChecksumEnabled",ns.core.BooleanValue (True))
再执行此脚本:
相关文章
- 网络正常,但是网络图标上有黄色的三角图标
- NS3网络仿真(7): Wifi节点
- 使用ORL人脸库,通过GRNN网络和HOG特征提取的人脸识别算法matlab仿真
- 基于NSGAII多目标遗传优化的WSN无线传感器网络最优覆盖率matlab仿真
- 基于MATLAB的车载自组织网络(Vehicular Ad-hoc Network VANET)的通信系统的仿真
- 【MATLAB教程案例77】WSN网络覆盖率问题与matlab仿真
- 【MATLAB教程案例61】使用matlab实现基于ResNet残差网络的数据分类仿真分析
- 【MATLAB教程案例56】VGG16网络的MATLAB编程学习和实现,以步态识别为例进行仿真分析
- 基于LSTM网络的视觉识别matlab仿真
- 基于GA优化的OSPF协议网络路由matlab仿真
- 基于用户泊松随机分布通讯网络的voronoi图的matlab仿真
- 【车牌识别】基于HOG特征提取和GRNN网络的车牌识别算法matlab仿真
- 【人脸识别】基于GoogleNet深度学习网络的人脸识别matlab仿真
- 【遗传优化BP网络】基于自适应遗传算法的BP神经网络的股票预测MATLAB仿真
- 基于随机接入代价的异构网络速率分配算法研究
- vCener 配置 vSan 网络
- 网络数据嗅探工具HexInject
- 《高性能Linux服务器构建实战:系统安全、故障排查、自动化运维与集群架构》——第2章 Linux网络安全运维 2.1 网络实时流量监测工具iftop
- 深入理解Linux网络技术内幕(六)——PCI层和网络接口卡
- 记一次网络举报成功案例——中央网信办举报中心
- 用Python构建贝叶斯信念网络解决Monty Hall三门问题
- 2016年这些网络新贵或被并购
- 网络远程视频会议系统方案发展趋势
- iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)