Skip to main content

Simulation of Mixed TCP and UDP Traffic and Their Performance Impact Using ns3 | NS3 Project 16

Simulation of Mixed TCP and UDP Traffic and their Performance Impact Using ns-3

The objective of this experiment is to simulate a network with both TCP and UDP traffic using ns-3 and analyze how they affect each other’s performance in terms of throughput. In computer networks, TCP (Transmission Control Protocol) is a reliable and congestion-controlled protocol, while UDP (User Datagram Protocol) is faster but does not implement congestion control. When both protocols share the same network, their behavior differs significantly. This experiment studies their interaction and performance impact.

A network topology with multiple nodes connected using point-to-point links is created in ns-3. TCP traffic is generated using BulkSendApplication and UDP traffic is generated using OnOffApplication. FlowMonitor is used to collect performance metrics such as throughput. NetAnim is used to visualize packet transmission.

Source Code

#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"
#include "ns3/flow-monitor-module.h"
#include "ns3/netanim-module.h"

using namespace ns3;

int main () {
    NodeContainer nodes;
    nodes.Create(4);

    // Point-to-Point links
    PointToPointHelper p2p;
    p2p.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
    p2p.SetChannelAttribute("Delay", StringValue("2ms"));

    NetDeviceContainer d1 = p2p.Install(nodes.Get(0), nodes.Get(1));
    NetDeviceContainer d2 = p2p.Install(nodes.Get(1), nodes.Get(2));
    NetDeviceContainer d3 = p2p.Install(nodes.Get(2), nodes.Get(3));

    // Internet stack
    InternetStackHelper stack;
    stack.Install(nodes);

    // IP addressing
    Ipv4AddressHelper address;

    address.SetBase("10.1.1.0", "255.255.255.0");
    Ipv4InterfaceContainer i1 = address.Assign(d1);

    address.SetBase("10.1.2.0", "255.255.255.0");
    Ipv4InterfaceContainer i2 = address.Assign(d2);

    address.SetBase("10.1.3.0", "255.255.255.0");
    Ipv4InterfaceContainer i3 = address.Assign(d3);

    Ipv4GlobalRoutingHelper::PopulateRoutingTables();

    // ---------------- TCP TRAFFIC ----------------
    uint16_t tcpPort = 8080;

    BulkSendHelper tcpSource("ns3::TcpSocketFactory",
                             InetSocketAddress(i3.GetAddress(1), tcpPort));
    tcpSource.SetAttribute("MaxBytes", UintegerValue(0));

    ApplicationContainer tcpApp = tcpSource.Install(nodes.Get(0));
    tcpApp.Start(Seconds(1.0));
    tcpApp.Stop(Seconds(10.0));

    PacketSinkHelper tcpSink("ns3::TcpSocketFactory",
                             InetSocketAddress(Ipv4Address::GetAny(), tcpPort));
    tcpSink.Install(nodes.Get(3));

    // ---------------- UDP TRAFFIC ----------------
    uint16_t udpPort = 9090;

    OnOffHelper udpSource("ns3::UdpSocketFactory",
                          InetSocketAddress(i3.GetAddress(1), udpPort));
    udpSource.SetAttribute("DataRate", StringValue("5Mbps"));
    udpSource.SetAttribute("PacketSize", UintegerValue(1024));

    ApplicationContainer udpApp = udpSource.Install(nodes.Get(1));
    udpApp.Start(Seconds(2.0));
    udpApp.Stop(Seconds(10.0));

    PacketSinkHelper udpSink("ns3::UdpSocketFactory",
                             InetSocketAddress(Ipv4Address::GetAny(), udpPort));
    udpSink.Install(nodes.Get(3));

    // ---------------- FLOW MONITOR ----------------
    FlowMonitorHelper flowmon;
    Ptr<FlowMonitor> monitor = flowmon.InstallAll();

    // ---------------- NETANIM ----------------
    AnimationInterface anim("anim.xml");

    Simulator::Stop(Seconds(10.0));
    Simulator::Run();

    // Save results
    monitor->SerializeToXmlFile("results.xml", true, true);

    Simulator::Destroy();
    return 0;
}

Simulation Setup Details

  • LLM Used: ChatGPT (GPT-5.3)
  • Prompt: “Generate ns-3 code to simulate mixed TCP and UDP traffic and analyze their performance using FlowMonitor.”

Network Animation

NetAnim Simulation Animation

The animation shows packet transmission between nodes. Both TCP and UDP flows are visible. UDP transmits continuously, while TCP adapts its rate. This demonstrates how both protocols share network resources.

Throughput Analysis

Throughput Comparison Graph

Explanation:

The graph was generated using Gnuplot based on throughput values obtained from FlowMonitor. Both TCP and UDP achieve nearly equal throughput (~6.9 Mbps), indicating that the network is not congested. TCP maintains its transmission rate even in the presence of UDP traffic.

FlowMonitor Output & Calculations

FlowMonitor Console Output 1 FlowMonitor Console Output 2

Throughput is calculated using the formula:

Throughput (Mbps) = (rxBytes × 8) / (time × 1,000,000)

For TCP:
= (8,693,408 × 8) / (10 × 1,000,000) ≈ 6.95 Mbps

For UDP:
Throughput = (8,649,896 × 8) / (10 × 1,000,000) ≈ 6.91 Mbps

Conclusion

The experiment demonstrates that both TCP and UDP can achieve similar throughput under low congestion conditions. However, UDP does not adjust its rate, whereas TCP adapts based on network conditions. This highlights the importance of congestion control in network performance.

Increasing UDP traffic can reduce TCP throughput due to congestion, showing unfair bandwidth sharing.

Comments

Popular posts from this blog

How to Create Ubuntu 24.04 Bootable USB Using Rufus [Step-by-Step Guide]

How to Create Ubuntu 24.04 Bootable USB Using Rufus [Step-by-Step Guide] Are you planning to install or try Ubuntu 24.04 LTS ? The easiest and most reliable method is to create a bootable USB drive using Rufus on a Windows system. This detailed guide will help you create a Ubuntu 24.04 USB bootloader using Rufus with easy-to-follow steps and screenshots (optional). Here is the complete video of the bootloader creation and OS installation in Windows 11. 🧰 Requirements A USB flash drive (minimum 8GB recommended) A Windows PC Ubuntu 24.04 LTS ISO file Rufus USB creation tool 🧾 Steps to Create a Ubuntu 24.04 Bootable USB Using Rufus ✅ Step 1: Download Ubuntu 24.04 ISO Visit the official Ubuntu website and download the Ubuntu 24.04 LTS ISO file . ✅ Step 2: Download and Run Rufus Head to Rufus official site and download the latest version. Open the executable file (no installation required). ✅ Step 3: Insert USB Drive Plug in your USB drive. Rufus ...

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libc...

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it...