A Discrete-Event Network Simulator
API
tcp-bulk-send.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // Network topology
17 //
18 // n0 ----------- n1
19 // 500 Kbps
20 // 5 ms
21 //
22 // - Flow from n0 to n1 using BulkSendApplication.
23 // - Tracing of queues and packet receptions to file "tcp-bulk-send.tr"
24 // and pcap tracing available when tracing is turned on.
25 
26 #include "ns3/applications-module.h"
27 #include "ns3/core-module.h"
28 #include "ns3/internet-module.h"
29 #include "ns3/network-module.h"
30 #include "ns3/packet-sink.h"
31 #include "ns3/point-to-point-module.h"
32 
33 #include <fstream>
34 #include <string>
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE("TcpBulkSendExample");
39 
40 int
41 main(int argc, char* argv[])
42 {
43  bool tracing = false;
44  uint32_t maxBytes = 0;
45 
46  //
47  // Allow the user to override any of the defaults at
48  // run-time, via command-line arguments
49  //
50  CommandLine cmd(__FILE__);
51  cmd.AddValue("tracing", "Flag to enable/disable tracing", tracing);
52  cmd.AddValue("maxBytes", "Total number of bytes for application to send", maxBytes);
53  cmd.Parse(argc, argv);
54 
55  //
56  // Explicitly create the nodes required by the topology (shown above).
57  //
58  NS_LOG_INFO("Create nodes.");
60  nodes.Create(2);
61 
62  NS_LOG_INFO("Create channels.");
63 
64  //
65  // Explicitly create the point-to-point link required by the topology (shown above).
66  //
68  pointToPoint.SetDeviceAttribute("DataRate", StringValue("500Kbps"));
69  pointToPoint.SetChannelAttribute("Delay", StringValue("5ms"));
70 
72  devices = pointToPoint.Install(nodes);
73 
74  //
75  // Install the internet stack on the nodes
76  //
77  InternetStackHelper internet;
78  internet.Install(nodes);
79 
80  //
81  // We've got the "hardware" in place. Now we need to add IP addresses.
82  //
83  NS_LOG_INFO("Assign IP Addresses.");
84  Ipv4AddressHelper ipv4;
85  ipv4.SetBase("10.1.1.0", "255.255.255.0");
87 
88  NS_LOG_INFO("Create Applications.");
89 
90  //
91  // Create a BulkSendApplication and install it on node 0
92  //
93  uint16_t port = 9; // well-known echo port number
94 
95  BulkSendHelper source("ns3::TcpSocketFactory", InetSocketAddress(i.GetAddress(1), port));
96  // Set the amount of data to send in bytes. Zero is unlimited.
97  source.SetAttribute("MaxBytes", UintegerValue(maxBytes));
98  ApplicationContainer sourceApps = source.Install(nodes.Get(0));
99  sourceApps.Start(Seconds(0.0));
100  sourceApps.Stop(Seconds(10.0));
101 
102  //
103  // Create a PacketSinkApplication and install it on node 1
104  //
105  PacketSinkHelper sink("ns3::TcpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
106  ApplicationContainer sinkApps = sink.Install(nodes.Get(1));
107  sinkApps.Start(Seconds(0.0));
108  sinkApps.Stop(Seconds(10.0));
109 
110  //
111  // Set up tracing if enabled
112  //
113  if (tracing)
114  {
115  AsciiTraceHelper ascii;
116  pointToPoint.EnableAsciiAll(ascii.CreateFileStream("tcp-bulk-send.tr"));
117  pointToPoint.EnablePcapAll("tcp-bulk-send", false);
118  }
119 
120  //
121  // Now, do the actual simulation.
122  //
123  NS_LOG_INFO("Run Simulation.");
124  Simulator::Stop(Seconds(10.0));
125  Simulator::Run();
127  NS_LOG_INFO("Done.");
128 
129  Ptr<PacketSink> sink1 = DynamicCast<PacketSink>(sinkApps.Get(0));
130  std::cout << "Total Bytes Received: " << sink1->GetTotalRx() << std::endl;
131 
132  return 0;
133 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
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.
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
Parse command-line arguments.
Definition: command-line.h:232
an Inet address class
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="0.0.0.1")
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...
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:96
Build a set of PointToPointNetDevice objects.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
NodeContainer nodes
pointToPoint
Definition: first.py:31
devices
Definition: first.py:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:33
bool tracing
Flag to enable/disable generation of tracing files.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55