/* -*- 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" //头文件包含在build/ns3目录下,头文件里包含的具体内容在ns-3.28/src中
#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"); //用特殊的名字定义一个日志组件
int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); //设置客户和服务器端的日志级别为“INFO”,当仿真产生数据的分组发送时,对应的应用就会输出相应的日志
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes; //定义网络节点对象,在仿真中代表计算机
nodes.Create (2); //生成2个网络节点
PointToPointHelper pointToPoint; //创建一个PointToPointHelper类的对象
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); //创建一个PointToPointNetDevice对象时,设置传输速率为5Mbit/s。
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); //创建一个PointToPointChannel对象时,设置传输时延值为2ms。
NetDeviceContainer devices; //声明devices为NetDeviceContainer类
devices = pointToPoint.Install (nodes); //为每一个节点安装点到点网络设备,设备之间为点到点信道,5Mbit/s以及2ms时延
InternetStackHelper stack; //为计算机安装网络协议栈,主要为IP层
stack.Install (nodes);
Ipv4AddressHelper address; //声明节点设备IP地址
address.SetBase ("10.1.1.0", "255.255.255.0"); //设置IP地址与子网掩码
Ipv4InterfaceContainer interfaces = address.Assign (devices); //将地址与网络设备关联起来,创建了一个接口列表
UdpEchoServerHelper echoServer (9); //声明服务器端口号
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); //在NodeContainer容器中索引为1的节点上安装一个UdpEchoServerApplication,返回一个容器,容器包含指向所有被助手创建的该节点的应用指针
serverApps.Start (Seconds (1.0)); //服务器应用在1s生效
serverApps.Stop (Seconds (10.0)); //服务器应用在10s失效
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)); //在NodeContainer容器中索引为0的节点上安装一个UdpEchoClientApplication,返回一个容器,容器包含指向所有被助手创建的该节点的应用指针
clientApps.Start (Seconds (2.0)); //客户端应用在2s时生效
clientApps.Stop (Seconds (10.0)); //客户端应用在10s时失效
Simulator::Run (); //启动模拟器
Simulator::Destroy (); //销毁模拟器
return 0;
}
NodeContainer类:为ns-3中的一个Helper类,能一次操作多个节点。
Helper类:为一簇类,几乎所有的模块都有一个或多个Helper类,负责把网络设备连接到节点、信道,配置IP地址等任务。
PointToPointHelper类:负责设置网络设备和信道属性,并通过Install()方法把设备安装到节点中。信道和网络设备时对应的,比如以太网设备和无线信道就不能一起使用。
物理计算机连接:两台计算机成为网络一般来说需要使用网卡和网线。
ns-3中计算机物理连接:网卡抽象为网络设备,网线抽象为信道。