美文网首页
NS初学-first.cc

NS初学-first.cc

作者: 53324d792ce6 | 来源:发表于2016-03-08 20:06 被阅读170次

    在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;
    
    }
    
    

    相关文章

      网友评论

          本文标题:NS初学-first.cc

          本文链接:https://www.haomeiwen.com/subject/gsszkttx.html