A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
main-simple.cc
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);
53  sink->Bind (local);
54 
55  Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid);
57  source->Connect (remote);
58 
59  GenerateTraffic (source, 500);
60  PrintTraffic (sink);
61 
62 
63  Simulator::Run ();
64 
66 }
67 
68 int main (int argc, char *argv[])
69 {
70  RunSimulation ();
71 
72  return 0;
73 }
an Inet address class
static Ipv4Address GetAny(void)
static void Run(void)
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Definition: packet.h:620
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
double GetSeconds(void) const
Definition: nstime.h:262
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
Definition: socket.cc:70
static void Destroy(void)
Definition: simulator.cc:121
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void Install(std::string nodeName) const
static Time Now(void)
Definition: simulator.cc:179
static Ipv4Address GetLoopback(void)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
a unique identifier for an interface.
Definition: type-id.h:44
static TypeId LookupByName(std::string name)
Definition: type-id.cc:415