1. 程式人生 > >ns3模擬無線Ad hoc 網路通訊

ns3模擬無線Ad hoc 網路通訊

ns3模擬無線Ad hoc 網路通訊

Ad hoc網路

Ad hoc網是一種多跳的、無中心的、自組織無線網路,又稱為多跳網(Multi-hop Network)、無基礎設施網(Infrastructureless Network)或自組織網(Self-organizing Network)。整個網路沒有固定的基礎設施,每個節點都是移動的,並且都能以任意方式動態地保持與其它節點的聯絡。在這種網路中,由於終端無線覆蓋取值範圍的有限性,兩個無法直接進行通訊的使用者終端可以藉助其它節點進行分組轉發。每一個節點同時是一個路由器,它們能完成發現以及維持到其它節點路由的功能。

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /*   * This program is free software; you can redistribute it and/or modify  
* it under the terms of the GNU General Public License version 2 as   * published by the Free Software Foundation;   *   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * GNU General Public License for more details.   *   * You should have received a copy of the GNU General Public License   * along with this program; if not, write to the Free Software   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   */   #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" #include "ns3/internet-module.h"   using namespace ns3;   NS_LOG_COMPONENT_DEFINE ( "AdHocExample" );   int main( int argc, char *argv[]) {      Time::SetResolution (Time::NS);        LogComponentEnable ( "AdHocExample" , LOG_LEVEL_INFO);       // LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_INFO);      LogComponentEnable ( "PacketSink" , LOG_LEVEL_ALL);        uint32_t nAdHoc=30;        CommandLine cmd;        cmd.AddValue ( "nAdHoc" , "Number of wifi ad devices" , nAdHoc);        cmd.Parse (argc,argv);        NodeContainer AdHocNode;      AdHocNode.Create(nAdHoc);        YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();      YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();      phy.SetChannel (channel.Create ());        WifiHelper wifi;      wifi.SetStandard(WIFI_PHY_STANDARD_80211a); //設定標準      wifi.SetRemoteStationManager( "ns3::ConstantRateWifiManager" , "DataMode" ,StringValue( "OfdmRate6Mbps" ));        NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();      mac.SetType ( "ns3::AdhocWifiMac" ,                     "Slot" , StringValue ( "16us" ));        NetDeviceContainer AdHocDevices;      AdHocDevices = wifi.Install(phy,mac,AdHocNode);        MobilityHelper mobility;      mobility.SetPositionAllocator ( "ns3::GridPositionAllocator" ,                                       "MinX" , DoubleValue (0.0),                                       "MinY" , DoubleValue (0.0),                                       "DeltaX" , DoubleValue (5.0),                                       "DeltaY" , DoubleValue (5.0),                                       "GridWidth" , UintegerValue (10),                                       "LayoutType" , StringValue ( "RowFirst" ));        mobility.SetMobilityModel ( "ns3::RandomWalk2dMobilityModel" ,                                   "Bounds" , RectangleValue (Rectangle (-500, 500, -500, 500)));      mobility.Install (AdHocNode);        InternetStackHelper Internet;      Internet.Install(AdHocNode);        Ipv4AddressHelper address;      address.SetBase( "195.1.1.0" , "255.255.255.0" );        Ipv4InterfaceContainer AdHocIp;      AdHocIp = address.Assign(AdHocDevices);        NS_LOG_INFO ( "Create Applications." );      uint16_t port = 9999;      OnOffHelper onOff1( "ns3::TcpSocketFactory" ,Address(InetSocketAddress(AdHocIp.GetAddress(0),port)));      onOff1.SetAttribute ( "OnTime" , StringValue ( "ns3::ConstantRandomVariable[Constant=1]" ));      onOff1.SetAttribute ( "OffTime" , StringValue ( "ns3::ConstantRandomVariable[Constant=0]" ));        ApplicationContainer apps1 = onOff1.Install(AdHocNode);      apps1.Start(Seconds(1.0));      apps1.Stop(Seconds(500.0));        PacketSinkHelper sinkHelper ( "ns3::TcpSocketFactory" , Address(InetSocketAddress (Ipv4Address::GetAny(), port)));      ApplicationContainer apps2 = sinkHelper.Install(AdHocNode.Get(0));        apps2.Start(Seconds(0.0));      apps2.Stop(Seconds(500.0));        Ipv4GlobalRoutingHelper::PopulateRoutingTables ();        Simulator::Stop(Seconds(500.0));      Simulator::Run();      Simulator::Destroy();        return 0;   }

 模擬一個1千平方米的,場景中分佈了30個adhoc 節點,其餘節點向0號節點不斷髮送資料。

 

模擬結果

兩張資料爆炸的圖。。。