一、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
*/
#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;
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);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("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));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
二、学习内容
1、生成网络节点
生成两个网络节点
NodeContainer nodes;
nodes.Create (2);
2、创建物理连接
实际中物理连接两台计算机成为网络一般来说需要使用网卡和网线,ns3中将对应的物理实体抽象为网络设备和信道2个概念。
PointToPointHelper类负责挂载网络设备和设置信道属性。NetDeviceContainer设置设备容器
再利用PointToPointHelper的install方法挂载到网络信道上。
2.1 设置端到端属性
设置端到端属性——DataRate和Delay,还有其他属性见PointToPointHelper的其他属性(可查阅官网API)
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
2.2 创建设备群
NetDeviceContainer devices;
2.3 将连接挂载到设备上
devices = pointToPoint.Install (nodes);
一个PointToPointChannel对象被创建,2个 PointToPointNetDevices与之连接。当PointToPointHelper对象创建时,那些在Helper中就被预先设置的属性(DataRate、Delay等)被用来初始化对象对应的属性值。
3、安装协议栈
3.1 为节点安装IP协议
InternetStackHelper类,为每个节点容器(nodes)中的每个节点安装一个网络协议栈,主要是IP层
InternetStackHelper stack;
stack.Install (nodes);
3.2 设置IP地址
Ipv4AddressHelper为节点上的设备设置IP地址。
3.2.1 地址分配
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
声明了一个地址助手对象,并且告诉它应该从10.1.1.0开始以子网掩码为255.255.255.0分配地址。地址分配默认是从1开始并单调增长。当执行实际的地址分配时,唯一用户可见的API是设置基IP地址和子网掩码。
3.2.2 地址关联设备
使用 Ipv4Interface对象将一个IP地址同一个设备关联起来。
Ipv4InterfaceContainer提供了一个Ipv4Interface对象的列表
Ipv4InterfaceContainer interfaces = address.Assign (devices);
4、安装应用层
4.1 设置服务器端应用层
4.1.1 预设属性
使用UdpEchoServerHelper来帮助创建真正的应用对象。
UdpEchoServerHelper echoServer (9);
除非我们告知助手服务器和客户端所共知的一个端口号,否则这个助手是不会起任何作用的。这里将端口号设置为9。
可以使用setAttribute来设置端口号到另一个值
4.1.2 安装服务器端应用程序
echoServer.Install将会在管理节点的NodeContainer容器索引号为1的机节点上安装一个UdpEchoServerApplication。
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
安装会返回一个容器,这个容器中包含了指向所有被助手创建的应用指针。
4.1.3 设置时间参数
应用对象需要一个时间参数来“开始”产生数据通信并且可能在一个可选的时间点“停止”。
时间点是用 ApplicationContainer的方法 Start和 Stop来设置的
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
在这种情况下,使用了一种C++转换序列来获得C++双精度(double)的1.0
并且用一个Seconds转换来把它转换到ns-3的Time对象。
这两行会使echo服务应用在1s时开始(生效)并在10s时停止(失效)。
既然已经声明了一个模拟事件(就是应用的停止事件)在10s时被执行,模拟至少会持续10 s。
4.2 设置客户端应用层
4.2.1 预设属性
客户端应用的设置与服务器端类似。使用UdpEchoClientHelper管理UdpEchoClientApplication。
但是,客户端需要设置五个属性:
- RemoteAddress
- RemotePort
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
- MaxPackets属性告诉客户端所允许它在模拟期间能发送的最大数据分组个数。
- Interval属性告诉客户端在2个数据分组之间要等待多长时间
- PacketSize属性告诉客户端它的数据分组应该承载多少数据。本例中让客户端发送一个1024byte的数据分组。
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
4.2.2 安装客户端应用程序
使用ApplicationContainer将node与UdpEchoClientApplication关联起来。
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
4.2.3 设置时间参数
设置开始和停止。但是这里本文使客户端在服务端生效1s后才开始(在模拟器中时间2s的时候)。
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
5、启用模拟器
这是用全局变量来启用模拟器
Simulator::Run ();
当Simulator::Run被调用时,系统会开始遍历预设事件的列表并执行。
首先它会在1.0 s时运行事件,这个事件会使echo服务端应用生效。接下来仿真器会运行在t=2.0s时的事件,即让echo客户端应用开始。同样地,这个事件可能会预定更多的其他事件。
在 echo客户端应用中,开始事件的执行会通过给服务端传送一个数据分组来开始仿真的数据传送阶段。发送一个数据分组给服务端会引发一系列更多的事件。这些事件会被预设在此事件之后,并根据已经在脚本中设定的时间参数来执行数据分组的应答。我们只发送了一个数据分组,在此之后,那个被单独的客户端应答请求所引发的连锁反应会停止,并且模拟器会进入空闲状态。
当这发生时,接下来的事件就是服务端和客户端的Stop事件。当这些事件被执行后,就没有将来的事件来执行了,函数Simulator::Run会返回。整个模拟过程结束。
六、清理
下面剩下的事情就是清理。清理通过调用全局函数来完成,助手函数被执行后,助手安排的钩子函数就被插入到模拟器中来销毁所有被创建的对象,ns-3系统会帮你料理繁杂的任务。
Simulator::Destroy ();
三、代码运行
把脚本放到scratch目录下,并运行waf,这样脚本就会被编译执行。
四、tcp拥塞控制源码
在ns3中就有相应一些简单的tcp拥塞控制代码源码,例如:westwood、veno、vegas。在ns3文件的src中: