A Discrete-Event Network Simulator
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2009 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19 */
21// Network topology
23// n0 n1
24// | |
25// =======
26// LAN (CSMA)
28// - UDP flow from n0 to n1 of packets drawn from a trace file
29// -- option to use IPv4 or IPv6 addressing
30// -- option to disable logging statements
32#include <fstream>
33#include "ns3/core-module.h"
34#include "ns3/csma-module.h"
35#include "ns3/applications-module.h"
36#include "ns3/internet-module.h"
38using namespace ns3;
40NS_LOG_COMPONENT_DEFINE ("UdpTraceClientServerExample");
43main (int argc, char *argv[])
45 // Declare variables used in command-line arguments
46 bool useV6 = false;
47 bool logging = true;
48 Address serverAddress;
50 CommandLine cmd (__FILE__);
51 cmd.AddValue ("useIpv6", "Use Ipv6", useV6);
52 cmd.AddValue ("logging", "Enable logging", logging);
53 cmd.Parse (argc, argv);
55 if (logging)
56 {
59 }
61 NS_LOG_INFO ("Create nodes in above topology.");
63 n.Create (2);
65 InternetStackHelper internet;
66 internet.Install (n);
68 NS_LOG_INFO ("Create channel between the two nodes.");
70 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
71 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
72 csma.SetDeviceAttribute ("Mtu", UintegerValue (1500));
73 NetDeviceContainer d = csma.Install (n);
75 NS_LOG_INFO ("Assign IP Addresses.");
76 if (useV6 == false)
77 {
79 ipv4.SetBase ("", "");
80 Ipv4InterfaceContainer i = ipv4.Assign (d);
81 serverAddress = Address (i.GetAddress (1));
82 }
83 else
84 {
86 ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64));
87 Ipv6InterfaceContainer i6 = ipv6.Assign (d);
88 serverAddress = Address(i6.GetAddress (1,1));
89 }
91 NS_LOG_INFO ("Create UdpServer application on node 1.");
92 uint16_t port = 4000;
93 UdpServerHelper server (port);
94 ApplicationContainer apps = server.Install (n.Get (1));
95 apps.Start (Seconds (1.0));
96 apps.Stop (Seconds (10.0));
98 NS_LOG_INFO ("Create UdpClient application on node 0 to send to node 1.");
99 uint32_t MaxPacketSize = 1472; // Back off 20 (IP) + 8 (UDP) bytes from MTU
100 UdpTraceClientHelper client (serverAddress, port,"");
101 client.SetAttribute ("MaxPacketSize", UintegerValue (MaxPacketSize));
102 apps = client.Install (n.Get (0));
103 apps.Start (Seconds (2.0));
104 apps.Stop (Seconds (10.0));
106 NS_LOG_INFO ("Run Simulation.");
107 Simulator::Run ();
108 Simulator::Destroy ();
109 NS_LOG_INFO ("Done.");
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
AttributeValue implementation for Time.
Definition: nstime.h:1308
Create a server application which waits for input UDP packets and uses the information carried into t...
Create UdpTraceClient application which sends UDP packets based on a trace file of an MPEG4 stream.
Hold an unsigned integer type.
Definition: uinteger.h:44
uint16_t port
Definition: dsdv-manet.cc:45
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
LOG_INFO and above.
Definition: log.h:107
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
Definition: second.py:63
Definition: second.py:35