快乐虾

http://blog.csdn.net/lights_joy/

欢迎转载,但请保留作者信息

在上一节中。我们仿真了一个总线型网络,这一节尝试将上一节中的n0变成一个无线的AP。再连上几个节点。这也是NS3中的演示样例third.cc干的事情。仅仅是我们用Python实现。

// Default Network Topology
//
// Wifi 10.1.3.0
// AP
// * * * *
// | | | | 10.1.1.0
// n5 n6 n7 n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0

与上一节一样,先构造p2p网络。再构建总线型网线:

# 构建点对点连接
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) # 构建总线连接
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)

接着构建无线网络:

# 构建Wifi连接
nWifi = 3
wifiStaNodes = ns.network.NodeContainer()
wifiStaNodes.Create (nWifi)
wifiApNode = p2pNodes.Get (0) channel = ns.wifi.YansWifiChannelHelper.Default ()
phy = ns.wifi.YansWifiPhyHelper.Default ()
phy.SetChannel (channel.Create ())

接着配置AP:

# 配置AP
wifi = ns.wifi.WifiHelper.Default ()
wifi.SetRemoteStationManager ("ns3::AarfWifiManager") mac = ns.wifi.NqosWifiMacHelper.Default () ssid = ns.wifi.Ssid ("ns-3-ssid")
mac.SetType ("ns3::StaWifiMac",
"Ssid", ns.wifi.SsidValue (ssid),
"ActiveProbing", ns.core.BooleanValue (False)) staDevices = wifi.Install (phy, mac, wifiStaNodes) mac.SetType ("ns3::ApWifiMac",
"Ssid", ns.wifi.SsidValue (ssid)) apDevices = wifi.Install (phy, mac, wifiApNode);

接着配置无线节点的位置參数:

# 配置无线节点的位置
mobility = ns.mobility.MobilityHelper() mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue (0.0),
"MinY", ns.core.DoubleValue (0.0),
"DeltaX", ns.core.DoubleValue (5.0),
"DeltaY", ns.core.DoubleValue (10.0),
"GridWidth", ns.core.UintegerValue (3),
"LayoutType", ns.core.StringValue ("RowFirst")) mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", ns.mobility.RectangleValue (ns.mobility.Rectangle (-50, 50, -50, 50)))
mobility.Install (wifiStaNodes) mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel")
mobility.Install (wifiApNode)

接着安装协议栈:

# 安装协议栈
stack = ns.internet.InternetStackHelper()
stack.Install (csmaNodes)
stack.Install (wifiApNode)
stack.Install (wifiStaNodes)

配置IP,这个和上一节一样,仅仅是加上10.1.3.0网段而已:

# 配置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) address.SetBase (
ns.network.Ipv4Address("10.1.3.0"),
ns.network.Ipv4Mask("255.255.255.0"))
address.Assign (staDevices)
address.Assign (apDevices)

接下来模拟一个Echo服务,这个与上一节同样。仅仅是Client安装在了Wifi节点上。

# 配置应用程序
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 (wifiStaNodes.Get (nWifi - 1))
clientApps.Start (ns.core.Seconds (2.0))
clientApps.Stop (ns.core.Seconds (20.0))

接下来的部分与上一节差点儿全然同样。仅仅是加上了Simulator.Stop,由于假设没有这个函数调用,那么将导致Simulator.Run永远不会停止:

