A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
realtime-udp-echo.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Network topology
6//
7// n0 n1 n2 n3
8// | | | |
9// =================
10// LAN
11//
12// - UDP flows from n0 to n1 and back
13// - DropTail queues
14// - Tracing of queues and packet receptions to file "udp-echo.tr"
15
16#include "ns3/applications-module.h"
17#include "ns3/core-module.h"
18#include "ns3/csma-module.h"
19#include "ns3/internet-module.h"
20
21#include <fstream>
22
23using namespace ns3;
24
25NS_LOG_COMPONENT_DEFINE("RealtimeUdpEchoExample");
26
27int
28main(int argc, char* argv[])
29{
30 //
31 // Allow the user to override any of the defaults and the above Bind() at
32 // run-time, via command-line arguments
33 //
34 CommandLine cmd(__FILE__);
35 cmd.Parse(argc, argv);
36
37 //
38 // But since this is a realtime script, don't allow the user to mess with
39 // that.
40 //
41 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
42
43 //
44 // Explicitly create the nodes required by the topology (shown above).
45 //
46 NS_LOG_INFO("Create nodes.");
48 n.Create(4);
49
51 internet.Install(n);
52
53 //
54 // Explicitly create the channels required by the topology (shown above).
55 //
56 NS_LOG_INFO("Create channels.");
58 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
59 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
60 csma.SetDeviceAttribute("Mtu", UintegerValue(1400));
61 NetDeviceContainer d = csma.Install(n);
62
63 //
64 // We've got the "hardware" in place. Now we need to add IP addresses.
65 //
66 NS_LOG_INFO("Assign IP Addresses.");
68 ipv4.SetBase("10.1.1.0", "255.255.255.0");
69 Ipv4InterfaceContainer i = ipv4.Assign(d);
70
71 NS_LOG_INFO("Create Applications.");
72
73 //
74 // Create a UdpEchoServer application on node one.
75 //
76 uint16_t port = 9; // well-known echo port number
78 ApplicationContainer apps = server.Install(n.Get(1));
79 apps.Start(Seconds(1.0));
80 apps.Stop(Seconds(10.0));
81
82 //
83 // Create a UdpEchoClient application to send UDP datagrams from node zero to
84 // node one.
85 //
86 uint32_t packetSize = 1024;
87 uint32_t maxPacketCount = 500;
88 Time interPacketInterval = Seconds(0.01);
90 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
91 client.SetAttribute("Interval", TimeValue(interPacketInterval));
92 client.SetAttribute("PacketSize", UintegerValue(packetSize));
93 apps = client.Install(n.Get(0));
94 apps.Start(Seconds(2.0));
95 apps.Stop(Seconds(10.0));
96
97 AsciiTraceHelper ascii;
98 csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"));
99 csma.EnablePcapAll("realtime-udp-echo", false);
100
101 //
102 // Now, do the actual simulation.
103 //
105 NS_LOG_INFO("Run Simulation.");
108 NS_LOG_INFO("Done.");
109
110 return 0;
111}
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
#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
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize
Packet size generated at the AP.