A Discrete-Event Network Simulator
API
main-simple.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
18 */
19
20#include "ns3/core-module.h"
21#include "ns3/internet-module.h"
22#include "ns3/network-module.h"
23
24#include <iostream>
25
26using namespace ns3;
27
38static void
40{
41 if (size <= 0)
42 {
43 socket->Close();
44 return;
45 }
46
47 std::cout << "at=" << Simulator::Now().GetSeconds() << "s, tx bytes=" << size << std::endl;
48 socket->Send(Create<Packet>(size));
49 Simulator::Schedule(Seconds(0.5), &GenerateTraffic, socket, size - 50);
50}
51
56static void
58{
59 Ptr<Packet> packet;
60 while ((packet = socket->Recv()))
61 {
62 std::cout << "at=" << Simulator::Now().GetSeconds() << "s, rx bytes=" << packet->GetSize()
63 << std::endl;
64 }
65}
66
67int
68main(int argc, char* argv[])
69{
70 CommandLine cmd(__FILE__);
71 cmd.Parse(argc, argv);
72
74 c.Create(1);
75
76 InternetStackHelper internet;
77 internet.Install(c);
78
79 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
80 Ptr<Socket> sink = Socket::CreateSocket(c.Get(0), tid);
81 InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 80);
82 sink->Bind(local);
83
84 Ptr<Socket> source = Socket::CreateSocket(c.Get(0), tid);
85 InetSocketAddress remote = InetSocketAddress(Ipv4Address::GetLoopback(), 80);
86 source->Connect(remote);
87
88 GenerateTraffic(source, 500);
89 sink->SetRecvCallback(MakeCallback(&SocketPrinter));
90
91 Simulator::Run();
92
93 Simulator::Destroy();
94
95 return 0;
96}
Parse command-line arguments.
Definition: command-line.h:232
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Close()=0
Close a socket.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
a unique identifier for an interface.
Definition: type-id.h:60
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
static void GenerateTraffic(Ptr< Socket > socket, int32_t size)
Generates traffic.
Definition: main-simple.cc:39
static void SocketPrinter(Ptr< Socket > socket)
Prints the packets received by a socket.
Definition: main-simple.cc:57
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:691
cmd
Definition: second.py:33
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55