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
csma-broadcast.cc
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: GPL-2.0-only
3
*/
4
5
//
6
// Example of the sending of a datagram to a broadcast address
7
//
8
// Network topology
9
// ==============
10
// | |
11
// n0 n1 n2
12
// | |
13
// ==========
14
//
15
// n0 originates UDP broadcast to 255.255.255.255/discard port, which
16
// is replicated and received on both n1 and n2
17
18
#include "ns3/applications-module.h"
19
#include "ns3/core-module.h"
20
#include "ns3/csma-module.h"
21
#include "ns3/internet-module.h"
22
#include "ns3/network-module.h"
23
24
#include <cassert>
25
#include <fstream>
26
#include <iostream>
27
#include <string>
28
29
using namespace
ns3
;
30
31
NS_LOG_COMPONENT_DEFINE
(
"CsmaBroadcastExample"
);
32
33
int
34
main(
int
argc,
char
* argv[])
35
{
36
// Users may find it convenient to turn on explicit debugging
37
// for selected modules; the below lines suggest how to do this
38
#if 0
39
LogComponentEnable
(
"CsmaBroadcastExample"
,
LOG_LEVEL_INFO
);
40
#endif
41
LogComponentEnable
(
"CsmaBroadcastExample"
,
LOG_PREFIX_TIME
);
42
43
// Allow the user to override any of the defaults and the above
44
// Bind()s at run-time, via command-line arguments
45
CommandLine
cmd
(__FILE__);
46
cmd
.Parse(argc, argv);
47
48
NS_LOG_INFO
(
"Create nodes."
);
49
NodeContainer
c;
50
c.
Create
(3);
51
NodeContainer
c0 =
NodeContainer
(c.
Get
(0), c.
Get
(1));
52
NodeContainer
c1 =
NodeContainer
(c.
Get
(0), c.
Get
(2));
53
54
NS_LOG_INFO
(
"Build Topology."
);
55
CsmaHelper
csma
;
56
csma
.SetChannelAttribute(
"DataRate"
,
DataRateValue
(
DataRate
(5000000)));
57
csma
.SetChannelAttribute(
"Delay"
,
TimeValue
(
MilliSeconds
(2)));
58
59
NetDeviceContainer
n0 =
csma
.Install(c0);
60
NetDeviceContainer
n1 =
csma
.Install(c1);
61
62
InternetStackHelper
internet
;
63
internet
.Install(c);
64
65
NS_LOG_INFO
(
"Assign IP Addresses."
);
66
Ipv4AddressHelper
ipv4
;
67
ipv4
.SetBase(
"10.1.0.0"
,
"255.255.255.0"
);
68
ipv4
.Assign(n0);
69
ipv4
.SetBase(
"192.168.1.0"
,
"255.255.255.0"
);
70
ipv4
.Assign(n1);
71
72
// RFC 863 discard port ("9") indicates packet should be thrown away
73
// by the system. We allow this silent discard to be overridden
74
// by the PacketSink application.
75
uint16_t
port
= 9;
76
77
// Create the OnOff application to send UDP datagrams of size
78
// 512 bytes (default) at a rate of 500 Kb/s (default) from n0
79
NS_LOG_INFO
(
"Create Applications."
);
80
OnOffHelper
onoff
(
"ns3::UdpSocketFactory"
,
81
Address
(
InetSocketAddress
(
Ipv4Address
(
"255.255.255.255"
),
port
)));
82
onoff
.SetConstantRate(
DataRate
(
"500kb/s"
));
83
84
ApplicationContainer
app
=
onoff
.Install(c0.
Get
(0));
85
// Start the application
86
app
.Start(
Seconds
(1.0));
87
app
.Stop(
Seconds
(10.0));
88
89
// Create an optional packet sink to receive these packets
90
PacketSinkHelper
sink
(
"ns3::UdpSocketFactory"
,
91
Address
(
InetSocketAddress
(
Ipv4Address::GetAny
(),
port
)));
92
app
=
sink
.Install(c0.
Get
(1));
93
app
.Add(
sink
.Install(c1.
Get
(1)));
94
app
.Start(
Seconds
(1.0));
95
app
.Stop(
Seconds
(10.0));
96
97
// Configure ascii tracing of all enqueue, dequeue, and NetDevice receive
98
// events on all devices. Trace output will be sent to the file
99
// "csma-one-subnet.tr"
100
AsciiTraceHelper
ascii;
101
csma
.EnableAsciiAll(ascii.
CreateFileStream
(
"csma-broadcast.tr"
));
102
103
// Also configure some tcpdump traces; each interface will be traced
104
// The output files will be named
105
// csma-broadcast-<nodeId>-<interfaceId>.pcap
106
// and can be read by the "tcpdump -tt -r" command
107
csma
.EnablePcapAll(
"csma-broadcast"
,
false
);
108
109
NS_LOG_INFO
(
"Run Simulation."
);
110
Simulator::Run
();
111
Simulator::Destroy
();
112
NS_LOG_INFO
(
"Done."
);
113
114
return
0;
115
}
ns3::Address
a polymophic address class
Definition
address.h:90
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition
application-container.h:33
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition
trace-helper.h:163
ns3::AsciiTraceHelper::CreateFileStream
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.
Definition
trace-helper.cc:182
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition
csma-helper.h:37
ns3::DataRate
Class for representing data rates.
Definition
data-rate.h:78
ns3::DataRateValue
Definition
data-rate.h:285
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
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny()
Definition
ipv4-address.cc:368
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition
net-device-container.h:32
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::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition
node-container.cc:67
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::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::TimeValue
Definition
nstime.h:1395
port
uint16_t port
Definition
dsdv-manet.cc:33
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::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1308
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition
nstime.h:1320
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LogComponentEnable
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition
log.cc:291
ns3::LOG_PREFIX_TIME
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition
log.h:108
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition
log.h:93
nsclick-simple-lan.ipv4
ipv4
Definition
nsclick-simple-lan.py:46
nsclick-simple-lan.internet
internet
Definition
nsclick-simple-lan.py:34
openflow-switch.app
app
Definition
openflow-switch.py:64
openflow-switch.onoff
onoff
Definition
openflow-switch.py:59
second.csma
csma
Definition
second.py:52
second.cmd
cmd
Definition
second.py:29
sink
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition
wifi-tcp.cc:44
src
csma
examples
csma-broadcast.cc
Generated on Fri Nov 8 2024 13:59:00 for ns-3 by
1.11.0