first.cc内容如下:
/* -*- 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
*/
//点对点有线网络:first.cc--创建了两个节点的有线网络。链路层使用点对点协议(PPP)传输分组
//在文件ns-allinone-3.29/ns-3.29/examples/tutorial/first.cc
#include "ns3/core-module.h" //Core模块定义了NS3的核心功能(如模拟事件、事件调度)
#include "ns3/network-module.h" //network模块定义了NS3的基本网络组件(如网络结点、分组和地址)
#include "ns3/internet-module.h"//internet模块定义了NS3的TCP/IP协议栈
#include "ns3/point-to-point-module.h"//点对点模块
#include "ns3/applications-module.h"//Application模块定义了应用层分组收发模型(如贪婪模型、ON/OFF模型)
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); //允许在本脚本中使用Log系统中的宏定义打印辅助信息
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv); //读取命令行参数
Time::SetResolution (Time::NS); //最小模拟时间单位:ns
//开启Log组件,打印制定Log组件信息。也就是个日志定义
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes; //在node-container.h中有此类
nodes.Create (2); //使用结点容器创建两个结点,调用NodeContainer中void Creat(Unit_32 n)成员函数
PointToPointHelper pointToPoint; //PPP信道助手类
//配置PPP信道属性
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//传输速率 5Mbps
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); //信道传播延迟 2ms
NetDeviceContainer devices; //使用网络设备容器创建网络设备,
devices = pointToPoint.Install (nodes);//连接结点与信道
/*注意
*PointToPointHelper::Install()函数内部分别创建了两个PPP网络设备对象(PointToPointNetDevice)和一个PPP信道对象(PointToPointChannel)。
*这两个网络设备对象被分别安装在两个结点中,然后又共同连接至同一个信道对象。
*Install()函数的返回值是一个网络设备容器NetDeviceContainer对象。
*这个容器包含了为NodeContainer中所有结点所分配的NetDevice对象*/
InternetStackHelper stack; //InternetStackHelper是为结点安装TCP/IP协议栈的助手类
stack.Install (nodes); //为nodes容器中的结点安装TCP/IP协议栈
Ipv4AddressHelper address; //为网络设备分配IPv4地址
address.SetBase ("10.1.1.0", "255.255.255.0");
//助手类Ipv4AddressHelper以10.1.1.0为起始地址、255.255.255.0为网络掩码
//分别向两台主机结点分配 10.1.1.1和10.1.1.2两个IP地址。
Ipv4InterfaceContainer interfaces = address.Assign (devices);
//为网络设备NetDevice所分配的IPv4地址保存在IPv4接口Ipv4Interface对象中。
//以下为应用层
UdpEchoServerHelper echoServer (9);
//服务器端助手类UdpEchoServerHelper在结点1中创建了一个服务器端应用echoServer
//调用了UDP的bind()、RecvFrom()和sendTo()来监听、接收和发送UDP分组
//echoServer自模拟启动后1.0s开始监听并接收9号端口的数据
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// echoServer自模拟启动后1.0s开始监听并接收9号端口数据,于10.0s后停止
//客户端助手类UdpEchoClientHelper在结点0中创建了一个客户端应用echoClient
//调用bind()、connect()来建立与服务器端的UDP连接
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
//配置客户端程序属性
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));//最大发送数据个数
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));//分组发送间隔
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));//分组负载字节大小
//在结点0中安装客户端程序
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
//echoClient在模拟启动后2.0s向结点1的9号端口发送一个1024B的UDP数据包
//echoClient从9号端口接收到数据包后向echoClient返回一个相同大小的UDP数据包,于10.0s后停止
Simulator::Run (); //执行操作
Simulator::Destroy (); //清除操作
return 0;
}
在ns-3.29目录下且在root权限下执行./waf --run first
即可执行该脚本
运行结果如下:
参考:周迪之. 开源网络模拟器ns-3架构与实践