# 全局路由管理器依据节点产生 的链路通告为每一个节点建立路由表
ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables() ns.core.Simulator.Stop (ns.core.Seconds (10.0)); pointToPoint.EnablePcapAll ("third");
csma.EnablePcap ("third", csmaDevices.Get (1), True)
phy.EnablePcap ("third", apDevices.Get (0)) anim = ns.netanim.AnimationInterface('third.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)
anim.EnablePacketMetadata(True) # 開始仿真
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()

看看NetAnim显示的仿真结果:

再看看third-0-1.pcap的内容:

如我们所愿。802.11协议。呵呵~~~~~



NS3网络仿真(7): Wifi节点的更多相关文章

  1. NS3网络仿真(6): 总线型网络

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载.但请保留作者信息 在NS3提供的第一个演示样例first.py中,模拟了一个点对点的网络,接下来的一个演示样例代码模 ...

  2. NS3网络仿真(2):first.py

    1    安装基本模块 11  安装Python 12  安装PTVS 13  加入对python-279的支持 2    在vs2013下编译NS3 3    编译NetAnim 4    在vs2 ...

  3. NS3网络仿真(3): NetAnim

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在NS3提供的演示样例first.py中,并没有生成NetAnim所须要的xml文件,本节我们尝试 ...

  4. NS3网络仿真(12): ICMPv4协议

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 ICMP的全称是 Internet ControlMessage Protocol . 其目的就是 ...

  5. NS3网络仿真(9): 构建以太网帧

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在NS3使用了一个叫Packet的类来表示一个数据帧,本节尝试用它构造一个以太网帧. 以下是一个典 ...

  6. NS3网络仿真(11): ARP

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 ARP(Address ResolutionProtocol,地址解析协议)协议的基本功能就是通过 ...

  7. NS3网络仿真(4): DataRate属性

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在first.py中创建了一个点到点的信道,且配置了两个属性: pointToPoint = ns ...

  8. NS3网络仿真(10): 解析以太网帧

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载.但请保留作者信息 解析以太网帧的过程是构建以太网帧的逆过程,当我们接收到一个以太网帧时,仍然以上一节中的ARP帧为例 ...

  9. NS3网络仿真(5): 数据包分析

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在我们生成的xml文件里.是不包括生成的数据包的数据的,在我们的脚本中加入以下的语句: point ...

随机推荐

  1. ProxyHandler处理器__代理设置__自定义opener

    ProxyHandler处理器(代理设置) 使用代理IP,这是爬虫/反爬虫的第二大招,通常也是最好用的. 很多网站会检测某一段时间某个IP的访问次数(通过流量统计,系统日志等),如果访问次数多的不像正 ...

  2. Struts 2 动作注释 配置动作扩展 全局开关

    动作注释package com.yiibai.user.action; import org.apache.struts2.convention.annotation.Action; import o ...

  3. linux下防火墙iptables原理及使用

    iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵的商业防火 ...

  4. 在web中绘制图像 -- canvas篇

    汗,不小心点击发布了.其实正在编辑中...... HTML Canvas是现代浏览器中非常棒的绘图技术,Canvas可以绘制图形,操作图片,制作游戏,创建动画等:Canvas是很容易使用的,下面我们来 ...

  5. Service Broadcast简单音乐播放功能

    在Activity上有两个ImageButton,分别控制播放/暂停.停止. @Override    public void onCreate(Bundle savedInstanceState) ...

  6. 划分树C++版百度百科模板

    此代码同hdu2665改编#include <iostream> #include <cstdio> #include<cstring> #include < ...

  7. CodeIgniter 防止XSS攻击

    CodeIgniter 包含了跨站脚本攻击的防御机制,它可以自动地对所有POST以及COOKIE数据进行过滤,或者您也可以针对单个项目来运行它.默认情况下,它 不会 全局运行,因为这样也需要一些执行开 ...

  8. 【bzoj4240】有趣的家庭菜园 贪心+树状数组

    题目描述 对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物.JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N.IOI草一共有N株,每个区域种植着一株.在第i个区域种 ...

  9. ORACLE无法删除当前连接用户

    今天在做Oracle数据库是遇到ORACLE无法删除当前连接用户 ,经查找可用如下方法解决 . 在Oracle中删除用户时提示:ORACLE无法删除当前连接用户  可以用以下语句    Sql代码   ...

  10. Spoj-BITDIFF Bit Difference

    Given an integer array of N integers, find the sum of bit differences in all the pairs that can be f ...