Examples

Tutorial

First

/* -*- 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
 */
 
/* 1.头文件 */
#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"

/* 2.名字空间 */
using namespace ns3;

/* 3.定义一个LOG模块 */
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

/* 4.主函数 */
int
main (int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse (argc, argv);/* 读取命令行参数 */
  
  Time::SetResolution (Time::NS);/* 最小单元时间 NS */
  /* 打印指定LOG组件信息 */
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
  
/* 5.创建网络拓扑 */
  NodeContainer nodes;
  nodes.Create (2);/* 创建2个网络节点 */

  PointToPointHelper pointToPoint;/* PPP助手类 */
  /* 设置PPP信道属性 */
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));/* 设置传输速率为5Mbps */
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));/* 设置信道传播时延为2ms */
  
  NetDeviceContainer devices;/* 创建网络设备 */
  devices = pointToPoint.Install (nodes);/* pointToPoint.Install (nodes)函数内共创建了2个PPP网络设备对象pointToPointDevice和1个PPP信道对象pointToPointChannel。连接节点与信道 */

/* 6.安装TCP/IP协议簇 */
  InternetStackHelper stack;
  stack.Install (nodes);/* 为nodes容器中的节点安装TCP/IP协议栈 */

  Ipv4AddressHelper address;/* 为网络设备分配IP地址。起始地址为10.1.1.0 */
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);/* 产生网络接口 */

/* 7.安装应用程序 */
  UdpEchoServerHelper echoServer (9);/* 监听9号端口 */

/* 使用Install方法将Application安装在节点上。Application在第1秒开始运行并接受9号端口的数据,在第10秒停止 */
  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

/* 配置客户端属性 */
  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);/* 客户端类UdpEchoClientHelper在节点0创建一个客户端应用echoClient */
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));/* 最大发送分组个数,1 */
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));/* 分组发送间隔1秒 */
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));/* 分组负载字节大小,1024比特 */

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));/* 在模拟启动后2秒向节点1的9号端口发送一个1024比特的UDP数据包,在模拟启动后10秒停止 */

/* 8.启动与结束 */
  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

终端运行结果

./waf --run scratch/first

Waf: Entering directory `/home/dragon/repos/ns-3-dev/build'
Waf: Leaving directory `/home/dragon/repos/ns-3-dev/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (3.025s)
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9

alt