Skip to main content

Simulation and Analysis of TCP Three-Way Handshake using ns-3 | NS3 Project 20

Simulation and Analysis of TCP Three-Way Handshake using ns-3

Question:-

Generate and analyze Wireshark traces for TCP three-way handshake and data transfer.

LLM Used: - Chatgpt

Prompt: -

1. Create a simple point-to-point network topology with 2 nodes (client and server).

2. Use TCP protocol with BulkSendApplication on the client and PacketSink on the server.

3. Configure link parameters such as DataRate and Delay.

4. Enable PCAP tracing so that the output can be opened in Wireshark to observe TCP three-way handshake (SYN, SYN-ACK, ACK) and data packets.

5. Enable FlowMonitor to measure performance metrics such as throughput, delay, and packet loss, and export results to a tcp_output.flowmonitor file.

6. Enable NetAnim and generate an XML file(tcp.xml) for animation visualization.

7. Ensure the program runs using the command: ./ns3 run scratch/24bps1078.cc

8. Include necessary headers, namespaces, and complete working code without errors.

9. Add comments explaining each major section of the code.

Additionally: -

   Use proper IP addressing and port configuration.

   Set simulation time appropriately.

   Ensure traffic is generated so that meaningful FlowMonitor data is produced.

Network Topology

   The network consists of two nodes connected using a point-to-point link. Node 0 (10.1.1.1) acts as the client, while Node 1 (10.1.1.2) acts as the server. A TCP connection is established between the two nodes, where the client initiates communication and sends data to the server.

 

                      10.1.1.0

        n0 ------------------------------------------   n1

    (client)             point-to-point         (Server)


Source Code: -

// Simulation of TCP Three-Way Handshake and Data Transfer using ns-3

// Includes: FlowMonitor, NetAnim, and Wireshark (PCAP)

 

#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/netanim-module.h"

#include "ns3/flow-monitor-module.h"

 using namespace ns3;

 int main ()

{

    // -----------------------------

    // 1. Create Nodes (Client & Server)

    // -----------------------------

    NodeContainer nodes;

    nodes.Create(2);

     // -----------------------------

    // 2. Configure Point-to-Point Link

    // -----------------------------

    PointToPointHelper p2p;

    p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));

    p2p.SetChannelAttribute("Delay", StringValue("2ms"));

 

    NetDeviceContainer devices = p2p.Install(nodes);

 

    // -----------------------------

    // 3. Install Internet Stack

    // -----------------------------

    InternetStackHelper stack;

    stack.Install(nodes);

 

    // -----------------------------

    // 4. Assign IP Addresses

    // -----------------------------

    Ipv4AddressHelper address;

    address.SetBase("10.1.1.0", "255.255.255.0");

 

    Ipv4InterfaceContainer interfaces = address.Assign(devices);

 

    // Enable Routing

    Ipv4GlobalRoutingHelper::PopulateRoutingTables();

 

    // -----------------------------

    // 5. Configure Applications

    // -----------------------------

    uint16_t port = 8080;

 

    // Server (PacketSink)

    PacketSinkHelper sink("ns3::TcpSocketFactory",

                          InetSocketAddress(Ipv4Address::GetAny(), port));

 

    ApplicationContainer serverApp = sink.Install(nodes.Get(1));

    serverApp.Start(Seconds(0.0));

    serverApp.Stop(Seconds(10.0));

 

    // Client (BulkSend)

    BulkSendHelper client("ns3::TcpSocketFactory",

                          InetSocketAddress(interfaces.GetAddress(1), port));

 

    // Limit data

    client.SetAttribute("MaxBytes", UintegerValue(1000000)); //1MB

 

    ApplicationContainer clientApp = client.Install(nodes.Get(0));

    clientApp.Start(Seconds(1.0));  // Start after server

    clientApp.Stop(Seconds(10.0));

 

    // -----------------------------

    // 6. Enable FlowMonitor

    // -----------------------------

    FlowMonitorHelper flowHelper;

    Ptr<FlowMonitor> monitor = flowHelper.InstallAll();

 

    // -----------------------------

    // 7. Enable NetAnim (Animation)

    // -----------------------------

    AnimationInterface anim("tcp.xml");

 

    // -----------------------------

    // 8. Enable PCAP (Wireshark)

    // -----------------------------

    p2p.EnablePcapAll("tcp_trace");

 

    // -----------------------------

    // 9. Run Simulation

    // -----------------------------

    Simulator::Stop(Seconds(10.0));

    Simulator::Run();

 

    // -----------------------------

    // 10. Save FlowMonitor Results

    // -----------------------------

    monitor->CheckForLostPackets();

    flowHelper.SerializeToXmlFile("tcp_output.flowmonitor", true, true);

 

    // -----------------------------

    // 11. Cleanup

    // -----------------------------

    Simulator::Destroy();

 

    return 0;

}

