A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-star-server.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
17// Default Network topology, 9 nodes in a star
18/*
19 n2 n3 n4
20 \ | /
21 \|/
22 n1---n0---n5
23 /| \
24 / | \
25 n8 n7 n6
26*/
27// - CBR Traffic goes from the star "arms" to the "hub"
28// - Tracing of queues and packet receptions to file
29// "tcp-star-server.tr"
30// - pcap traces also generated in the following files
31// "tcp-star-server-$n-$i.pcap" where n and i represent node and interface
32// numbers respectively
33// Usage examples for things you might want to tweak:
34// ./ns3 run "tcp-star-server"
35// ./ns3 run "tcp-star-server --nNodes=25"
36// ./ns3 run "tcp-star-server --ns3::OnOffApplication::DataRate=10000"
37// ./ns3 run "tcp-star-server --ns3::OnOffApplication::PacketSize=500"
38// See the ns-3 tutorial for more info on the command line:
39// https://www.nsnam.org/docs/tutorial/html/index.html
40
41#include "ns3/applications-module.h"
42#include "ns3/core-module.h"
43#include "ns3/internet-module.h"
44#include "ns3/ipv4-global-routing-helper.h"
45#include "ns3/network-module.h"
46#include "ns3/point-to-point-module.h"
47
48#include <cassert>
49#include <fstream>
50#include <iostream>
51#include <string>
52
53using namespace ns3;
54
55NS_LOG_COMPONENT_DEFINE("TcpServer");
56
57int
58main(int argc, char* argv[])
59{
60 // Users may find it convenient to turn on explicit debugging
61 // for selected modules; the below lines suggest how to do this
62
63 // LogComponentEnable ("TcpServer", LOG_LEVEL_INFO);
64 // LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
65 // LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);
66 // LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
67
68 // Set up some default values for the simulation.
69 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(250));
70 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("5kb/s"));
71 uint32_t N = 9; // number of nodes in the star
72
73 // Allow the user to override any of the defaults and the above
74 // Config::SetDefault()s at run-time, via command-line arguments
75 CommandLine cmd(__FILE__);
76 cmd.AddValue("nNodes", "Number of nodes to place in the star", N);
77 cmd.Parse(argc, argv);
78
79 // Here, we will create N nodes in a star.
80 NS_LOG_INFO("Create nodes.");
81 NodeContainer serverNode;
82 NodeContainer clientNodes;
83 serverNode.Create(1);
84 clientNodes.Create(N - 1);
85 NodeContainer allNodes = NodeContainer(serverNode, clientNodes);
86
87 // Install network stacks on the nodes
89 internet.Install(allNodes);
90
91 // Collect an adjacency list of nodes for the p2p topology
92 std::vector<NodeContainer> nodeAdjacencyList(N - 1);
93 for (uint32_t i = 0; i < nodeAdjacencyList.size(); ++i)
94 {
95 nodeAdjacencyList[i] = NodeContainer(serverNode, clientNodes.Get(i));
96 }
97
98 // We create the channels first without any IP addressing information
99 NS_LOG_INFO("Create channels.");
101 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
102 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
103 std::vector<NetDeviceContainer> deviceAdjacencyList(N - 1);
104 for (uint32_t i = 0; i < deviceAdjacencyList.size(); ++i)
105 {
106 deviceAdjacencyList[i] = p2p.Install(nodeAdjacencyList[i]);
107 }
108
109 // Later, we add IP addresses.
110 NS_LOG_INFO("Assign IP Addresses.");
112 std::vector<Ipv4InterfaceContainer> interfaceAdjacencyList(N - 1);
113 for (uint32_t i = 0; i < interfaceAdjacencyList.size(); ++i)
114 {
115 std::ostringstream subnet;
116 subnet << "10.1." << i + 1 << ".0";
117 ipv4.SetBase(subnet.str().c_str(), "255.255.255.0");
118 interfaceAdjacencyList[i] = ipv4.Assign(deviceAdjacencyList[i]);
119 }
120
121 // Turn on global static routing
123
124 // Create a packet sink on the star "hub" to receive these packets
125 uint16_t port = 50000;
127 PacketSinkHelper sinkHelper("ns3::TcpSocketFactory", sinkLocalAddress);
128 ApplicationContainer sinkApp = sinkHelper.Install(serverNode);
129 sinkApp.Start(Seconds(1.0));
130 sinkApp.Stop(Seconds(10.0));
131
132 // Create the OnOff applications to send TCP to the server
133 OnOffHelper clientHelper("ns3::TcpSocketFactory", Address());
134 clientHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
135 clientHelper.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
136
137 // normally wouldn't need a loop here but the server IP address is different
138 // on each p2p subnet
140 for (uint32_t i = 0; i < clientNodes.GetN(); ++i)
141 {
143 InetSocketAddress(interfaceAdjacencyList[i].GetAddress(0), port));
144 clientHelper.SetAttribute("Remote", remoteAddress);
145 clientApps.Add(clientHelper.Install(clientNodes.Get(i)));
146 }
147 clientApps.Start(Seconds(1.0));
148 clientApps.Stop(Seconds(10.0));
149
150 // configure tracing
151 AsciiTraceHelper ascii;
152 p2p.EnableAsciiAll(ascii.CreateFileStream("tcp-star-server.tr"));
153 p2p.EnablePcapAll("tcp-star-server");
154
155 NS_LOG_INFO("Run Simulation.");
158 NS_LOG_INFO("Done.");
159
160 return 0;
161}
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Address.
Definition: address.h:286
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.
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:174
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.
Parse command-line arguments.
Definition: command-line.h:232
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
keep track of a set of node pointers.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
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::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
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:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
#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:1319
ns clientApps
Definition: first.py:64
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40