A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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("UdpEchoExample");
26
27int
28main(int argc, char* argv[])
29{
30//
31// Users may find it convenient to turn on explicit debugging
32// for selected modules; the below lines suggest how to do this
33//
34#if 0
35 LogComponentEnable ("UdpEchoExample", LOG_LEVEL_INFO);
36 LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_ALL);
37 LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_ALL);
38#endif
39 //
40 // Allow the user to override any of the defaults and the above Bind() at
41 // run-time, via command-line arguments
42 //
43 bool useV6 = false;
44 Address serverAddress;
45
46 CommandLine cmd(__FILE__);
47 cmd.AddValue("useIpv6", "Use Ipv6", useV6);
48 cmd.Parse(argc, argv);
49 //
50 // Explicitly create the nodes required by the topology (shown above).
51 //
52 NS_LOG_INFO("Create nodes.");
54 n.Create(4);
55
57 internet.Install(n);
58
59 NS_LOG_INFO("Create channels.");
60 //
61 // Explicitly create the channels required by the topology (shown above).
62 //
64 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
65 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
66 csma.SetDeviceAttribute("Mtu", UintegerValue(1400));
67 NetDeviceContainer d = csma.Install(n);
68
69 //
70 // We've got the "hardware" in place. Now we need to add IP addresses.
71 //
72 NS_LOG_INFO("Assign IP Addresses.");
73 if (!useV6)
74 {
76 ipv4.SetBase("10.1.1.0", "255.255.255.0");
77 Ipv4InterfaceContainer i = ipv4.Assign(d);
78 serverAddress = Address(i.GetAddress(1));
79 }
80 else
81 {
83 ipv6.SetBase("2001:0000:f00d:cafe::", Ipv6Prefix(64));
84 Ipv6InterfaceContainer i6 = ipv6.Assign(d);
85 serverAddress = Address(i6.GetAddress(1, 1));
86 }
87
88 NS_LOG_INFO("Create Applications.");
89 //
90 // Create a UdpEchoServer application on node one.
91 //
92 uint16_t port = 9; // well-known echo port number
94 ApplicationContainer apps = server.Install(n.Get(1));
95 apps.Start(Seconds(1.0));
96 apps.Stop(Seconds(10.0));
97
98 //
99 // Create a UdpEchoClient application to send UDP datagrams from node zero to
100 // node one.
101 //
102 uint32_t packetSize = 1024;
103 uint32_t maxPacketCount = 1;
104 Time interPacketInterval = Seconds(1.);
105 UdpEchoClientHelper client(serverAddress, port);
106 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
107 client.SetAttribute("Interval", TimeValue(interPacketInterval));
108 client.SetAttribute("PacketSize", UintegerValue(packetSize));
109 apps = client.Install(n.Get(0));
110 apps.Start(Seconds(2.0));
111 apps.Stop(Seconds(10.0));
112
113#if 0
114//
115// Users may find it convenient to initialize echo packets with actual data;
116// the below lines suggest how to do this
117//
118 client.SetFill (apps.Get (0), "Hello World");
119
120 client.SetFill (apps.Get (0), 0xa5, 1024);
121
122 uint8_t fill[] = { 0, 1, 2, 3, 4, 5, 6};
123 client.SetFill (apps.Get (0), fill, sizeof(fill), 1024);
124#endif
125
126 AsciiTraceHelper ascii;
127 csma.EnableAsciiAll(ascii.CreateFileStream("udp-echo.tr"));
128 csma.EnablePcapAll("udp-echo", false);
129
130 //
131 // Now, do the actual simulation.
132 //
133 NS_LOG_INFO("Run Simulation.");
136 NS_LOG_INFO("Done.");
137
138 return 0;
139}
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.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
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
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
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
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
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.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
static const uint32_t packetSize
Packet size generated at the AP.