A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
42
using namespace
ns3
;
43
44
NS_LOG_COMPONENT_DEFINE
(
"TcpServer"
);
45
46
int
47
main(
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
77
InternetStackHelper
internet
;
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."
);
89
PointToPointHelper
p2p
;
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."
);
100
Ipv4AddressHelper
ipv4
;
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
111
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
112
113
// Create a packet sink on the star "hub" to receive these packets
114
uint16_t
port
= 50000;
115
Address
sinkLocalAddress
(
InetSocketAddress
(
Ipv4Address::GetAny
(),
port
));
116
PacketSinkHelper
sinkHelper
(
"ns3::TcpSocketFactory"
,
sinkLocalAddress
);
117
ApplicationContainer
sinkApp
=
sinkHelper
.Install(
serverNode
);
118
sinkApp
.
Start
(
Seconds
(1));
119
sinkApp
.Stop(
Seconds
(10));
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
128
ApplicationContainer
clientApps
;
129
for
(
uint32_t
i
= 0;
i
<
clientNodes
.GetN(); ++
i
)
130
{
131
AddressValue
remoteAddress
(
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));
137
clientApps
.Stop(
Seconds
(10));
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."
);
145
Simulator::Run
();
146
Simulator::Destroy
();
147
NS_LOG_INFO
(
"Done."
);
148
149
return
0;
150
}
ns3::Address
a polymophic address class
Definition
address.h:90
ns3::AddressValue
AttributeValue implementation for Address.
Definition
address.h:275
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition
application-container.h:33
ns3::ApplicationContainer::Start
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Definition
application-container.cc:81
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition
trace-helper.h:163
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::InetSocketAddress
an Inet address class
Definition
inet-socket-address.h:31
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition
internet-stack-helper.h:81
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition
ipv4-address-helper.h:38
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny()
Definition
ipv4-address.cc:368
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition
ipv4-global-routing-helper.cc:50
ns3::NodeContainer
keep track of a set of node pointers.
Definition
node-container.h:29
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition
node-container.cc:73
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition
on-off-helper.h:26
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition
packet-sink-helper.h:23
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition
point-to-point-helper.h:33
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::StringValue
Hold variables of type string.
Definition
string.h:45
ns3::UintegerValue
Hold an unsigned integer type.
Definition
uinteger.h:34
uint32_t
port
uint16_t port
Definition
dsdv-manet.cc:33
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition
config.cc:883
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition
log.h:264
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
brite-generic-example.p2p
p2p
Definition
brite-generic-example.py:32
first.clientApps
clientApps
Definition
first.py:53
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
nsclick-simple-lan.remoteAddress
remoteAddress
Definition
nsclick-simple-lan.py:63
nsclick-simple-lan.ipv4
ipv4
Definition
nsclick-simple-lan.py:46
nsclick-simple-lan.internet
internet
Definition
nsclick-simple-lan.py:34
second.cmd
cmd
Definition
second.py:29
examples
tcp
tcp-star-server.cc
Generated on Mon Dec 15 2025 15:21:47 for ns-3 by
1.9.8