To Implement and simulate TCP protocols in ns3, we set-up a network topology, configure TCP connections, and run applications which uses TCP. Below given steps will guide on simulating the TCP in ns3.Further more if you ant any implementation support then reach out for us.
Step-by-Step Implementation of TCP in ns3:
- Set Up Your Environment
Ensure that ns3 is installed.
- Create a New ns3 Script
Create a new simulation script in the scratch directory of your ns3 installation. For example, create a file named tcp-simulation.cc.
cd ns3.xx
cd scratch
touch tcp-simulation.cc
3. Include Necessary Headers
In thetcp-simulation.cc file, include the necessary headers for the simulation.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
4. Set Up the Network Topology
Define the network topology. Let’s create a basic topology with two nodes connected by a point-to-point link.
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (2);
// Create a point-to-point link between nodes
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up a TCP server on node 1
uint16_t port = 9; // Discard port (RFC 863)
Address serverAddress (InetSocketAddress (interfaces.GetAddress (1), port));
PacketSinkHelper packetSinkHelper (“ns3::TcpSocketFactory”, serverAddress);
ApplicationContainer serverApps = packetSinkHelper.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Set up a TCP client on node 0
OnOffHelper onOffHelper (“ns3::TcpSocketFactory”, serverAddress);
onOffHelper.SetAttribute(“OnTime”,StringValue (“ns3::ConstantRandomVariable[Constant=1]”));
onOffHelper.SetAttribute(“OffTime”,StringValue (“ns3::ConstantRandomVariable[Constant=0]”));
onOffHelper.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));
onOffHelper.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = onOffHelper.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“tcp-simulation.tr”));
pointToPoint.EnablePcapAll (“tcp-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
5. Build and Run the Simulation
After writing your script, you need to build and run it.
./waf build
./waf –run scratch/tcp-simulation
6. Analyze the Results
After running the simulation, you can analyze the results using the generated trace files (tcp-simulation.tr and tcp-simulation-0-0.pcap).
Adding Different TCP Variants
ns-3 supports various TCP variants such as TCP NewReno, TCP Westwood, and TCP BBR. You can specify the TCP variant you want to use by setting the appropriate TCP type. Here’s an example of how to set the TCP variant to TCP Westwood:
// Set the TCP variant
TypeId tcpTypeId = TypeId::LookupByName (“ns3::TcpWestwood”);
Config::SetDefault (“ns3::TcpL4Protocol::SocketType”, TypeIdValue (tcpTypeId));
Add the above code before creating the nodes to set the default TCP variant for the simulation.
Complete Example Script with TCP Variant
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
// Set the TCP variant to TCP Westwood
TypeId tcpTypeId = TypeId::LookupByName (“ns3::TcpWestwood”);
Config::SetDefault (“ns3::TcpL4Protocol::SocketType”, TypeIdValue (tcpTypeId));
// Create nodes
NodeContainer nodes;
nodes.Create (2);
// Create a point-to-point link between nodes
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up a TCP server on node 1
uint16_t port = 9; // Discard port (RFC 863)
Address serverAddress (InetSocketAddress (interfaces.GetAddress (1), port));
PacketSinkHelper packetSinkHelper (“ns3::TcpSocketFactory”, serverAddress);
ApplicationContainer serverApps = packetSinkHelper.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Set up a TCP client on node 0
OnOffHelper onOffHelper (“ns3::TcpSocketFactory”, serverAddress);
onOffHelper.SetAttribute(“OnTime”,StringValue (“ns3::ConstantRandomVariable[Constant=1]”));
onOffHelper.SetAttribute(“OffTime”,StringValue (“ns3::ConstantRandomVariable[Constant=0]”));
onOffHelper.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));
onOffHelper.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = onOffHelper.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“tcp-simulation.tr”));
pointToPoint.EnablePcapAll (“tcp-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Atlast, by configuring TCP connections and running applications which uses TCP we have successfully Implemented and simulated the TCP protocols in ns3 atmosphere.