Lewis_Liu
/* -*- 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[])
{
 
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
 
  CommandLine cmd;//开启命令行参数解析
  cmd.Parse (argc, argv);

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

编译执行结果:

lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf
Waf: Entering directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
Waf: Leaving directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
\'build\' finished successfully (2.682s)

Modules built:
antenna                   aodv                      applications             
bridge                    buildings                 config-store             
core                      csma                      csma-layout              
dsdv                      dsr                       energy                   
fd-net-device             flow-monitor              internet                 
lr-wpan                   lte                       mesh                     
mobility                  mpi                       netanim (no Python)      
network                   nix-vector-routing        olsr                     
point-to-point            point-to-point-layout     propagation              
sixlowpan                 spectrum                  stats                    
tap-bridge                test (no Python)          topology-read            
uan                       virtual-net-device        visualizer               
wave                      wifi                      wimax                    

Modules not built (see ns-3 tutorial for explanation):
brite                     click                     openflow                 

lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf --run "scratch/myfirst --PrintHelp"
Waf: Entering directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
Waf: Leaving directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
\'build\' finished successfully (2.664s)
myfirst [Program Arguments] [General Arguments]

General Arguments:
    --PrintGlobals:              Print the list of globals.
    --PrintGroups:               Print the list of groups.
    --PrintGroup=[group]:        Print all TypeIds of group.
    --PrintTypeIds:              Print all TypeIds.
    --PrintAttributes=[typeid]:  Print all attributes of typeid.
    --PrintHelp:                 Print this help message.

lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf --run scratch/myfirstWaf: Entering directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
Waf: Leaving directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
\'build\' finished successfully (2.701s)
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9
lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ 
View Code

 一个小例子:

/* -*- 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[])
{
  uint32_t nPackets=1;
 
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
 
  CommandLine cmd;
  cmd.AddValue("nPackets","Number of packets to echo",nPackets);
  cmd.Parse (argc, argv);
  


  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 (nPackets));
  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;
}
View Code

执行结果:

lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf
Waf: Entering directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
[2079/2427] cxx: scratch/myfirst.cc -> build/scratch/myfirst.cc.1.o
[2399/2427] cxxprogram: build/scratch/myfirst.cc.1.o -> build/scratch/myfirst
Waf: Leaving directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
\'build\' finished successfully (5.375s)

Modules built:
antenna                   aodv                      applications             
bridge                    buildings                 config-store             
core                      csma                      csma-layout              
dsdv                      dsr                       energy                   
fd-net-device             flow-monitor              internet                 
lr-wpan                   lte                       mesh                     
mobility                  mpi                       netanim (no Python)      
network                   nix-vector-routing        olsr                     
point-to-point            point-to-point-layout     propagation              
sixlowpan                 spectrum                  stats                    
tap-bridge                test (no Python)          topology-read            
uan                       virtual-net-device        visualizer               
wave                      wifi                      wimax                    

Modules not built (see ns-3 tutorial for explanation):
brite                     click                     openflow                 

lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf --run "scratch/myfirst --PrintHelp"
Waf: Entering directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
Waf: Leaving directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build\'
\'build\' finished successfully (2.679s)
myfirst [Program Arguments] [General Arguments]

Program Arguments:
    --nPackets:  Number of packets to echo [1]

General Arguments:
    --PrintGlobals:              Print the list of globals.
    --PrintGroups:               Print the list of groups.
    --PrintGroup=[group]:        Print all TypeIds of group.
    --PrintTypeIds:              Print all TypeIds.
    --PrintAttributes=[typeid]:  Print all attributes of typeid.
    --PrintHelp:                 Print this help message.
View Code

 

分类:

技术点:

相关文章: