tutorial/tutorial-1.cc
changeset 1759 a7793aa3ef66
parent 1757 530d2e974eb2
parent 1541 fc95ffdef447
child 1760 15e9e94e7e2e
equal deleted inserted replaced
1757:530d2e974eb2 1759:a7793aa3ef66
     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 
       
    30 NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
       
    31 
       
    32 using namespace ns3;
       
    33 
       
    34 int 
       
    35 main (int argc, char *argv[])
       
    36 {
       
    37   LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
       
    38 
       
    39   NS_LOG_INFO ("UDP Echo Simulation");
       
    40 
       
    41   Ptr<Node> n0 = Create<InternetNode> ();
       
    42   Ptr<Node> n1 = Create<InternetNode> ();
       
    43   Ptr<Node> n2 = Create<InternetNode> ();
       
    44   Ptr<Node> n3 = Create<InternetNode> ();
       
    45 
       
    46   Ptr<CsmaChannel> lan = 
       
    47     CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
       
    48 
       
    49   uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
       
    50     "08:00:2e:00:00:00");
       
    51 
       
    52   uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
       
    53     "08:00:2e:00:00:01");
       
    54 
       
    55   uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
       
    56     "08:00:2e:00:00:02");
       
    57 
       
    58   uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
       
    59     "08:00:2e:00:00:03");
       
    60 
       
    61   CsmaIpv4Topology::AddIpv4Address (n0, nd0, "10.1.1.1", "255.255.255.0");
       
    62   CsmaIpv4Topology::AddIpv4Address (n1, nd1, "10.1.1.2", "255.255.255.0");
       
    63   CsmaIpv4Topology::AddIpv4Address (n2, nd2, "10.1.1.3", "255.255.255.0");
       
    64   CsmaIpv4Topology::AddIpv4Address (n3, nd3, "10.1.1.4", "255.255.255.0");
       
    65 
       
    66   uint16_t port = 7;
       
    67 
       
    68   Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
       
    69     1, Seconds(1.), 1024);
       
    70 
       
    71   Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
       
    72 
       
    73   server->Start(Seconds(1.));
       
    74   client->Start(Seconds(2.));
       
    75 
       
    76   server->Stop (Seconds(10.));
       
    77   client->Stop (Seconds(10.));
       
    78 
       
    79   Simulator::Run ();
       
    80   Simulator::Destroy ();
       
    81 }