A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
udp-trace-client-server.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 // Network topology
18 //
19 // n0 n1
20 // | |
21 // =======
22 // LAN
23 //
24 // - UDP flows from n0 to n1
25 
26 #include <fstream>
27 #include "ns3/core-module.h"
28 #include "ns3/csma-module.h"
29 #include "ns3/applications-module.h"
30 #include "ns3/internet-module.h"
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("UdpTraceClientServerExample");
35 
36 int
37 main (int argc, char *argv[])
38 {
39 //
40 // Enable logging for UdpClient and
41 //
42  LogComponentEnable ("UdpTraceClient", LOG_LEVEL_INFO);
43  LogComponentEnable ("UdpServer", LOG_LEVEL_INFO);
44 
45  bool useV6 = false;
46  Address serverAddress;
47 
48  CommandLine cmd;
49  cmd.AddValue ("useIpv6", "Use Ipv6", useV6);
50  cmd.Parse (argc, argv);
51 
52 //
53 // Explicitly create the nodes required by the topology (shown above).
54 //
55  NS_LOG_INFO ("Create nodes.");
56  NodeContainer n;
57  n.Create (2);
58 
59  InternetStackHelper internet;
60  internet.Install (n);
61 
62  NS_LOG_INFO ("Create channels.");
63 //
64 // Explicitly create the channels required by the topology (shown above).
65 //
66  CsmaHelper csma;
67  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
68  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
69  csma.SetDeviceAttribute ("Mtu", UintegerValue (1500));
70  NetDeviceContainer d = csma.Install (n);
71 
72 //
73 // We've got the "hardware" in place. Now we need to add IP addresses.
74 //
75  NS_LOG_INFO ("Assign IP Addresses.");
76  if (useV6 == false)
77  {
78  Ipv4AddressHelper ipv4;
79  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
80  Ipv4InterfaceContainer i = ipv4.Assign (d);
81  serverAddress = Address (i.GetAddress (1));
82  }
83  else
84  {
85  Ipv6AddressHelper ipv6;
86  ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64));
87  Ipv6InterfaceContainer i6 = ipv6.Assign (d);
88  serverAddress = Address(i6.GetAddress (1,1));
89  }
90 
91  NS_LOG_INFO ("Create Applications.");
92 //
93 // Create one udpServer applications on node one.
94 //
95  uint16_t port = 4000;
96  UdpServerHelper server (port);
97  ApplicationContainer apps = server.Install (n.Get (1));
98  apps.Start (Seconds (1.0));
99  apps.Stop (Seconds (10.0));
100 
101 //
102 // Create one UdpTraceClient application to send UDP datagrams from node zero to
103 // node one.
104 //
105  uint32_t MaxPacketSize = 1472; // Back off 20 (IP) + 8 (UDP) bytes from MTU
106  UdpTraceClientHelper client (serverAddress, port,"");
107  client.SetAttribute ("MaxPacketSize", UintegerValue (MaxPacketSize));
108  apps = client.Install (n.Get (0));
109  apps.Start (Seconds (2.0));
110  apps.Stop (Seconds (10.0));
111 
112 //
113 // Now, do the actual simulation.
114 //
115  NS_LOG_INFO ("Run Simulation.");
116  Simulator::Run ();
118  NS_LOG_INFO ("Done.");
119 }
holds a vector of ns3::Application pointers.
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
Keep track of a set of IPv6 interfaces.
holds a vector of std::pair of Ptr and interface index.
void SetAttribute(std::string name, const AttributeValue &value)
static void Run(void)
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
NetDeviceContainer Install(Ptr< Node > node) const
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Definition: log.h:264
ApplicationContainer Install(NodeContainer c)
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Class for representing data rates.
Definition: data-rate.h:71
int main(int argc, char *argv[])
hold objects of type ns3::Time
Definition: nstime.h:828
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
Create a server application which waits for input udp packets and uses the information carried into t...
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Definition: simulator.cc:121
keep track of a set of node pointers.
void Install(std::string nodeName) const
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:63
Helper class to auto-assign global IPv6 unicast addresses.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
NS_LOG_COMPONENT_DEFINE("PacketLossCounter")
ApplicationContainer Install(NodeContainer c)
hold objects of type ns3::DataRate
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:408
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask.
Definition: ipv6-address.h:364
void Parse(int argc, char *argv[])
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const