A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
packet-socket-apps.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2014 Universita' di Firenze
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7
*/
8
9
// Network topology
10
//
11
// n0 n1
12
// | |
13
// =================
14
// SimpleChannel
15
//
16
// - Packets flows from n0 to n1
17
//
18
// This example shows how to use the PacketSocketServer and PacketSocketClient
19
// to send non-IP packets over a SimpleNetDevice
20
21
#include "ns3/core-module.h"
22
#include "ns3/network-module.h"
23
24
using namespace
ns3
;
25
26
int
27
main(
int
argc,
char
* argv[])
28
{
29
bool
verbose
=
false
;
30
31
CommandLine
cmd
(__FILE__);
32
cmd
.AddValue(
"verbose"
,
"turn on log components"
,
verbose
);
33
cmd
.Parse(argc, argv);
34
35
if
(
verbose
)
36
{
37
LogComponentEnable
(
"PacketSocketServer"
,
LOG_LEVEL_ALL
);
38
LogComponentEnable
(
"PacketSocketClient"
,
LOG_LEVEL_ALL
);
39
LogComponentEnable
(
"SimpleNetDevice"
,
LOG_LEVEL_ALL
);
40
}
41
42
NodeContainer
nodes
;
43
nodes
.
Create
(2);
44
45
ns3::PacketMetadata::Enable
();
46
47
PacketSocketHelper
packetSocket;
48
49
// give packet socket powers to nodes.
50
packetSocket.
Install
(
nodes
);
51
52
Ptr<SimpleNetDevice>
txDev;
53
txDev =
CreateObject<SimpleNetDevice>
();
54
nodes
.
Get
(0)->
AddDevice
(txDev);
55
56
Ptr<SimpleNetDevice>
rxDev;
57
rxDev =
CreateObject<SimpleNetDevice>
();
58
nodes
.
Get
(1)->
AddDevice
(rxDev);
59
60
Ptr<SimpleChannel>
channel
=
CreateObject<SimpleChannel>
();
61
txDev->SetChannel(channel);
62
rxDev->SetChannel(channel);
63
txDev->SetNode(
nodes
.
Get
(0));
64
rxDev->SetNode(
nodes
.
Get
(1));
65
66
PacketSocketAddress
socketAddr;
67
socketAddr.
SetSingleDevice
(txDev->GetIfIndex());
68
socketAddr.
SetPhysicalAddress
(rxDev->GetAddress());
69
// Arbitrary protocol type.
70
// Note: PacketSocket doesn't have any L4 multiplexing or demultiplexing
71
// The only mux/demux is based on the protocol field
72
socketAddr.
SetProtocol
(1);
73
74
Ptr<PacketSocketClient>
client
=
CreateObject<PacketSocketClient>
();
75
client
->SetRemote(socketAddr);
76
nodes
.
Get
(0)->
AddApplication
(client);
77
78
Ptr<PacketSocketServer>
server
=
CreateObject<PacketSocketServer>
();
79
server
->SetLocal(socketAddr);
80
nodes
.
Get
(1)->
AddApplication
(server);
81
82
Simulator::Run
();
83
Simulator::Destroy
();
84
return
0;
85
}
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::NodeContainer
keep track of a set of node pointers.
Definition
node-container.h:29
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:73
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:67
ns3::Node::AddDevice
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition
node.cc:124
ns3::Node::AddApplication
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition
node.cc:153
ns3::PacketMetadata::Enable
static void Enable()
Enable the packet metadata.
Definition
packet-metadata.cc:44
ns3::PacketSocketAddress
an address for a packet socket
Definition
packet-socket-address.h:29
ns3::PacketSocketAddress::SetProtocol
void SetProtocol(uint16_t protocol)
Set the protocol.
Definition
packet-socket-address.cc:24
ns3::PacketSocketAddress::SetPhysicalAddress
void SetPhysicalAddress(const Address address)
Set the destination address.
Definition
packet-socket-address.cc:47
ns3::PacketSocketAddress::SetSingleDevice
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Definition
packet-socket-address.cc:39
ns3::PacketSocketHelper
Give ns3::PacketSocket powers to ns3::Node.
Definition
packet-socket-helper.h:21
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:27
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:619
nodes
NodeContainer nodes
Definition
lr-wpan-bootstrap.cc:43
brite-generic-example.client
client
Definition
brite-generic-example.py:50
brite-generic-example.server
server
Definition
brite-generic-example.py:51
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:291
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition
log.h:105
second.cmd
cmd
Definition
second.py:29
third.channel
channel
Definition
third.py:77
verbose
bool verbose
Definition
openflow-switch.cc:42
src
network
examples
packet-socket-apps.cc
Generated on Fri Nov 8 2024 13:59:04 for ns-3 by
1.11.0