A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-trace-client-server.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
7 */
8
9// Network topology
10//
11// n0 n1
12// | |
13// =======
14// LAN (CSMA)
15//
16// - UDP flow from n0 to n1 of packets drawn from a trace file
17// -- option to use IPv4 or IPv6 addressing
18// -- option to disable logging statements
19
20#include "ns3/applications-module.h"
21#include "ns3/core-module.h"
22#include "ns3/csma-module.h"
23#include "ns3/internet-module.h"
24
25#include <fstream>
26
27using namespace ns3;
28
29NS_LOG_COMPONENT_DEFINE("UdpTraceClientServerExample");
30
31int
32main(int argc, char* argv[])
33{
34 // Declare variables used in command-line arguments
35 bool useV6 = false;
36 bool logging = true;
37 uint16_t port = 4000;
38 Address serverAddress;
39
40 CommandLine cmd(__FILE__);
41 cmd.AddValue("useIpv6", "Use Ipv6", useV6);
42 cmd.AddValue("logging", "Enable logging", logging);
43 cmd.Parse(argc, argv);
44
45 if (logging)
46 {
49 }
50
51 NS_LOG_INFO("Create nodes in above topology.");
53 n.Create(2);
54
56 internet.Install(n);
57
58 NS_LOG_INFO("Create channel between the two nodes.");
60 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
61 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
62 csma.SetDeviceAttribute("Mtu", UintegerValue(1500));
63 NetDeviceContainer d = csma.Install(n);
64
65 NS_LOG_INFO("Assign IP Addresses.");
66 if (!useV6)
67 {
69 ipv4.SetBase("10.1.1.0", "255.255.255.0");
70 Ipv4InterfaceContainer i = ipv4.Assign(d);
71 serverAddress = InetSocketAddress(i.GetAddress(1), port);
72 }
73 else
74 {
76 ipv6.SetBase("2001:0000:f00d:cafe::", Ipv6Prefix(64));
77 Ipv6InterfaceContainer i6 = ipv6.Assign(d);
78 serverAddress = Inet6SocketAddress(i6.GetAddress(1, 1), port);
79 }
80
81 NS_LOG_INFO("Create UdpServer application on node 1.");
83 ApplicationContainer apps = server.Install(n.Get(1));
84 apps.Start(Seconds(1));
85 apps.Stop(Seconds(10));
86
87 NS_LOG_INFO("Create UdpClient application on node 0 to send to node 1.");
88 uint32_t MaxPacketSize = 1472; // Back off 20 (IP) + 8 (UDP) bytes from MTU
89 UdpTraceClientHelper client(serverAddress);
90 client.SetAttribute("MaxPacketSize", UintegerValue(MaxPacketSize));
91 apps = client.Install(n.Get(0));
92 apps.Start(Seconds(2));
93 apps.Stop(Seconds(10));
94
95 NS_LOG_INFO("Run Simulation.");
98 NS_LOG_INFO("Done.");
99
100 return 0;
101}
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.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
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
An Inet6 address class.
an Inet address class
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
Create a server application which waits for input UDP packets and uses the information carried into t...
Create UdpTraceClient application which sends UDP packets based on a trace file of an MPEG4 stream.
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:1344
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1356
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_INFO
LOG_INFO and above.
Definition log.h:93