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-echo-server.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright 2007 University of Washington
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*/
18
19
#include "ns3/log.h"
20
#include "ns3/ipv4-address.h"
21
#include "ns3/ipv6-address.h"
22
#include "ns3/address-utils.h"
23
#include "ns3/nstime.h"
24
#include "ns3/inet-socket-address.h"
25
#include "ns3/inet6-socket-address.h"
26
#include "ns3/socket.h"
27
#include "ns3/udp-socket.h"
28
#include "ns3/simulator.h"
29
#include "ns3/socket-factory.h"
30
#include "ns3/packet.h"
31
#include "ns3/uinteger.h"
32
33
#include "
udp-echo-server.h
"
34
35
namespace
ns3 {
36
37
NS_LOG_COMPONENT_DEFINE
(
"UdpEchoServerApplication"
);
38
NS_OBJECT_ENSURE_REGISTERED
(UdpEchoServer);
39
40
TypeId
41
UdpEchoServer::GetTypeId
(
void
)
42
{
43
static
TypeId
tid =
TypeId
(
"ns3::UdpEchoServer"
)
44
.
SetParent
<
Application
> ()
45
.AddConstructor<UdpEchoServer> ()
46
.AddAttribute (
"Port"
,
"Port on which we listen for incoming packets."
,
47
UintegerValue
(9),
48
MakeUintegerAccessor (&
UdpEchoServer::m_port
),
49
MakeUintegerChecker<uint16_t> ())
50
;
51
return
tid;
52
}
53
54
UdpEchoServer::UdpEchoServer
()
55
{
56
NS_LOG_FUNCTION_NOARGS
();
57
}
58
59
UdpEchoServer::~UdpEchoServer
()
60
{
61
NS_LOG_FUNCTION_NOARGS
();
62
m_socket
= 0;
63
m_socket6
= 0;
64
}
65
66
void
67
UdpEchoServer::DoDispose
(
void
)
68
{
69
NS_LOG_FUNCTION_NOARGS
();
70
Application::DoDispose
();
71
}
72
73
void
74
UdpEchoServer::StartApplication
(
void
)
75
{
76
NS_LOG_FUNCTION_NOARGS
();
77
78
if
(
m_socket
== 0)
79
{
80
TypeId
tid =
TypeId::LookupByName
(
"ns3::UdpSocketFactory"
);
81
m_socket
=
Socket::CreateSocket
(
GetNode
(), tid);
82
InetSocketAddress
local =
InetSocketAddress
(
Ipv4Address::GetAny
(),
m_port
);
83
m_socket
->
Bind
(local);
84
if
(
addressUtils::IsMulticast
(
m_local
))
85
{
86
Ptr<UdpSocket>
udpSocket = DynamicCast<UdpSocket> (
m_socket
);
87
if
(udpSocket)
88
{
89
// equivalent to setsockopt (MCAST_JOIN_GROUP)
90
udpSocket->MulticastJoinGroup (0,
m_local
);
91
}
92
else
93
{
94
NS_FATAL_ERROR
(
"Error: Failed to join multicast group"
);
95
}
96
}
97
}
98
99
if
(
m_socket6
== 0)
100
{
101
TypeId
tid =
TypeId::LookupByName
(
"ns3::UdpSocketFactory"
);
102
m_socket6
=
Socket::CreateSocket
(
GetNode
(), tid);
103
Inet6SocketAddress
local6 =
Inet6SocketAddress
(
Ipv6Address::GetAny
(),
m_port
);
104
m_socket6
->
Bind
(local6);
105
if
(
addressUtils::IsMulticast
(local6))
106
{
107
Ptr<UdpSocket>
udpSocket = DynamicCast<UdpSocket> (
m_socket6
);
108
if
(udpSocket)
109
{
110
// equivalent to setsockopt (MCAST_JOIN_GROUP)
111
udpSocket->MulticastJoinGroup (0, local6);
112
}
113
else
114
{
115
NS_FATAL_ERROR
(
"Error: Failed to join multicast group"
);
116
}
117
}
118
}
119
120
m_socket
->
SetRecvCallback
(
MakeCallback
(&
UdpEchoServer::HandleRead
,
this
));
121
m_socket6
->
SetRecvCallback
(
MakeCallback
(&
UdpEchoServer::HandleRead
,
this
));
122
}
123
124
void
125
UdpEchoServer::StopApplication
()
126
{
127
NS_LOG_FUNCTION_NOARGS
();
128
129
if
(
m_socket
!= 0)
130
{
131
m_socket
->
Close
();
132
m_socket
->
SetRecvCallback
(
MakeNullCallback
<
void
,
Ptr<Socket>
> ());
133
}
134
if
(
m_socket6
!= 0)
135
{
136
m_socket6
->
Close
();
137
m_socket6
->
SetRecvCallback
(
MakeNullCallback
<
void
,
Ptr<Socket>
> ());
138
}
139
}
140
141
void
142
UdpEchoServer::HandleRead
(
Ptr<Socket>
socket)
143
{
144
Ptr<Packet>
packet;
145
Address
from;
146
while
((packet = socket->
RecvFrom
(from)))
147
{
148
if
(
InetSocketAddress::IsMatchingType
(from))
149
{
150
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s server received "
<< packet->
GetSize
() <<
" bytes from "
<<
151
InetSocketAddress::ConvertFrom
(from).
GetIpv4
() <<
" port "
<<
152
InetSocketAddress::ConvertFrom
(from).
GetPort
());
153
}
154
else
if
(
Inet6SocketAddress::IsMatchingType
(from))
155
{
156
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s server received "
<< packet->
GetSize
() <<
" bytes from "
<<
157
Inet6SocketAddress::ConvertFrom
(from).
GetIpv6
() <<
" port "
<<
158
InetSocketAddress::ConvertFrom
(from).
GetPort
());
159
}
160
161
packet->
RemoveAllPacketTags
();
162
packet->
RemoveAllByteTags
();
163
164
NS_LOG_LOGIC
(
"Echoing packet"
);
165
socket->
SendTo
(packet, 0, from);
166
167
if
(
InetSocketAddress::IsMatchingType
(from))
168
{
169
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s server sent "
<< packet->
GetSize
() <<
" bytes to "
<<
170
InetSocketAddress::ConvertFrom
(from).
GetIpv4
() <<
" port "
<<
171
InetSocketAddress::ConvertFrom
(from).
GetPort
());
172
}
173
else
if
(
Inet6SocketAddress::IsMatchingType
(from))
174
{
175
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s server sent "
<< packet->
GetSize
() <<
" bytes to "
<<
176
Inet6SocketAddress::ConvertFrom
(from).
GetIpv6
() <<
" port "
<<
177
InetSocketAddress::ConvertFrom
(from).
GetPort
());
178
}
179
}
180
}
181
182
}
// Namespace ns3
src
applications
model
udp-echo-server.cc
Generated on Tue Oct 9 2012 16:45:33 for ns-3 by
1.8.1.2