A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
tcp-star-server.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
18
19
// Default Network topology, 9 nodes in a star
20
/*
21
n2 n3 n4
22
\ | /
23
\|/
24
n1---n0---n5
25
/| \
26
/ | \
27
n8 n7 n6
28
*/
29
// - CBR Traffic goes from the star "arms" to the "hub"
30
// - Tracing of queues and packet receptions to file
31
// "tcp-star-server.tr"
32
// - pcap traces also generated in the following files
33
// "tcp-star-server-$n-$i.pcap" where n and i represent node and interface
34
// numbers respectively
35
// Usage examples for things you might want to tweak:
36
// ./waf --run="tcp-star-server"
37
// ./waf --run="tcp-star-server --nNodes=25"
38
// ./waf --run="tcp-star-server --ns3::OnOffApplication::DataRate=10000"
39
// ./waf --run="tcp-star-server --ns3::OnOffApplication::PacketSize=500"
40
// See the ns-3 tutorial for more info on the command line:
41
// http://www.nsnam.org/tutorials.html
42
43
44
45
46
#include <iostream>
47
#include <fstream>
48
#include <string>
49
#include <cassert>
50
51
#include "ns3/core-module.h"
52
#include "ns3/network-module.h"
53
#include "ns3/internet-module.h"
54
#include "ns3/point-to-point-module.h"
55
#include "ns3/applications-module.h"
56
#include "ns3/ipv4-global-routing-helper.h"
57
58
using namespace
ns3;
59
60
NS_LOG_COMPONENT_DEFINE
(
"TcpServer"
);
61
62
int
63
main
(
int
argc,
char
*argv[])
64
{
65
// Users may find it convenient to turn on explicit debugging
66
// for selected modules; the below lines suggest how to do this
67
68
//LogComponentEnable ("TcpServer", LOG_LEVEL_INFO);
69
//LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
70
//LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);
71
//LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
72
73
// Set up some default values for the simulation.
74
Config::SetDefault
(
"ns3::OnOffApplication::PacketSize"
,
UintegerValue
(250));
75
Config::SetDefault
(
"ns3::OnOffApplication::DataRate"
,
StringValue
(
"5kb/s"
));
76
uint32_t N = 9;
//number of nodes in the star
77
78
// Allow the user to override any of the defaults and the above
79
// Config::SetDefault()s at run-time, via command-line arguments
80
CommandLine
cmd;
81
cmd.
AddValue
(
"nNodes"
,
"Number of nodes to place in the star"
, N);
82
cmd.
Parse
(argc, argv);
83
84
// Here, we will create N nodes in a star.
85
NS_LOG_INFO
(
"Create nodes."
);
86
NodeContainer
serverNode;
87
NodeContainer
clientNodes;
88
serverNode.
Create
(1);
89
clientNodes.
Create
(N-1);
90
NodeContainer
allNodes =
NodeContainer
(serverNode, clientNodes);
91
92
// Install network stacks on the nodes
93
InternetStackHelper
internet;
94
internet.
Install
(allNodes);
95
96
//Collect an adjacency list of nodes for the p2p topology
97
std::vector<NodeContainer> nodeAdjacencyList (N-1);
98
for
(uint32_t i=0; i<nodeAdjacencyList.size (); ++i)
99
{
100
nodeAdjacencyList[i] =
NodeContainer
(serverNode, clientNodes.
Get
(i));
101
}
102
103
// We create the channels first without any IP addressing information
104
NS_LOG_INFO
(
"Create channels."
);
105
PointToPointHelper
p2p;
106
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
107
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
108
std::vector<NetDeviceContainer> deviceAdjacencyList (N-1);
109
for
(uint32_t i=0; i<deviceAdjacencyList.size (); ++i)
110
{
111
deviceAdjacencyList[i] = p2p.
Install
(nodeAdjacencyList[i]);
112
}
113
114
// Later, we add IP addresses.
115
NS_LOG_INFO
(
"Assign IP Addresses."
);
116
Ipv4AddressHelper
ipv4;
117
std::vector<Ipv4InterfaceContainer> interfaceAdjacencyList (N-1);
118
for
(uint32_t i=0; i<interfaceAdjacencyList.size (); ++i)
119
{
120
std::ostringstream subnet;
121
subnet<<
"10.1."
<<i+1<<
".0"
;
122
ipv4.
SetBase
(subnet.str ().c_str (),
"255.255.255.0"
);
123
interfaceAdjacencyList[i] = ipv4.
Assign
(deviceAdjacencyList[i]);
124
}
125
126
//Turn on global static routing
127
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
128
129
// Create a packet sink on the star "hub" to receive these packets
130
uint16_t
port
= 50000;
131
Address
sinkLocalAddress (
InetSocketAddress
(
Ipv4Address::GetAny
(), port));
132
PacketSinkHelper
sinkHelper (
"ns3::TcpSocketFactory"
, sinkLocalAddress);
133
ApplicationContainer
sinkApp = sinkHelper.
Install
(serverNode);
134
sinkApp.
Start
(Seconds (1.0));
135
sinkApp.
Stop
(Seconds (10.0));
136
137
// Create the OnOff applications to send TCP to the server
138
OnOffHelper
clientHelper (
"ns3::TcpSocketFactory"
,
Address
());
139
clientHelper.
SetAttribute
(
"OnTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=1]"
));
140
clientHelper.
SetAttribute
(
"OffTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=0]"
));
141
142
//normally wouldn't need a loop here but the server IP address is different
143
//on each p2p subnet
144
ApplicationContainer
clientApps
;
145
for
(uint32_t i=0; i<clientNodes.
GetN
(); ++i)
146
{
147
AddressValue
remoteAddress
148
(
InetSocketAddress
(interfaceAdjacencyList[i].GetAddress (0), port));
149
clientHelper.
SetAttribute
(
"Remote"
, remoteAddress);
150
clientApps.
Add
(clientHelper.
Install
(clientNodes.
Get
(i)));
151
}
152
clientApps.
Start
(Seconds (1.0));
153
clientApps.
Stop
(Seconds (10.0));
154
155
156
//configure tracing
157
AsciiTraceHelper
ascii;
158
p2p.
EnableAsciiAll
(ascii.
CreateFileStream
(
"tcp-star-server.tr"
));
159
p2p.
EnablePcapAll
(
"tcp-star-server"
);
160
161
NS_LOG_INFO
(
"Run Simulation."
);
162
Simulator::Run
();
163
Simulator::Destroy
();
164
NS_LOG_INFO
(
"Done."
);
165
166
return
0;
167
}
examples
tcp
tcp-star-server.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2