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
main-simple.cc
Go to the documentation of this file.
1
#include <iostream>
2
3
#include "ns3/core-module.h"
4
#include "ns3/network-module.h"
5
#include "ns3/internet-module.h"
6
7
using namespace
ns3;
8
9
static
void
10
GenerateTraffic
(
Ptr<Socket>
socket, uint32_t size)
11
{
12
std::cout <<
"at="
<<
Simulator::Now
().
GetSeconds
() <<
"s, tx bytes="
<< size << std::endl;
13
socket->
Send
(Create<Packet> (size));
14
if
(size > 0)
15
{
16
Simulator::Schedule
(
Seconds
(0.5), &
GenerateTraffic
, socket, size - 50);
17
}
18
else
19
{
20
socket->
Close
();
21
}
22
}
23
24
static
void
25
SocketPrinter
(
Ptr<Socket>
socket)
26
{
27
Ptr<Packet>
packet;
28
while
((packet = socket->
Recv
()))
29
{
30
std::cout <<
"at="
<<
Simulator::Now
().
GetSeconds
() <<
"s, rx bytes="
<< packet->
GetSize
() << std::endl;
31
}
32
}
33
34
static
void
35
PrintTraffic
(
Ptr<Socket>
socket)
36
{
37
socket->
SetRecvCallback
(
MakeCallback
(&
SocketPrinter
));
38
}
39
40
void
41
RunSimulation
(
void
)
42
{
43
NodeContainer
c;
44
c.
Create
(1);
45
46
InternetStackHelper
internet;
47
internet.
Install
(c);
48
49
50
TypeId
tid =
TypeId::LookupByName
(
"ns3::UdpSocketFactory"
);
51
Ptr<Socket>
sink =
Socket::CreateSocket
(c.
Get
(0), tid);
52
InetSocketAddress
local =
InetSocketAddress
(
Ipv4Address::GetAny
(), 80);
53
sink->
Bind
(local);
54
55
Ptr<Socket>
source =
Socket::CreateSocket
(c.
Get
(0), tid);
56
InetSocketAddress
remote =
InetSocketAddress
(
Ipv4Address::GetLoopback
(), 80);
57
source->
Connect
(remote);
58
59
GenerateTraffic
(source, 500);
60
PrintTraffic
(sink);
61
62
63
Simulator::Run
();
64
65
Simulator::Destroy
();
66
}
67
68
int
main
(
int
argc,
char
*argv[])
69
{
70
RunSimulation
();
71
72
return
0;
73
}
src
internet
examples
main-simple.cc
Generated on Fri Dec 21 2012 19:00:35 for ns-3 by
1.8.1.2