A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
udp-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/core-module.h"
29
#include "ns3/csma-module.h"
30
#include "ns3/applications-module.h"
31
#include "ns3/internet-module.h"
32
33
using namespace
ns3;
34
35
NS_LOG_COMPONENT_DEFINE
(
"UdpClientServerExample"
);
36
37
int
38
main
(
int
argc,
char
*argv[])
39
{
40
//
41
// Enable logging for UdpClient and
42
//
43
LogComponentEnable
(
"UdpClient"
,
LOG_LEVEL_INFO
);
44
LogComponentEnable
(
"UdpServer"
,
LOG_LEVEL_INFO
);
45
46
bool
useV6 =
false
;
47
Address
serverAddress;
48
49
CommandLine
cmd;
50
cmd.
AddValue
(
"useIpv6"
,
"Use Ipv6"
, useV6);
51
cmd.
Parse
(argc, argv);
52
53
//
54
// Explicitly create the nodes required by the topology (shown above).
55
//
56
NS_LOG_INFO
(
"Create nodes."
);
57
NodeContainer
n;
58
n.
Create
(2);
59
60
InternetStackHelper
internet;
61
internet.
Install
(n);
62
63
NS_LOG_INFO
(
"Create channels."
);
64
//
65
// Explicitly create the channels required by the topology (shown above).
66
//
67
CsmaHelper
csma;
68
csma.
SetChannelAttribute
(
"DataRate"
,
DataRateValue
(
DataRate
(5000000)));
69
csma.
SetChannelAttribute
(
"Delay"
,
TimeValue
(MilliSeconds (2)));
70
csma.
SetDeviceAttribute
(
"Mtu"
,
UintegerValue
(1400));
71
NetDeviceContainer
d = csma.
Install
(n);
72
73
//
74
// We've got the "hardware" in place. Now we need to add IP addresses.
75
//
76
NS_LOG_INFO
(
"Assign IP Addresses."
);
77
if
(useV6 ==
false
)
78
{
79
Ipv4AddressHelper
ipv4;
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
{
86
Ipv6AddressHelper
ipv6;
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 Applications."
);
93
//
94
// Create one udpServer applications on node one.
95
//
96
uint16_t
port
= 4000;
97
UdpServerHelper
server (port);
98
ApplicationContainer
apps = server.
Install
(n.
Get
(1));
99
apps.
Start
(Seconds (1.0));
100
apps.
Stop
(Seconds (10.0));
101
102
//
103
// Create one UdpClient application to send UDP datagrams from node zero to
104
// node one.
105
//
106
uint32_t MaxPacketSize = 1024;
107
Time
interPacketInterval = Seconds (0.05);
108
uint32_t maxPacketCount = 320;
109
UdpClientHelper
client (serverAddress, port);
110
client.
SetAttribute
(
"MaxPackets"
,
UintegerValue
(maxPacketCount));
111
client.
SetAttribute
(
"Interval"
,
TimeValue
(interPacketInterval));
112
client.
SetAttribute
(
"PacketSize"
,
UintegerValue
(MaxPacketSize));
113
apps = client.
Install
(n.
Get
(0));
114
apps.
Start
(Seconds (2.0));
115
apps.
Stop
(Seconds (10.0));
116
117
//
118
// Now, do the actual simulation.
119
//
120
NS_LOG_INFO
(
"Run Simulation."
);
121
Simulator::Run
();
122
Simulator::Destroy
();
123
NS_LOG_INFO
(
"Done."
);
124
}
examples
udp-client-server
udp-client-server.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2