-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02.Ping.cc
More file actions
100 lines (78 loc) · 2.84 KB
/
Copy path02.Ping.cc
File metadata and controls
100 lines (78 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
2. Implement transmission of ping messages/trace route over a network topology
consisting of 6 nodes and find the number of packets dropped due to congestion.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/internet-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("CsmaPingExample");
static void PingRtt(std::string context, Time rtt)
{
std::cout << context << " " << rtt << std::endl;
}
int main(int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse(argc, argv);
// Here, we will explicitly create four nodes.
NS_LOG_INFO("Create nodes.");
NodeContainer c;
c.Create(6);
// connect all our nodes to a shared channel.
NS_LOG_INFO("Build Topology.");
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(10000)));
csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(0.2)));
NetDeviceContainer devs = csma.Install(c);
// add an ip stack to all nodes.
NS_LOG_INFO("Add ip stack.");
InternetStackHelper ipStack;
ipStack.Install(c);
// assign ip addresses
NS_LOG_INFO("Assign ip addresses.");
Ipv4AddressHelper ip;
ip.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer addresses = ip.Assign(devs);
NS_LOG_INFO("Create Sink.");
// Create an OnOff application to send UDP datagrams from node zero to //node 1.
NS_LOG_INFO("Create Applications.");
uint16_t port = 9; // Discard port (RFC 863)
OnOffHelper onoff("ns3::UdpSocketFactory",
Address(InetSocketAddress(addresses.GetAddress(2), port)));
onoff.SetConstantRate(DataRate("500Mb/s"));
ApplicationContainer app = onoff.Install(c.Get(0));
// Start the application
app.Start(Seconds(6.0));
app.Stop(Seconds(10.0));
// Create an optional packet sink to receive these packets
PacketSinkHelper sink("ns3::UdpSocketFactory",
Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
app = sink.Install(c.Get(2));
app.Start(Seconds(0.0));
NS_LOG_INFO("Create pinger");
V4PingHelper ping = V4PingHelper(addresses.GetAddress(2));
NodeContainer pingers;
pingers.Add(c.Get(0));
pingers.Add(c.Get(1));
ApplicationContainer apps;
apps = ping.Install(pingers);
apps.Start(Seconds(1.0));
apps.Stop(Seconds(5.0));
// finally, print the ping rtts.
Config::Connect("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt",
MakeCallback(&PingRtt));
NS_LOG_INFO("Run Simulation.");
AsciiTraceHelper ascii;
csma.EnableAsciiAll(ascii.CreateFileStream("ping1.tr"));
Simulator::Run();
Simulator::Destroy();
NS_LOG_INFO("Done.");
}