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 }