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"              //頭文件包含在build/ns3目錄下,頭文件裏包含的具體內容在ns-3.28/src中
#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;       //創建命名空間,省去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);   //設置客戶和服務器端的日誌級別爲“INFO”,當仿真產生數據的分組發送時,對應的應用就會輸出相應的日誌
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

  NodeContainer nodes;    //定義網絡節點對象,在仿真中代表計算機
  nodes.Create (2);          //生成2個網絡節點

  PointToPointHelper pointToPoint;    //創建一個PointToPointHelper類的對象
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));  //創建一個PointToPointNetDevice對象時,設置傳輸速率爲5Mbit/s。
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); //創建一個PointToPointChannel對象時,設置傳輸時延值爲2ms。

  NetDeviceContainer devices;  //聲明devices爲NetDeviceContainer類
  devices = pointToPoint.Install (nodes);  //爲每一個節點安裝點到點網絡設備,設備之間爲點到點信道,5Mbit/s以及2ms時延

  InternetStackHelper stack;     //爲計算機安裝網絡協議棧,主要爲IP層
  stack.Install (nodes);
  Ipv4AddressHelper address;    //聲明節點設備IP地址
  address.SetBase ("10.1.1.0", "255.255.255.0");  //設置IP地址與子網掩碼
  Ipv4InterfaceContainer interfaces = address.Assign (devices);  //將地址與網絡設備關聯起來,創建了一個接口列表

  UdpEchoServerHelper echoServer (9);  //聲明服務器端口號
  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));  //在NodeContainer容器中索引爲1的節點上安裝一個UdpEchoServerApplication,返回一個容器,容器包含指向所有被助手創建的該節點的應用指針
  
  serverApps.Start (Seconds (1.0));  //服務器應用在1s生效
  serverApps.Stop (Seconds (10.0));  //服務器應用在10s失效

  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));   //在NodeContainer容器中索引爲0的節點上安裝一個UdpEchoClientApplication,返回一個容器,容器包含指向所有被助手創建的該節點的應用指針
  clientApps.Start (Seconds (2.0));     //客戶端應用在2s時生效
  clientApps.Stop (Seconds (10.0));   //客戶端應用在10s時失效

  Simulator::Run ();  //啓動模擬器
  Simulator::Destroy ();  //銷燬模擬器
  return 0;
}

NodeContainer類:爲ns-3中的一個Helper類,能一次操作多個節點。
Helper類:爲一簇類,幾乎所有的模塊都有一個或多個Helper類,負責把網絡設備連接到節點、信道,配置IP地址等任務。
PointToPointHelper類:負責設置網絡設備和信道屬性,並通過Install()方法把設備安裝到節點中。信道和網絡設備時對應的,比如以太網設備和無線信道就不能一起使用。
物理計算機連接:兩臺計算機成爲網絡一般來說需要使用網卡和網線。
ns-3中計算機物理連接:網卡抽象爲網絡設備,網線抽象爲信道。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章