To implement Network communication in ns-3 we need to set up basic network topology, configuring nodes with appropriate network devices, installing necessary protocols and application, and running the simulation to analyse communication between nodes.
Step-by-Step Guide to Implement Network Communication in ns-3
- Install ns-3:
- Ensure ns-3 is installed. Follow the installation instructions for ns-3 if you haven’t done so already.
- Create a Simulation Script:
- Create a new script file, e.g., network-communication.cc.
- Include Necessary Headers:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
Define the Main Function:
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
Set Up the Network Topology:
- Create nodes and configure network settings
NodeContainer nodes;
nodes.Create (2); // Create 2 nodes
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
Configure Mobility:
- Set up mobility models for the nodes (optional)
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
Install Applications:
- Set up a UDP echo server on one node and a UDP echo client on the other node
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Run the Simulation:
- Define the simulation stop time and start the simulator:
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
Example Complete Script (network-communication.cc):
Here some example scripts for implementing network communication.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (2); // Create 2 nodes
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Explanation:
Here we have enlightened the process of network communication
- Network Configuration:
- Nodes are created, and a point-to-point link is set up between them.
- The Internet stack is installed on the nodes, and IP addresses are assigned to the network devices.
- Mobility (Optional):
- The ConstantPositionMobilityModel is used to keep the nodes at fixed positions. This step is optional and can be modified to simulate different mobility scenarios.
- Applications:
- A UDP echo server application is installed on one node.
- A UDP echo client application is installed on the other node to simulate communication.
- Running the Simulation:
- The simulation is run for a specified duration, and then the simulator is destroyed to clean up.
Finally, implementing the network communication is discussed and simulated. So for any Network communication in ns-3 implementation contact us.