A Discrete-Event Network Simulator
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
24using namespace ns3;
25
26int
27main(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
43 nodes.Create(2);
44
46
47 PacketSocketHelper packetSocket;
48
49 // give packet socket powers to nodes.
50 packetSocket.Install(nodes);
51
54 nodes.Get(0)->AddDevice(txDev);
55
58 nodes.Get(1)->AddDevice(rxDev);
59
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
75 client->SetRemote(socketAddr);
76 nodes.Get(0)->AddApplication(client);
77
79 server->SetLocal(socketAddr);
80 nodes.Get(1)->AddApplication(server);
81
84 return 0;
85}
Parse command-line arguments.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition node.cc:124
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition node.cc:153
static void Enable()
Enable the packet metadata.
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
channel
Definition third.py:77
bool verbose