NS初学-first.cc

在ubuntu上安装了ns3后开始使用样例学习使用ns3,ns3使用c++编程还是让我学起来比较爽的

/* NS3关键的抽象概念
 节点、应用程序、信道、网络设备、拓扑生成器
*/
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
// 表明NS3代码风格
/*

* 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"//C++标准格式 这些模块头文件里更加详细的头文件

using namespace ns3;//ns3命名空间 类似于std

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");//声明了一个叫FirstScriptExample的日志构件

int

main (int argc, char *argv[])

{

Time::SetResolution (Time::NS);//设置仿真时间分辨率

LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);

LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);//设置这两个组件生效并设置回显clients和server的日志级别为”INFO”级

//使用生成器对象来帮助配置和管理潜在的对象
NodeContainer nodes;

nodes.Create (2);//创建两个节点

PointToPointHelper pointToPoint;//建立一个点对点的拓扑助手帮助生成点对点的连接

pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));

pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//设置pointToPoint属性

NetDeviceContainer devices;//创建一个NetDeviceContainer来容纳连接的设备(网卡)列表

devices = pointToPoint.Install (nodes);//依据上面属性pointToPoint给设备安装网卡并创建信道连接 具体描述可以去api文档中阅读

InternetStackHelper stack;//网络协议栈

stack.Install (nodes);//Aggregate implementations of the [ns3::Ipv4], [ns3::Ipv6], ns3::Udp, and ns3::Tcp classes onto the provided node.

Ipv4AddressHelper address;//声明了一个地址生成器对象,设置起始地址和掩码

address.SetBase ("10.1.1.0", "255.255.255.0");

Ipv4InterfaceContainer interfaces = address.Assign (devices);//把地址分配给node上的device 将ip地址和Interface联合封装好放入Ipv4InterfaceContainer 

UdpEchoServerHelper echoServer (9);//创建一个服务器程序,9是端口号

ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));//在1节点上创建UdpEchoServer这个应用程序然后返回return ApplicationContainer (InstallPriv (node));
//InstallPriv (node) 给node安装了udp回显服务程序并返回了指向安装程序的指针
serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);//创建一个客户端程序,利用刚才追踪ip的 Ipv4InterfaceContainer得到1接口的ip地址,即刚才安装服务器的节点,设定远程访问服务器端口号为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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,637评论 25 708
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,598评论 9 467
  • Kindle是亚马逊推出的电纸书阅读器,采用电子墨水屏幕,不伤眼睛而且又轻巧,阅读体验是任何手机或者平板电脑完全...
    爱阅读一辈子阅读 49,745评论 6 53
  • 窗外的天空浮着浅浅的云,似乎是多云的天气夹杂着丝丝的阴郁,糅杂在空气里的幽香由隐隐的思绪串起,透过玻璃有淡淡的阳光...
    虞茴阅读 341评论 0 0
  • 是怎样一种缘分,让你我遇见,是冥冥中的注 定,还是莫名的巧合?或许缘分本身就是一种奇妙而又无法解答的相遇。 ...
    浅卿君阅读 256评论 1 3