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:
- Create a simple point-to-point network topology with 2 nodes (client and server).
- Use TCP protocol with
BulkSendApplicationon the client andPacketSinkon the server. - Configure link parameters such as
DataRateandDelay. - 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.
- Enable FlowMonitor to measure performance metrics such as throughput, delay, and packet loss, and export results to a tcp_output.flowmonitor file.
- Enable NetAnim and generate an XML file (tcp.xml) for animation visualization.
- Ensure the program runs using the command:
./ns3 run scratch/24bps1078.cc - Include necessary headers, namespaces, and complete working code without errors.
- 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
Post a Comment