/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ //emacs模式,这行告诉源代码使用的预订格式(代码风格)
/*
* GNU---
* GPL---General Public License许可
* 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/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
using namespace ns3;//使用ns3命名空间
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");//这段代码的作用是向ns-3系统注册一个名为FirstScriptExample的记录组件,只有定义了记录组件才能在仿真脚本中使用Logging系统来定义输出
int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);设置时间单位为纳秒
//下面两行脚本是用来使2个日志组件生效的,它们被内建在Echo Client和Echo Server的应用中
//这两行代码将UdpEcho应用程序的客户端和服务器端的日志级别设置为“INFO”级
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable ("FirstScriptExample", LOG_LEVEL_INFO);
NS_LOG_INFO("Create Simulator");//输出该字符串
//LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_FUNCTION);
//LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_FUNCTION);
NodeContainer nodes;//创建ns3节点对象
nodes.Create (2);//它们在仿真中代表计算机创建两个节点
PointToPointHelper pointToPoint;//初始化一个PointToPointHelper的对象pointToPoint
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//DataRate属性值为”5Mbit/s“
//从上层的角度告诉PointToPointHelper对象当创建一个PointToPointNetDevice对象时使用”5Mbit/s“来作为数据速率
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
//Delay属性告诉PointToPointHelper使用“2ms”作为没一个被创建的点到点信道的传输延迟值
//下面两行代码完成信道和设备的配置
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 (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
//下面设置客户端应用层
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
//AsciiTraceHelper ascii;//使用AsciiTraceHelper生成ASCII格式的包
//pointToPoint.EnableAsciiAll(ascii.CreateFileStream("myfirst.tr"));
pointToPoint.EnablePcapAll("myfirst");//生成pcap格式的包
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
NS3之first.cc注解
最新推荐文章于 2022-07-28 11:23:11 发布