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
main-simple.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2007 INRIA
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
7
*/
8
9
#include "ns3/core-module.h"
10
#include "ns3/internet-module.h"
11
#include "ns3/network-module.h"
12
13
#include <iostream>
14
15
using namespace
ns3
;
16
17
/**
18
* Generates traffic.
19
*
20
* The first call sends a packet of the specified size, and then
21
* the function is scheduled to send a packet of (size-50) after 0.5s.
22
* The process is iterated until the packet size is zero.
23
*
24
* @param socket output socket
25
* @param size packet size
26
*/
27
static
void
28
GenerateTraffic
(
Ptr<Socket>
socket,
int32_t
size)
29
{
30
if
(size <= 0)
31
{
32
socket->Close();
33
return
;
34
}
35
36
std::cout <<
"at="
<<
Simulator::Now
().
GetSeconds
() <<
"s, tx bytes="
<< size << std::endl;
37
socket->Send(
Create<Packet>
(size));
38
Simulator::Schedule
(
Seconds
(0.5), &
GenerateTraffic
, socket, size - 50);
39
}
40
41
/**
42
* Prints the packets received by a socket
43
* @param socket input socket
44
*/
45
static
void
46
SocketPrinter
(
Ptr<Socket>
socket)
47
{
48
Ptr<Packet>
packet;
49
while
((packet = socket->Recv()))
50
{
51
std::cout <<
"at="
<<
Simulator::Now
().
GetSeconds
() <<
"s, rx bytes="
<< packet->GetSize()
52
<< std::endl;
53
}
54
}
55
56
int
57
main(
int
argc
,
char
*
argv
[])
58
{
59
CommandLine
cmd
(
__FILE__
);
60
cmd
.Parse(
argc
,
argv
);
61
62
NodeContainer
c
;
63
c
.
Create
(1);
64
65
InternetStackHelper
internet
;
66
internet
.Install(
c
);
67
68
TypeId
tid =
TypeId::LookupByName
(
"ns3::UdpSocketFactory"
);
69
Ptr<Socket>
sink
=
Socket::CreateSocket
(
c
.Get(0), tid);
70
InetSocketAddress
local
=
InetSocketAddress
(
Ipv4Address::GetAny
(), 80);
71
sink
->Bind(
local
);
72
73
Ptr<Socket>
source
=
Socket::CreateSocket
(
c
.Get(0), tid);
74
InetSocketAddress
remote
=
InetSocketAddress
(
Ipv4Address::GetLoopback
(), 80);
75
source
->Connect(
remote
);
76
77
GenerateTraffic
(
source
, 500);
78
sink
->SetRecvCallback(
MakeCallback
(&
SocketPrinter
));
79
80
Simulator::Run
();
81
82
Simulator::Destroy
();
83
84
return
0;
85
}
int32_t
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::InetSocketAddress
an Inet address class
Definition
inet-socket-address.h:31
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition
internet-stack-helper.h:81
ns3::Ipv4Address::GetLoopback
static Ipv4Address GetLoopback()
Definition
ipv4-address.cc:384
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny()
Definition
ipv4-address.cc:368
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::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition
simulator.h:560
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Now
static Time Now()
Return the current simulation virtual time.
Definition
simulator.cc:197
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::Socket::CreateSocket
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition
socket.cc:61
ns3::Time::GetSeconds
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition
nstime.h:392
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:49
ns3::TypeId::LookupByName
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition
type-id.cc:872
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
GenerateTraffic
static void GenerateTraffic(Ptr< Socket > socket, int32_t size)
Generates traffic.
Definition
main-simple.cc:28
SocketPrinter
static void SocketPrinter(Ptr< Socket > socket)
Prints the packets received by a socket.
Definition
main-simple.cc:46
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeCallback
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition
callback.h:684
nsclick-simple-lan.internet
internet
Definition
nsclick-simple-lan.py:34
second.cmd
cmd
Definition
second.py:29
sink
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition
wifi-tcp.cc:44
src
internet
examples
main-simple.cc
Generated on Mon Dec 15 2025 15:21:52 for ns-3 by
1.9.8