A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-client-server.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
18 */
19
20// Network topology
21//
22// n0 n1
23// | |
24// =======
25// LAN (CSMA)
26//
27// - UDP flow from n0 to n1 of 1024 byte packets at intervals of 50 ms
28// - maximum of 320 packets sent (or limited by simulation duration)
29// - option to use IPv4 or IPv6 addressing
30// - option to disable logging statements
31
32#include "ns3/applications-module.h"
33#include "ns3/core-module.h"
34#include "ns3/csma-module.h"
35#include "ns3/internet-module.h"
36
37#include <fstream>
38
39using namespace ns3;
40
41NS_LOG_COMPONENT_DEFINE("UdpClientServerExample");
42
43int
44main(int argc, char* argv[])
45{
46 // Declare variables used in command-line arguments
47 bool useV6 = false;
48 bool logging = true;
49 Address serverAddress;
50
51 CommandLine cmd(__FILE__);
52 cmd.AddValue("useIpv6", "Use Ipv6", useV6);
53 cmd.AddValue("logging", "Enable logging", logging);
54 cmd.Parse(argc, argv);
55
56 if (logging)
57 {
60 }
61
62 NS_LOG_INFO("Create nodes in above topology.");
64 n.Create(2);
65
67 internet.Install(n);
68
69 NS_LOG_INFO("Create channel between the two nodes.");
71 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
72 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
73 csma.SetDeviceAttribute("Mtu", UintegerValue(1400));
74 NetDeviceContainer d = csma.Install(n);
75
76 NS_LOG_INFO("Assign IP Addresses.");
77 if (!useV6)
78 {
80 ipv4.SetBase("10.1.1.0", "255.255.255.0");
81 Ipv4InterfaceContainer i = ipv4.Assign(d);
82 serverAddress = Address(i.GetAddress(1));
83 }
84 else
85 {
87 ipv6.SetBase("2001:0000:f00d:cafe::", Ipv6Prefix(64));
88 Ipv6InterfaceContainer i6 = ipv6.Assign(d);
89 serverAddress = Address(i6.GetAddress(1, 1));
90 }
91
92 NS_LOG_INFO("Create UdpServer application on node 1.");
93 uint16_t port = 4000;
95 ApplicationContainer apps = server.Install(n.Get(1));
96 apps.Start(Seconds(1.0));
97 apps.Stop(Seconds(10.0));
98
99 NS_LOG_INFO("Create UdpClient application on node 0 to send to node 1.");
100 uint32_t MaxPacketSize = 1024;
101 Time interPacketInterval = Seconds(0.05);
102 uint32_t maxPacketCount = 320;
103 UdpClientHelper client(serverAddress, port);
104 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
105 client.SetAttribute("Interval", TimeValue(interPacketInterval));
106 client.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
107 apps = client.Install(n.Get(0));
108 apps.Start(Seconds(2.0));
109 apps.Stop(Seconds(10.0));
110
111 NS_LOG_INFO("Run Simulation.");
114 NS_LOG_INFO("Done.");
115
116 return 0;
117}
a polymophic address class
Definition: address.h:101
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.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
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.
Definition: ipv6-address.h:455
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:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1406
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Create a server application which waits for input UDP packets and uses the information carried into t...
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
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:302
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63