A Discrete-Event Network Simulator
API
tcp-bulk-send.cc
Go to the documentation of this file.
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 // Network topology
18 //
19 // n0 ----------- n1
20 // 500 Kbps
21 // 5 ms
22 //
23 // - Flow from n0 to n1 using BulkSendApplication.
24 // - Tracing of queues and packet receptions to file "tcp-bulk-send.tr"
25 // and pcap tracing available when tracing is turned on.
26 
27 #include <string>
28 #include <fstream>
29 #include "ns3/core-module.h"
30 #include "ns3/point-to-point-module.h"
31 #include "ns3/internet-module.h"
32 #include "ns3/applications-module.h"
33 #include "ns3/network-module.h"
34 #include "ns3/packet-sink.h"
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE ("TcpBulkSendExample");
39 
40 int
41 main (int argc, char *argv[])
42 {
43 
44  bool tracing = false;
45  uint32_t maxBytes = 0;
46 
47 //
48 // Allow the user to override any of the defaults at
49 // run-time, via command-line arguments
50 //
52  cmd.AddValue ("tracing", "Flag to enable/disable tracing", tracing);
53  cmd.AddValue ("maxBytes",
54  "Total number of bytes for application to send", maxBytes);
55  cmd.Parse (argc, argv);
56 
57 //
58 // Explicitly create the nodes required by the topology (shown above).
59 //
60  NS_LOG_INFO ("Create nodes.");
62  nodes.Create (2);
63 
64  NS_LOG_INFO ("Create channels.");
65 
66 //
67 // Explicitly create the point-to-point link required by the topology (shown above).
68 //
70  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("500Kbps"));
71  pointToPoint.SetChannelAttribute ("Delay", StringValue ("5ms"));
72 
74  devices = pointToPoint.Install (nodes);
75 
76 //
77 // Install the internet stack on the nodes
78 //
79  InternetStackHelper internet;
80  internet.Install (nodes);
81 
82 //
83 // We've got the "hardware" in place. Now we need to add IP addresses.
84 //
85  NS_LOG_INFO ("Assign IP Addresses.");
86  Ipv4AddressHelper ipv4;
87  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
88  Ipv4InterfaceContainer i = ipv4.Assign (devices);
89 
90  NS_LOG_INFO ("Create Applications.");
91 
92 //
93 // Create a BulkSendApplication and install it on node 0
94 //
95  uint16_t port = 9; // well-known echo port number
96 
97 
98  BulkSendHelper source ("ns3::TcpSocketFactory",
100  // Set the amount of data to send in bytes. Zero is unlimited.
101  source.SetAttribute ("MaxBytes", UintegerValue (maxBytes));
102  ApplicationContainer sourceApps = source.Install (nodes.Get (0));
103  sourceApps.Start (Seconds (0.0));
104  sourceApps.Stop (Seconds (10.0));
105 
106 //
107 // Create a PacketSinkApplication and install it on node 1
108 //
109  PacketSinkHelper sink ("ns3::TcpSocketFactory",
111  ApplicationContainer sinkApps = sink.Install (nodes.Get (1));
112  sinkApps.Start (Seconds (0.0));
113  sinkApps.Stop (Seconds (10.0));
114 
115 //
116 // Set up tracing if enabled
117 //
118  if (tracing)
119  {
120  AsciiTraceHelper ascii;
121  pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("tcp-bulk-send.tr"));
122  pointToPoint.EnablePcapAll ("tcp-bulk-send", false);
123  }
124 
125 //
126 // Now, do the actual simulation.
127 //
128  NS_LOG_INFO ("Run Simulation.");
129  Simulator::Stop (Seconds (10.0));
130  Simulator::Run ();
132  NS_LOG_INFO ("Done.");
133 
134  Ptr<PacketSink> sink1 = DynamicCast<PacketSink> (sinkApps.Get (0));
135  std::cout << "Total Bytes Received: " << sink1->GetTotalRx () << std::endl;
136 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:47
holds a vector of ns3::Application pointers.
tuple pointToPoint
Definition: first.py:28
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
tuple devices
Definition: first.py:32
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes...
holds a vector of std::pair of Ptr and interface index.
Hold variables of type string.
Definition: string.h:41
NetDeviceContainer Install(NodeContainer c)
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
tuple nodes
Definition: first.py:25
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:78
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const