A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-star-server.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6// Default Network topology, 9 nodes in a star
7/*
8 n2 n3 n4
9 \ | /
10 \|/
11 n1---n0---n5
12 /| \
13 / | \
14 n8 n7 n6
15*/
16// - CBR Traffic goes from the star "arms" to the "hub"
17// - Tracing of queues and packet receptions to file
18// "tcp-star-server.tr"
19// - pcap traces also generated in the following files
20// "tcp-star-server-$n-$i.pcap" where n and i represent node and interface
21// numbers respectively
22// Usage examples for things you might want to tweak:
23// ./ns3 run "tcp-star-server"
24// ./ns3 run "tcp-star-server --nNodes=25"
25// ./ns3 run "tcp-star-server --ns3::OnOffApplication::DataRate=10000"
26// ./ns3 run "tcp-star-server --ns3::OnOffApplication::PacketSize=500"
27// See the ns-3 tutorial for more info on the command line:
28// https://www.nsnam.org/docs/tutorial/html/index.html
29
30#include "ns3/applications-module.h"
31#include "ns3/core-module.h"
32#include "ns3/internet-module.h"
33#include "ns3/ipv4-global-routing-helper.h"
34#include "ns3/network-module.h"
35#include "ns3/point-to-point-module.h"
36
37#include <cassert>
38#include <fstream>
39#include <iostream>
40#include <string>
41
42using namespace ns3;
43
44NS_LOG_COMPONENT_DEFINE("TcpServer");
45
46int
47main(int argc, char* argv[])
48{
49 // Users may find it convenient to turn on explicit debugging
50 // for selected modules; the below lines suggest how to do this
51
52 // LogComponentEnable ("TcpServer", LOG_LEVEL_INFO);
53 // LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
54 // LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);
55 // LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
56
57 // Set up some default values for the simulation.
58 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(250));
59 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("5kb/s"));
60 uint32_t N = 9; // number of nodes in the star
61
62 // Allow the user to override any of the defaults and the above
63 // Config::SetDefault()s at run-time, via command-line arguments
64 CommandLine cmd(__FILE__);
65 cmd.AddValue("nNodes", "Number of nodes to place in the star", N);
66 cmd.Parse(argc, argv);
67
68 // Here, we will create N nodes in a star.
69 NS_LOG_INFO("Create nodes.");
70 NodeContainer serverNode;
71 NodeContainer clientNodes;
72 serverNode.Create(1);
73 clientNodes.Create(N - 1);
74 NodeContainer allNodes = NodeContainer(serverNode, clientNodes);
75
76 // Install network stacks on the nodes
78 internet.Install(allNodes);
79
80 // Collect an adjacency list of nodes for the p2p topology
81 std::vector<NodeContainer> nodeAdjacencyList(N - 1);
82 for (uint32_t i = 0; i < nodeAdjacencyList.size(); ++i)
83 {
84 nodeAdjacencyList[i] = NodeContainer(serverNode, clientNodes.Get(i));
85 }
86
87 // We create the channels first without any IP addressing information
88 NS_LOG_INFO("Create channels.");
90 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
91 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
92 std::vector<NetDeviceContainer> deviceAdjacencyList(N - 1);
93 for (uint32_t i = 0; i < deviceAdjacencyList.size(); ++i)
94 {
95 deviceAdjacencyList[i] = p2p.Install(nodeAdjacencyList[i]);
96 }
97
98 // Later, we add IP addresses.
99 NS_LOG_INFO("Assign IP Addresses.");
101 std::vector<Ipv4InterfaceContainer> interfaceAdjacencyList(N - 1);
102 for (uint32_t i = 0; i < interfaceAdjacencyList.size(); ++i)
103 {
104 std::ostringstream subnet;
105 subnet << "10.1." << i + 1 << ".0";
106 ipv4.SetBase(subnet.str().c_str(), "255.255.255.0");
107 interfaceAdjacencyList[i] = ipv4.Assign(deviceAdjacencyList[i]);
108 }
109
110 // Turn on global static routing
112
113 // Create a packet sink on the star "hub" to receive these packets
114 uint16_t port = 50000;
116 PacketSinkHelper sinkHelper("ns3::TcpSocketFactory", sinkLocalAddress);
117 ApplicationContainer sinkApp = sinkHelper.Install(serverNode);
118 sinkApp.Start(Seconds(1.0));
119 sinkApp.Stop(Seconds(10.0));
120
121 // Create the OnOff applications to send TCP to the server
122 OnOffHelper clientHelper("ns3::TcpSocketFactory", Address());
123 clientHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
124 clientHelper.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
125
126 // normally wouldn't need a loop here but the server IP address is different
127 // on each p2p subnet
129 for (uint32_t i = 0; i < clientNodes.GetN(); ++i)
130 {
132 InetSocketAddress(interfaceAdjacencyList[i].GetAddress(0), port));
133 clientHelper.SetAttribute("Remote", remoteAddress);
134 clientApps.Add(clientHelper.Install(clientNodes.Get(i)));
135 }
136 clientApps.Start(Seconds(1.0));
137 clientApps.Stop(Seconds(10.0));
138
139 // configure tracing
140 AsciiTraceHelper ascii;
141 p2p.EnableAsciiAll(ascii.CreateFileStream("tcp-star-server.tr"));
142 p2p.EnablePcapAll("tcp-star-server");
143
144 NS_LOG_INFO("Run Simulation.");
147 NS_LOG_INFO("Done.");
148
149 return 0;
150}
a polymophic address class
Definition address.h:90
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.
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.
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.
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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
clientApps
Definition first.py:53
Every class exported by the ns3 library is enclosed in the ns3 namespace.