Animation Using NetAnim

   The animation shows packet transmission over the link, including the TCP three-way handshake followed by continuous data transfer. The arrows indicate the direction of packet flow between the client and server. This setup demonstrates reliable data




Communication using TCP in a simple network topology.

FlowMonitor Data Analysis and Graph Plotting Using Gnuplot

FlowID: 1 (TCP 10.1.1.1/49153 --> 10.1.1.2/8080)

        TX bitrate: 4959.98 kbit/s

        RX bitrate: 4960.01 kbit/s

        Mean Delay: 60.53 ms

        Packet Loss Ratio: 0.00 %

FlowID: 2 (TCP 10.1.1.2/8080 --> 10.1.1.1/49153)

        TX bitrate: 220.58 kbit/s

        RX bitrate: 220.57 kbit/s

        Mean Delay: 2.09 ms

        Packet Loss Ratio: 0.00 %

 

The graph represents the throughput of two TCP flows observed during the simulation. Flow 1 corresponds to the primary data transfer from the client (10.1.1.1) to the server (10.1.1.2), showing a high throughput of approximately 4960 Kbps. Flow 2 represents acknowledgment packets transmitted from the server to the client, which have significantly lower throughput (~220 Kbps).

This behavior reflects the nature of TCP communication, where data transmission dominates in one direction while acknowledgments are sent in the reverse direction. The absence of packet loss and stable throughput indicates efficient and reliable communication.



Wireshark Analysis

The TCP connection establishment process was analyzed using Wireshark by examining the captured PCAP file.

   SYN Packet:
 The client (10.1.1.1) initiates the connection by sending a SYN packet to the server (10.1.1.2), indicating the start of communication.

   SYN-ACK Packet:
 The server responds with a SYN-ACK packet, acknowledging the client’s request and sending its own synchronization request.

   ACK Packet:
 The client sends an ACK packet to confirm the connection establishment. After this step, the TCP connection is successfully established.

Following the handshake, data packets (PSH, ACK) are exchanged between the client and server, demonstrating reliable data transfer.

The first three lines show the TCP three-way handshake, and the remaining packets indicate data transfer after the connection is successfully established.



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...

NS2 (NS-2.35) Installation in Ubuntu 11.10

This post will help you in installing Network Simulator 2 version NS2.35 in Ubuntu 11.10 Instructions Install Ubuntu Download NS-2.35 ( http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download ) Unzip or untar it to any folder (recommended is /home/ loginname) using the following commands one by one sudo apt-get update sudo apt-get install build-essential autoconf automake libxmu-dev tar zxvf ns-allinone-2.35.tar.gz cd ns-allinone-2.35 ./install Once installed the PATH information will be provided to you. Copy the PATH and LD_LIBRARY_PATH Variable to .bashrc (see a dot in the beginning) Input the path information in .bashrc file like this export PATH=$PATH:<Place your paths here> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: <place the LD_LIBRARY_PATHS> here. Once done, save the file and close execute the command source .bashrc try ns or nam to see whether your installation succeeded.