zoukankan      html  css  js  c++  java
  • ns3 myfirst.cc 两个节点点对点通信

    首先在ns3.25/examples/tutorial/下找到 first.cc文件,将他拷贝到到scratch目录下。

    然后为了方便将代码打出来

    /* -*- 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[])
    {
      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 (2));
      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;
    }

    首先研究头文件

    #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"
    

     这些头文件都被存放在build目录下的一个ns3的目录下,我们可以根据自己所需要的功能引用相应的头文件进来

    using namespace ns3;
    

     ns3的命名空间,这样我们就可以不用再写 ns3::xxx ,否则会编译错误。

    NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
    

     日志组件,这句话一看上去有点蒙,实际上的功能就是 生成一个名字为“FirstScriptExample"的日志组件。

    Time::SetResolution (Time::NS);
    

     设置时间单位为纳秒,虽然我不是很明白为什么要设置时间单位为纳秒??这个是百度得到的 存在疑点

      LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
      LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
    

     是两个日志组件生效,并且日志级别设置为INFO,这个两个日志是内建在Echo Client和Echo Sever的应用中。等下会有用到。

      NodeContainer nodes;
      nodes.Create (2);
    

     这句话差不多就是 声明一个节点容器 nodes,然后在李勇Create(2)生成两个节点。

      PointToPointHelper pointToPoint;
      pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
      pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
    

     这个PointToPointHelper类负责来设置网络设备和信道属性。

    其中把网络设备中“DateRate"数据速率设置为5Mbps,信道属性的延迟设置为2ms.  这个等下会在后面的生成结果,以及自己所学网络知识解释一下他用在什么地方。

      NetDeviceContainer devices;
      devices = pointToPoint.Install (nodes);
    

     前面已经完成了用NodeContainer来创建节点,用PointToPointHelper来做关于创建、配置、安装的工作。现在他们两个之间执需要连接起来。需要一个NetDevices对象列表,可以通过NetDeviceContainer来存放他们,就像使用第一个NodeContainer对象来存放自身所创建的节点一样。

    PointToPointHelper的Install()方法一个NodeContainer对象作为一个参数,以一个NetDevice Container为返回对象。

    对于NodeContainer的每一个节点,因为这里建立的是点对点的信道,所以明确的2个节点。一个PointToPointChannel对象被创建,2个PointToPointNetDevices和他连接。

    所以最终就得到了,两个节点,每个节点安装了点到点网络设备,这两个网络设备之间安装了点到点的信道,2个设备会被配置在一个有2ms传输时延的信道上以5Mbps的速率传送数据。

      InternetStackHelper stack;
      stack.Install (nodes);
    

     InternetStackHelper会为NodeContainer的每一个Node安装一个网络协议。

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

     Ipv4AddressHelper为节点上的设备设置IP地址。通过SetBase方法告诉它从10.1.1.0开始以子网掩码为255.255.255.0分配地址,地址分配默认是从1开始单调增长。

      Ipv4InterfaceContainer interfaces = address.Assign (devices);
    

     这个代码完成了地址配置, ns3中使用Ipv4Interface对象将一个IP地址同一个设备关联起来。而Ipv4InterfaceContainer提供了这样的功能,作为Ipv4Interface对象的容器。

    到这里已经完成了一个 安装了了协议栈,配置了IP地址类的点到点的网络。 现在就是要用它做数据通信。

      UdpEchoServerHelper echoServer (9);
    
      ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
      serverApps.Start (Seconds (1.0));
      serverApps.Stop (Seconds (10.0));
    

    声明了一个UdpEchoServerHelper 像其他Help类一样这个也不是应用本身,而是一个用来创建真正应用的对象,其中端口为9号端口。

    并在刚刚NodeContainer里面的2号节点 这里0为1号节点,1为2号节点。装入服务器应用,同样的这个的返回是一个Application对象,需要一个ApplicationContainer作为容器来存储

    应用对象需要一个时间参数 ,开始以及停止 ,这里两行使得echo服务应用在1s时开始,在10s的时候停止。

     UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
      echoClient.SetAttribute ("MaxPackets", UintegerValue (2));
      echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
      echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
    

     同样的客户端应用,首先给客户端的 UdpEchoClientHelper类 传入要客户端要发送数据到的IP地址,以及端口号,这里设置的是服务器的设备IP地址,以及刚刚设置接受9号端口。

    MaxPackets为在模拟期间能发送的最多的分组个数

    Interval 为两个分组的要等待多少时间

    PacketSize 为每个分组承载多少数据。 这里的参数1024是字节,不是位!!!

    ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
    clientApps.Start (Seconds (2.0));
    clientApps.Stop (Seconds (10.0));
    

     然后再用同样的方法,把一号节点作为客户端,把客户端应用装在一号节点上,

    应用开始服务为2s,结束服务为10s

    至于为什么是2s开始,我也是有点蒙,查了有关资料,说是要等服务器生效1s后才开始作为服务比较合理。也可以通过改变上面参数为1,这是客户端就变成了1s的时候开始服务,就是1s时候开始发送数据。

      Simulator::Run ();
      Simulator::Destroy ();
    

     启动模拟器,Simulator::Run(),系统会开始遍历预设事件的列表并执行。

    执行完毕用,就调用Simulator::Destory()来销毁刚刚所创建的一系列东西。

    在eclipse下执行

    运行结果

    这是发送一个包,即客户端发送一个1024字节的分组到服务器,而这里客户端的端口我觉得应该是随机设定的,因为从头到尾就没有碰到过49153端口。

    而为什么是 2.00369s才收到 我做了一个运算 即在2s的基础上  2s+1024*8/(5*10^6)(发送时延)s+0.002s(传输时延)=2.0036384s 与20.00369s还有一定差距,可差距已经很小,所以个人觉得把那部分时延可以视为处理时延(这里不存在排队时延).

  • 相关阅读:
    1592:【例 1】国王
    状态压缩类动态规划笔记
    1300:鸡蛋的硬度
    1263:【例9.7】友好城市
    第四部分-并发编程案例分析4:高性能数据库连接池HikariCP
    容器基础3:容器镜像
    第四部分-并发编程案例分析3:高性能队列Disruptor
    容器基础2:隔离与限制
    第四部分-并发编程案例分析1:限流Guava RateLimiter
    容器基础1:进程
  • 原文地址:https://www.cnblogs.com/wpqwpq/p/5671177.html
Copyright © 2011-2022 走看看