tutorial/tutorial-2.cc
changeset 1694 fc22854fc549
parent 1693 bf884d490040
parent 1670 09ff9d07333e
child 1695 fa84cc049e9c
child 1697 cf710ea9fd1f
child 1764 04f2a1dd7e45
equal deleted inserted replaced
1693:bf884d490040 1694:fc22854fc549
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * This program is free software; you can redistribute it and/or modify
       
     4  * it under the terms of the GNU General Public License version 2 as
       
     5  * published by the Free Software Foundation;
       
     6  *
       
     7  * This program is distributed in the hope that it will be useful,
       
     8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
     9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    10  * GNU General Public License for more details.
       
    11  *
       
    12  * You should have received a copy of the GNU General Public License
       
    13  * along with this program; if not, write to the Free Software
       
    14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    15  */
       
    16 
       
    17 #include "ns3/log.h"
       
    18 #include "ns3/ptr.h"
       
    19 #include "ns3/internet-node.h"
       
    20 #include "ns3/csma-channel.h"
       
    21 #include "ns3/mac48-address.h"
       
    22 #include "ns3/csma-net-device.h"
       
    23 #include "ns3/csma-topology.h"
       
    24 #include "ns3/csma-ipv4-topology.h"
       
    25 #include "ns3/udp-echo-client.h"
       
    26 #include "ns3/udp-echo-server.h"
       
    27 #include "ns3/simulator.h"
       
    28 #include "ns3/nstime.h"
       
    29 #include "ns3/ascii-trace.h"
       
    30 
       
    31 NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
       
    32 
       
    33 using namespace ns3;
       
    34 
       
    35 int 
       
    36 main (int argc, char *argv[])
       
    37 {
       
    38   LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
       
    39 
       
    40   NS_LOG_INFO ("UDP Echo Simulation");
       
    41 
       
    42   Ptr<Node> n0 = Create<InternetNode> ();
       
    43   Ptr<Node> n1 = Create<InternetNode> ();
       
    44   Ptr<Node> n2 = Create<InternetNode> ();
       
    45   Ptr<Node> n3 = Create<InternetNode> ();
       
    46 
       
    47   Ptr<CsmaChannel> lan = 
       
    48     CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
       
    49 
       
    50   uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
       
    51     "08:00:2e:00:00:00");
       
    52 
       
    53   uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
       
    54     "08:00:2e:00:00:01");
       
    55 
       
    56   uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
       
    57     "08:00:2e:00:00:02");
       
    58 
       
    59   uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
       
    60     "08:00:2e:00:00:03");
       
    61 
       
    62   CsmaIpv4Topology::AddIpv4Address (n0, nd0, "10.1.1.1", "255.255.255.0");
       
    63   CsmaIpv4Topology::AddIpv4Address (n1, nd1, "10.1.1.2", "255.255.255.0");
       
    64   CsmaIpv4Topology::AddIpv4Address (n2, nd2, "10.1.1.3", "255.255.255.0");
       
    65   CsmaIpv4Topology::AddIpv4Address (n3, nd3, "10.1.1.4", "255.255.255.0");
       
    66 
       
    67   uint16_t port = 7;
       
    68 
       
    69   Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
       
    70     1, Seconds(1.), 1024);
       
    71 
       
    72   Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
       
    73 
       
    74   server->Start(Seconds(1.));
       
    75   client->Start(Seconds(2.));
       
    76 
       
    77   server->Stop (Seconds(10.));
       
    78   client->Stop (Seconds(10.));
       
    79 
       
    80   AsciiTrace asciitrace ("tutorial.tr");
       
    81   asciitrace.TraceAllQueues ();
       
    82   asciitrace.TraceAllNetDeviceRx ();
       
    83 
       
    84   Simulator::Run ();
       
    85   Simulator::Destroy ();
       
    86 }