标签:ns3 注释 示例 second.cc third.cc
<span style="font-size:18px;">#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/applications-module.h" #include "ns3/ipv4-global-routing-helper.h" // Default Network Topology拓扑分布 // // 10.1.1.0 // n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0 //n0和n1实现点到点通信,n1~n4是个csma有线局域网。网络可以实现n0经由n1,和n4进行通信。 using namespace ns3; NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");//定义日志组件 int main (int argc, char *argv[]) { bool verbose = true;//定义bool变量用于决定是否开启日志组件,默认开启 uint32_t nCsma = 3;//变量,用于定义csma网络中有多少个node CommandLine cmd; cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); //命令行参数,设置是否开启logging cmd.Parse (argc,argv); if (verbose) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); } nCsma = nCsma == 0 ? 1 : nCsma; //********网络拓扑部分********// //p2p链路的两个节点创建 NodeContainer p2pNodes; p2pNodes.Create (2); //创建csma网络节点 NodeContainer csmaNodes; csmaNodes.Add (p2pNodes.Get (1));//将p2p节点1加到csma网络中 csmaNodes.Create (nCsma); //设置p2p传输速率和信道延迟 PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); //安装p2p网卡到p2p网络节点 NetDeviceContainer p2pDevices; p2pDevices = pointToPoint.Install (p2pNodes); //设置csma传输速率和信道延迟,注意传输速率有channel指定,而非device属性。(CSMA不允许同一信道上有多个不同数据率的设备) CsmaHelper csma; csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install (csmaNodes); //安装协议 InternetStackHelper stack; stack.Install (p2pNodes.Get (0));//p2p的n0节点,n1节点在csma网络中安装协议栈 stack.Install (csmaNodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer p2pInterfaces; p2pInterfaces = address.Assign (p2pDevices); address.SetBase ("10.1.2.0" , "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign (csmaDevices); //********应用程序部分********// UdpEchoServerHelper echoServer (9);//服务器端口号 //将server服务器安装在csma网络的n4节点。假定nCsma=3 ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);//设定远程服务器的IP地址和端口号 echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); //********建立网络路由********// Ipv4GlobalRoutingHelper::PopulateRoutingTables (); //********开启pcap追踪********// //开启p2pHelper,csmaHelper类对象的pcap pointToPoint.EnablePcapAll ("second"); csma.EnablePcap ("second", csmaDevices.Get (1), true); Simulator::Run (); Simulator::Destroy (); return 0; }</span><span style="font-size:24px;"> </span>
<span style="font-size:18px;">#include "ns3/core-module.h" #include "ns3/point-to-point-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" // 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 //n0,n5-n7:wifi网络,n0为AP,n5,n6,n7为无线STA; n0和n1:p2p通信; n1-n4:csma网络。示例中实现了节点n7和n4通信。 using namespace ns3; NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); int main (int argc, char *argv[]) { bool verbose = true;//用于是否启用logging组件 uint32_t nCsma = 3;//设置nCsma和nWifi的额外STA个数 uint32_t nWifi = 3; CommandLine cmd; cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); cmd.Parse (argc,argv); if (nWifi > 18) { std::cout << "Number of wifi nodes " << nWifi << " specified exceeds the mobility bounding box" << std::endl; exit (1); } if (verbose) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); } //创建p2p链路2个节点 NodeContainer p2pNodes; p2pNodes.Create (2); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); NetDeviceContainer p2pDevices; p2pDevices = pointToPoint.Install (p2pNodes); //创建csma网络的4个节点。默认nCsma=3 NodeContainer csmaNodes; csmaNodes.Add (p2pNodes.Get (1)); csmaNodes.Create (nCsma); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install (csmaNodes); //*********创建wifi网络的4个节点,默认STA=3;**********// NodeContainer wifiStaNodes; wifiStaNodes.Create (nWifi);//创建STA NodeContainer wifiApNode = p2pNodes.Get (0);//设置wifi的AP //配置PHY和Channel通道助手 YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); phy.SetChannel (channel.Create ());//Channel对象与PHY对象关联 //MAC层配置============================== WifiHelper wifi = WifiHelper::Default (); wifi.SetRemoteStationManager ("ns3::AarfWifiManager");//使用AARF速率控制算法 NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();//non-Qos的MAC //配置wifi网络STA网络设备的MAC类型和基础设施网络的SSID Ssid ssid = Ssid ("ns-3-ssid");//IEEE 802.1服务集标识符对象 mac.SetType ("ns3::StaWifiMac", //设置为特定种类MAC层,non-AP "Ssid", SsidValue (ssid),//设置SSID "ActiveProbing", BooleanValue (false));//设置ActiveProbing,false意味着助手创建的mac将不会发送探测请求。 NetDeviceContainer staDevices;//STA 网络设备 staDevices = wifi.Install (phy, mac, wifiStaNodes);//wifi的STA安装PHY层和MAC层 //配置wifi网络AP网络设备的MAC类型和基础设施网络的SSID mac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid)); NetDeviceContainer apDevices; apDevices = wifi.Install (phy, mac, wifiApNode);//与STA共同的PHY特性 //********移动模型********// MobilityHelper mobility; mobility.SetPositionAllocator ("ns3::GridPositionAllocator", //网格位置分配器 "MinX", DoubleValue (0.0), "MinY", DoubleValue (0.0), "DeltaX", DoubleValue (5.0), "DeltaY", DoubleValue (10.0), "GridWidth", UintegerValue (3), "LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", //以随机游走的方式移动 "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); mobility.Install (wifiStaNodes); //安装到wifi网络的STA mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (wifiApNode);//安装到wifi网络的AP //安装协议栈 InternetStackHelper stack; stack.Install (csmaNodes); stack.Install (wifiApNode); stack.Install (wifiStaNodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer p2pInterfaces; p2pInterfaces = address.Assign (p2pDevices); address.SetBase ("10.1.2.0", "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign (csmaDevices); address.SetBase ("10.1.3.0", "255.255.255.0"); address.Assign (staDevices); address.Assign (apDevices); //安装应用 UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));//csma网络的最后节点作为服务器 serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);//设置客户机的远程服务器IP地址和端口号 echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1));//在随后建立的wifi STA上安装客户机 clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); //建立网络路由 Ipv4GlobalRoutingHelper::PopulateRoutingTables (); Simulator::Stop (Seconds (10.0));//无线接入点会一直产生信标,模拟器时间列表一直非空,仿真无法结束 // pointToPoint.EnablePcapAll ("third"); phy.EnablePcap ("third", apDevices.Get (0)); csma.EnablePcap ("third", csmaDevices.Get (0), true); Simulator::Run (); Simulator::Destroy (); return 0; } </span>
[1] ns-3project. ns-3 Software Tutorial. ns-3 project. 2015年2月26日
[2]《ns-3网络模拟器基础及应用》,人民邮电出版社,马春光 姚建盛,2014年1月
部分参考网络资料,未一一列举,敬请原谅!
(四)NS3中示例:second.cc和third.cc程序的注释
标签:ns3 注释 示例 second.cc third.cc
原文地址:http://blog.csdn.net/finded/article/details/45745859