A Discrete-Event Network Simulator
API
 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
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
36 {
38 }
39 
40 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)
void RunSimulation(void)
Definition: main-simple.cc:41
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Definition: packet.h:650
int main(int argc, char *argv[])
Definition: main-simple.cc:68
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
double GetSeconds(void) const
Definition: nstime.h:274
static void GenerateTraffic(Ptr< Socket > socket, uint32_t size)
Definition: main-simple.cc:10
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
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:71
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
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
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
static Ipv4Address GetLoopback(void)
static void PrintTraffic(Ptr< Socket > socket)
Definition: main-simple.cc:35
Ptr< Node > Get(uint32_t i) const
Get the Ptr 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.
static void SocketPrinter(Ptr< Socket > socket)
Definition: main-simple.cc:25
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:49
static TypeId LookupByName(std::string name)
Definition: type-id.cc:536