Simulation of Mixed TCP and UDP Traffic and their Performance Impact Using ns-3
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.
#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;
}
· LLM Used: ChatGPT (GPT-5.3)
· Prompt:
“Generate ns-3 code to simulate mixed TCP and UDP traffic and analyze their
performance using FlowMonitor.”
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.
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.
Throughput is calculated using the formula:
Throughput (Mbps) = (rxBytes × 8) / (time × 1,000,000)
For example, for TCP:
= (8693408 × 8) / (10 × 1,000,000) ≈ 6.95 Mbps
For UDP:
Throughput = (8649896 × 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
Post a Comment