A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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;
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 }