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 //
51  CommandLine cmd (__FILE__);
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");
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:56
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
uint64_t GetTotalRx() const
Definition: packet-sink.cc:91
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
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:281
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
Build a set of PointToPointNetDevice objects.
uint16_t port
Definition: dsdv-manet.cc:45
nodes
Definition: first.py:32
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
pointToPoint
Definition: first.py:35
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:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
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...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1288
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
devices
Definition: first.py:39
bool tracing
Flag to enable/disable generation of tracing files.
Definition: wifi-bianchi.cc:82
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.