A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
packet-socket-apps.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2014 Universita' di Firenze
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18
*/
19
20
// Network topology
21
//
22
// n0 n1
23
// | |
24
// =================
25
// SimpleChannel
26
//
27
// - Packets flows from n0 to n1
28
//
29
// This example shows how to use the PacketSocketServer and PacketSocketClient
30
// to send non-IP packets over a SimpleNetDevice
31
32
#include "ns3/core-module.h"
33
#include "ns3/network-module.h"
34
35
using namespace
ns3
;
36
37
int
38
main(
int
argc,
char
* argv[])
39
{
40
bool
verbose
=
false
;
41
42
CommandLine
cmd
(__FILE__);
43
cmd
.AddValue(
"verbose"
,
"turn on log components"
,
verbose
);
44
cmd
.Parse(argc, argv);
45
46
if
(
verbose
)
47
{
48
LogComponentEnable
(
"PacketSocketServer"
,
LOG_LEVEL_ALL
);
49
LogComponentEnable
(
"PacketSocketClient"
,
LOG_LEVEL_ALL
);
50
LogComponentEnable
(
"SimpleNetDevice"
,
LOG_LEVEL_ALL
);
51
}
52
53
NodeContainer
nodes
;
54
nodes
.
Create
(2);
55
56
ns3::PacketMetadata::Enable
();
57
58
PacketSocketHelper
packetSocket;
59
60
// give packet socket powers to nodes.
61
packetSocket.
Install
(
nodes
);
62
63
Ptr<SimpleNetDevice>
txDev;
64
txDev = CreateObject<SimpleNetDevice>();
65
nodes
.
Get
(0)->
AddDevice
(txDev);
66
67
Ptr<SimpleNetDevice>
rxDev;
68
rxDev = CreateObject<SimpleNetDevice>();
69
nodes
.
Get
(1)->
AddDevice
(rxDev);
70
71
Ptr<SimpleChannel>
channel
= CreateObject<SimpleChannel>();
72
txDev->SetChannel(
channel
);
73
rxDev->SetChannel(
channel
);
74
txDev->SetNode(
nodes
.
Get
(0));
75
rxDev->SetNode(
nodes
.
Get
(1));
76
77
PacketSocketAddress
socketAddr;
78
socketAddr.
SetSingleDevice
(txDev->GetIfIndex());
79
socketAddr.
SetPhysicalAddress
(rxDev->GetAddress());
80
// Arbitrary protocol type.
81
// Note: PacketSocket doesn't have any L4 multiplexing or demultiplexing
82
// The only mux/demux is based on the protocol field
83
socketAddr.
SetProtocol
(1);
84
85
Ptr<PacketSocketClient>
client
= CreateObject<PacketSocketClient>();
86
client
->SetRemote(socketAddr);
87
nodes
.
Get
(0)->
AddApplication
(
client
);
88
89
Ptr<PacketSocketServer>
server
= CreateObject<PacketSocketServer>();
90
server
->SetLocal(socketAddr);
91
nodes
.
Get
(1)->
AddApplication
(
server
);
92
93
Simulator::Run
();
94
Simulator::Destroy
();
95
return
0;
96
}
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:232
ns3::NodeContainer
keep track of a set of node pointers.
Definition:
node-container.h:40
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition:
node-container.cc:84
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition:
node-container.cc:78
ns3::Node::AddDevice
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition:
node.cc:138
ns3::Node::AddApplication
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition:
node.cc:169
ns3::PacketMetadata::Enable
static void Enable()
Enable the packet metadata.
Definition:
packet-metadata.cc:55
ns3::PacketSocketAddress
an address for a packet socket
Definition:
packet-socket-address.h:40
ns3::PacketSocketAddress::SetProtocol
void SetProtocol(uint16_t protocol)
Set the protocol.
Definition:
packet-socket-address.cc:35
ns3::PacketSocketAddress::SetPhysicalAddress
void SetPhysicalAddress(const Address address)
Set the destination address.
Definition:
packet-socket-address.cc:58
ns3::PacketSocketAddress::SetSingleDevice
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Definition:
packet-socket-address.cc:50
ns3::PacketSocketHelper
Give ns3::PacketSocket powers to ns3::Node.
Definition:
packet-socket-helper.h:32
ns3::PacketSocketHelper::Install
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Definition:
packet-socket-helper.cc:38
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:77
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:142
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition:
simulator.cc:178
nodes
NodeContainer nodes
Definition:
lr-wpan-bootstrap.cc:53
brite-generic-example.client
client
Definition:
brite-generic-example.py:61
brite-generic-example.server
server
Definition:
brite-generic-example.py:62
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LogComponentEnable
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition:
log.cc:302
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition:
log.h:116
second.cmd
cmd
Definition:
second.py:40
third.channel
channel
Definition:
third.py:88
verbose
bool verbose
Definition:
openflow-switch.cc:53
src
network
examples
packet-socket-apps.cc
Generated on Sun Mar 3 2024 17:11:05 for ns-3 by
1.9.1