0% found this document useful (0 votes)
34 views35 pages

ACN Practical File[1]

Uploaded by

Gold Crow Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views35 pages

ACN Practical File[1]

Uploaded by

Gold Crow Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

School of engineering and computing

“Practical Lab File”


Advanced Computer Networks Lab
Course Code: 21MCA381

Submitted to:- Submitted by:-


Mr. Mohit Kumar Sharma Arpan Sharma
Assistant professor ID: 23MCA0022
SOEC Program: MCA
Semester: 3rd

1
LIST OF EXPERIMENTS
Sr. Title of Experiment Pg. Date Signature
No. No.
1 Write a program for distance vector algorithm to find suitable path 3-5
for transmission.
2 Using TCP/IP sockets, write a client, server program to 6-10
make the client send the file name and to make the server
send back the contents of the requested file if present.
3 Write a program for Hamming code generation for error 11-14
detection and correction.

4 Write a program for congestion control using leaky bucket 15-17


algorithm.
PARTB (Simulate the following Computer Networks concepts
using any network simulators)
5 Simulate a three nodes point -to-point network with duplex 18-20
links between them. Set the queue size and vary the bandwidth
and find the number of packets dropped.
6 Simulate the network with five nodes n0, n1, n2, n3, n4, 21-23
forming a star topology. The node n4 is at the center. Node n0
is a TCP source, which transmits packets to node n3 (a TCP
sink) through the node n4. Node n1 is another traffic source,
and sends UDP packets to node n2 through n4. The duration
of the simulation time is 10 seconds.
7 Simulate to study transmission of packets over Ethernet LAN and 24-26
determine the number of packets drop destination.
8 Write a TCL Script to simulate working of multicasting routing 27-29
protocol and analyze the throughput of the network
9 Simulate the different types of internet traffic such as FTP and 30-32
TELNET over a wired network and analyze the packet drop and
packet delivery ratio in the network..
10. Simulate the different type of internet traffic such as FTP and 33-35
TELNET over a wired network and analyze the packet drop and
packet delivery ratio in the network.

2
EXPERIMENT NO.: 1
Title of Experiment: Write a program for distance vector algorithm to find suitable path for transmission

#include <stdio.h>

#define INF 999 // Representation of infinity

struct Router {

int cost[10]; // Cost to each node

int nextHop[10]; // Next hop to reach each node

};

void distanceVectorRouting(int graph[10][10], int n) {

struct Router routers[10];

int i, j, k;

// Initialize routers

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

routers[i].cost[j] = graph[i][j];

routers[i].nextHop[j] = (graph[i][j] != INF && i != j) ? j : -1;}}

int updated;

do {

updated = 0;

// Update cost and next hop using neighbors

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

for (k = 0; k < n; k++) {

if (routers[i].cost[j] > routers[i].cost[k] + routers[k].cost[j]) {

routers[i].cost[j] = routers[i].cost[k] + routers[k].cost[j];

3
routers[i].nextHop[j] = k;

updated = 1;

} }}}} while (updated);

// Print the final routing tables

printf("\nFinal Routing Tables:\n");

for (i = 0; i < n; i++) {

printf("Router %d:\n", i + 1);

printf("Destination\tCost\tNext Hop\n");

for (j = 0; j < n; j++) {

printf("%d\t\t%d\t%d\n", j + 1, routers[i].cost[j],

routers[i].nextHop[j] == -1 ? -1 : routers[i].nextHop[j] + 1) }

printf("\n"); }}

int main() {

int graph[10][10], n, i, j;

printf("Enter the number of nodes: ");

scanf("%d", &n);

printf("Enter the cost matrix (enter %d for infinity):\n", INF);

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

scanf("%d", &graph[i][j]);

if (i == j) graph[i][j] = 0; // Distance to itself is 0 }}

distanceVectorRouting(graph, n);

return 0;

Output :-

4
5
EXPERIMENT NO.: 2
Title of Experiment: Using TCP/IP sockets, write a client, server program to make the client send the
file name and to make the server send back the contents of the requested file if present.

// Server.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define PORT 8080

#define BUFFER_SIZE 1024

int main() {

int server_fd, new_socket;

struct sockaddr_in address;

int addr_len = sizeof(address);

char buffer[BUFFER_SIZE] = {0};

char *not_found_msg = "File not found.";

// Create socket

if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {

perror("Socket failed");

exit(EXIT_FAILURE);

// Bind socket to port

address.sin_family = AF_INET;

address.sin_addr.s_addr = INADDR_ANY;

6
address.sin_port = htons(PORT);

if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {

perror("Bind failed");

exit(EXIT_FAILURE);

// Listen for connections

if (listen(server_fd, 3) < 0) {

perror("Listen failed");

exit(EXIT_FAILURE);

printf("Server is listening on port %d...\n", PORT);

while (1) {

// Accept a client connection

if ((new_socket = accept(server_fd, (struct sockaddr *)&address,

(socklen_t*)&addr_len)) < 0) {

perror("Accept failed");

exit(EXIT_FAILURE);

printf("Connection established with client.\n");

// Receive the filename from the client

read(new_socket, buffer, BUFFER_SIZE);

printf("Client requested file: %s\n", buffer);

// Open the requested file

FILE *file = fopen(buffer, "r");

if (file == NULL) {

7
// Send "File not found" message to client

send(new_socket, not_found_msg, strlen(not_found_msg), 0);

printf("File %s not found.\n", buffer);

} else {

// Read file contents and send to client

char file_buffer[BUFFER_SIZE];

while (fgets(file_buffer, BUFFER_SIZE, file) != NULL) {

send(new_socket, file_buffer, strlen(file_buffer), 0);

fclose(file);

printf("File %s sent successfully.\n", buffer);

close(new_socket); // Close client socket

return 0;

// Client.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define PORT 8080

8
#define BUFFER_SIZE 1024

int main() {

int sock = 0;

struct sockaddr_in server_address;

char buffer[BUFFER_SIZE] = {0};

char filename[BUFFER_SIZE];

// Create socket

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {

perror("Socket creation failed");

exit(EXIT_FAILURE);

server_address.sin_family = AF_INET;

server_address.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary

if (inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr) <= 0) {

perror("Invalid address / Address not supported");

exit(EXIT_FAILURE);

// Connect to server

if (connect(sock, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {

perror("Connection failed");

exit(EXIT_FAILURE);

printf("Connected to server.\n");

// Input filename to request

printf("Enter the filename to request: ");

9
scanf("%s", filename);

send(sock, filename, strlen(filename), 0);

// Receive response from server

memset(buffer, 0, BUFFER_SIZE);

read(sock, buffer, BUFFER_SIZE);

printf("\nServer Response:\n%s\n", buffer);

close(sock);

return 0;

10
EXPERIMENT NO.: 3

Title of Experiment: Write a program for Hamming code generation for error detection and
correction.

#include <stdio.h>

#include <math.h>

// Function to calculate parity bits and generate the Hamming code

void generateHammingCode(int data[], int dataBits) {

int totalBits = dataBits + (int)log2(dataBits) + 1;

int hammingCode[totalBits];

// Initialize hamming code array with 0

for (int i = 0; i < totalBits; i++) {

hammingCode[i] = 0;}

// Place data bits in non-parity positions

int j = 0;

for (int i = 0; i < totalBits; i++) {

if ((i & (i + 1)) == 0) {

// Skip positions 0, 1, 3, 7, etc. for parity bits

continue;}

hammingCode[i] = data[j++];}

// Calculate parity bits

for (int i = 0; i < totalBits; i++) {

if ((i & (i + 1)) == 0) {

int parity = 0;

for (int k = i; k < totalBits; k++) {

if (((k + 1) & (i + 1)) != 0) {

parity ^= hammingCode[k];}}

11
hammingCode[i] = parity;}

// Print the Hamming code

printf("Generated Hamming Code: ");

for (int i = 0; i < totalBits; i++) {

printf("%d", hammingCode[i]);}

printf("\n");}

// Function to simulate an error in the code

void simulateError(int hammingCode[], int totalBits, int errorPos) {

printf("Simulating an error at position %d...\n", errorPos);

hammingCode[errorPos - 1] ^= 1; // Flip the bit

// Function to detect and correct errors in the Hamming code

void detectAndCorrectError(int hammingCode[], int totalBits) {

int errorPos = 0;

// Detect the error position by calculating parity

for (int i = 0; i < totalBits; i++) {

if ((i & (i + 1)) == 0) {

int parity = 0;

for (int k = i; k < totalBits; k++) {

if (((k + 1) & (i + 1)) != 0) {

parity ^= hammingCode[k];}}

if (parity != 0) {

errorPos += (i + 1);}}}

if (errorPos == 0) {

printf("No errors detected in the Hamming code.\n");} else {

printf("Error detected at position %d.\n", errorPos);

12
hammingCode[errorPos - 1] ^= 1; // Correct the error

printf("Corrected Hamming Code: ");

for (int i = 0; i < totalBits; i++) {

printf("%d", hammingCode[i]);}

printf("\n");}}

int main() {

int dataBits;

printf("Enter the number of data bits: ");

scanf("%d", &dataBits);

int data[dataBits];

printf("Enter the data bits (space-separated): ");

for (int i = 0; i < dataBits; i++) {

scanf("%d", &data[i]);}

// Generate Hamming code

int totalBits = dataBits + (int)log2(dataBits) + 1;

int hammingCode[totalBits];

generateHammingCode(data, dataBits);

// Simulate and correct an error

int errorPos;

printf("Enter the position to simulate an error (1-based index, or 0 to skip): ");

scanf("%d", &errorPos);

if (errorPos > 0 && errorPos <= totalBits) {

simulateError(hammingCode, totalBits, errorPos); }

detectAndCorrectError(hammingCode, totalBits);

return 0;}

13
14
EXPERIMENT NO.: 4
Title of Experiment: Write a program for congestion control using leaky bucket algorithm.
PARTB (Simulate the following Computer Networks concepts using any network simulators)

#include <stdio.h>

#include <unistd.h> // For sleep function

int main() {

int bucket_size, output_rate, num_packets, packet_size;

int current_bucket = 0; // Current amount of data in the bucket

printf("Enter the bucket size (in KB): ");

scanf("%d", &bucket_size);

printf("Enter the output rate (in KB/sec): ");

scanf("%d", &output_rate);

printf("Enter the number of packets: ");

scanf("%d", &num_packets);

for (int i = 1; i <= num_packets; i++) {

printf("\nEnter the size of packet %d (in KB): ", i);

scanf("%d", &packet_size);

// Check if the bucket can accommodate the incoming packet

if (packet_size > bucket_size) {

printf("Packet %d of size %dKB is too large and cannot be accommodated. Dropped!\n", i,


packet_size);

continue;}

// Add the packet to the bucket if there's enough space

if (current_bucket + packet_size <= bucket_size) {

current_bucket += packet_size;

15
printf("Packet %d of size %dKB added to the bucket. Current bucket size: %dKB\n", i,
packet_size, current_bucket);} else {

printf("Bucket overflow! Packet %d of size %dKB dropped.\n", i, packet_size);}

// Simulate the transmission of data from the bucket

printf("Transmitting data...\n");

if (current_bucket > 0) {

if (current_bucket <= output_rate) {

printf("Sent %dKB from the bucket. Bucket is now empty.\n", current_bucket);

current_bucket = 0;} else {

printf("Sent %dKB from the bucket. Remaining bucket size: %dKB\n", output_rate,
current_bucket - output_rate);

current_bucket -= output_rate;} }

// Simulate 1 second delay

sleep(1);}

// Drain remaining data in the bucket

while (current_bucket > 0) {

printf("\nTransmitting remaining data...\n");

if (current_bucket <= output_rate) {

printf("Sent %dKB from the bucket. Bucket is now empty.\n", current_bucket);

current_bucket = 0;} else {

printf("Sent %dKB from the bucket. Remaining bucket size: %dKB\n", output_rate,
current_bucket - output_rate);

current_bucket -= output_rate;}

sleep(1);}

printf("\nTransmission complete. Bucket is empty.\n");

return 0;}

16
17
EXPERIMENT NO.: 5
Title of Experiment: Simulate a three nodes point -to-point network with duplex links between them.
Set the queue size and vary the bandwidth and find the number of packets dropped.

# Create a new simulator instance

set ns [new Simulator]

# Open trace file for monitoring

set tracefile [open trace.tr w]

$ns trace-all $tracefile

# Create network nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

# Create duplex links between nodes with initial bandwidth and delay

$ns duplex-link $n0 $n1 2Mb 10ms DropTail

$ns duplex-link $n1 $n2 2Mb 10ms DropTail

# Set queue size for links

$ns queue-limit $n0 $n1 10

$ns queue-limit $n1 $n2 10

# Create a UDP agent and attach it to node n0

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

# Create a CBR traffic generator and attach it to the UDP agent

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp0

18
$cbr set packetSize_ 1000

$cbr set rate_ 1Mb

# Create a Null agent and attach it to node n2

set null [new Agent/Null]

$ns attach-agent $n2 $null

# Connect UDP to Null at the other end

$ns connect $udp0 $null

# Schedule the traffic

$ns at 0.5 "$cbr start"

$ns at 4.5 "$cbr stop"

# Monitor queue drop events

$ns at 0.0 "monitor-queue $n0 $n1"

$ns at 0.0 "monitor-queue $n1 $n2"

# Finish the simulation

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

# Queue monitoring function

proc monitor-queue { src dst } {

global ns

set q [$ns link $src $dst]

$q queue-monitor

19
# Schedule simulation end

$ns at 5.0 "finish"

# Run the simulation

$ns run

Steps to Analyze Results:

1. Run the Simulation:

ns script.tcl

2. Analyze Trace File: Use grep to filter packet drops from the trace file:

grep "d" trace.tr | wc -l

20
EXPERIMENT NO.: 6
Title of Experiment: Simulate the network with five nodes n0, n1, n2, n3, n4, forming a star topology.
The node n4 is at the center. Node n0 is a TCP source, which transmits packets to node n3 (a TCP sink)
through the node n4. Node n1 is another traffic source, and sends UDP packets to node n2 through n4.
The duration of the simulation time is 10 seconds.

# Create a new simulator instance

set ns [new Simulator]

# Open trace file for monitoring

set tracefile [open trace.tr w]

$ns trace-all $tracefile

# Create network nodes

set n0 [$ns node] ;# TCP source

set n1 [$ns node] ;# UDP source

set n2 [$ns node] ;# UDP sink

set n3 [$ns node] ;# TCP sink

set n4 [$ns node] ;# Central node

# Create duplex links between central node n4 and other nodes

$ns duplex-link $n0 $n4 1Mb 10ms DropTail

$ns duplex-link $n1 $n4 1Mb 10ms DropTail

$ns duplex-link $n2 $n4 1Mb 10ms DropTail

$ns duplex-link $n3 $n4 1Mb 10ms DropTail

# Create a TCP agent for the source and sink

set tcp [new Agent/TCP]

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

$ns connect $tcp $sink

21
# Create a UDP agent for the source and sink

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n2 $null

$ns connect $udp $null

# Configure TCP flow

$tcp set class_ 2

$ns at 0.5 "$tcp send 1000" ;# Send initial packet

$ns at 1.0 "$tcp send 1000"

$ns at 1.5 "$tcp send 1000"

$ns at 2.0 "$tcp send 1000"

# Configure UDP flow

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetSize_ 1000

$cbr set interval_ 0.5

$cbr set rate_ 1Mb

# Start the CBR traffic

$ns at 0.5 "$cbr start"

$ns at 9.5 "$cbr stop"

# Finish the simulation

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

22
# Schedule end of simulation

$ns at 10.0 "finish"

# Run the simulation

$ns run

To analyze the trace file:

# Count total packets sent by TCP and UDP


grep "send" trace.tr | wc -l

To see packet drops:

grep "dropped" trace.tr | wc –l

grep "send" trace.tr | wc –l

grep "drop" trace.tr | wc –l

23
EXPERIMENT NO.: 7
Title of Experiment Simulate to study transmission of packets over Ethernet LAN and determine the
number of packets drop destination.

# Create a new simulator instance

set ns [new Simulator]

# Open trace file for monitoring

set tracefile [open trace.tr w]

$ns trace-all $tracefile

# Create network nodes

set n0 [$ns node] ;# Source node

set n1 [$ns node] ;# Intermediate node

set n2 [$ns node] ;# Destination node

# Create a shared Ethernet link between the nodes

$ns duplex-link $n0 $n1 10Mb 10ms DropTail

$ns duplex-link $n1 $n2 10Mb 10ms DropTail

# Set queue limits to introduce potential drops

$ns queue-limit $n0 $n1 10

$ns queue-limit $n1 $n2 10

# Create a TCP agent for the source and sink

set tcp [new Agent/TCP]

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n2 $sink

$ns connect $tcp $sink

# Create traffic generator for the TCP source

24
set cbr [new Application/Traffic/CBR]

$cbr attach-agent $tcp

$cbr set packetSize_ 1000

$cbr set rate_ 1Mb

# Schedule the traffic

$ns at 0.5 "$cbr start"

$ns at 9.5 "$cbr stop"

# Finish the simulation

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

# Monitor queue drops

proc monitor_queue { src dst } {

global ns

set q [$ns link $src $dst]

$q queue-monitor

# Schedule monitoring for drops

$ns at 0.0 "monitor_queue $n0 $n1"

$ns at 0.0 "monitor_queue $n1 $n2"

# Schedule end of simulation

$ns at 10.0 "finish"

# Run the simulation

25
$ns run

Running the Simulation

1. Save the script to a file, e.g., ethernet_lan_simulation.tcl.


2. Open a terminal and run the simulation using:
3. ns ethernet_lan_simulation.tcl
4. Analyze the output:
o The trace file trace.tr will contain information about packet transmissions,
including any drops.

Count total packets sent:

grep "send" trace.tr | wc –l


Count total packets received:

grep "recv" trace.tr | wc -l

Count total packets dropped:

grep "drop" trace.tr | wc -l

26
EXPERIMENT NO.: 8
Title of Experiment: Write a TCL Script to simulate working of multicasting routing protocol and
analyze the throughput of the network.

# Create a simulator instance

set ns [new Simulator]

# Open trace file for monitoring

set tracefile [open multicast.tr w]

$ns trace-all $tracefile

# Open NAM trace file for visualization

set namfile [open multicast.nam w]

$ns namtrace-all $namfile

# Define multicast group address

set group [Node allocaddr]

# Create nodes

set n0 [$ns node] ;# Multicast source

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

# Create links between nodes

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n3 1Mb 10ms DropTail

$ns duplex-link $n3 $n4 1Mb 10ms DropTail

# Configure multicast routing protocol

27
$ns enable-mcast DVMRP ;# Dense Mode Routing Protocol

# Attach multicast agents to nodes

set mcast_agent [new Agent/UDP]

$mcast_agent set dst_addr_ $group

$mcast_agent set dst_port_ 0

$ns attach-agent $n0 $mcast_agent

# Create multicast receivers (members of the group)

for {set i 2} {$i <= 4} {incr i} {

set recv_agent($i) [new Agent/Null]

$ns attach-agent [set n$i] $recv_agent($i)

$ns at 1.0 "$ns join-group [set n$i] $group"

# Create CBR traffic and attach to the multicast source

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $mcast_agent

$cbr set packetSize_ 512

$cbr set interval_ 0.01

$cbr set rate_ 1Mb

# Start traffic

$ns at 2.0 "$cbr start"

$ns at 8.0 "$cbr stop"

# Finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

28
close $namfile

exec nam multicast.nam &

exit 0

# Schedule the end of the simulation

$ns at 10.0 "finish"

# Run the simulation

$ns run

Save the script as multicast_routing.tcl.


Run the simulation in NS2:
ns multicast_routing.tcl
To visualize the network in NAM:
nam multicast.nam

29
EXPERIMENT NO.: 9
Title of Experiment: Simulate the different types of internet traffic such as FTP and TELNET over a
wired network and analyze the packet drop and packet delivery ratio in the network.

# Create a simulator instance

set ns [new Simulator]

# Open trace file for monitoring

set tracefile [open traffic_sim.tr w]

$ns trace-all $tracefile

# Open NAM trace file for visualization

set namfile [open traffic_sim.nam w]

$ns namtrace-all $namfile

# Create network nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

# Create duplex links between nodes

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n3 1Mb 10ms DropTail

# Define queue limits to induce packet drops

$ns queue-limit $n1 $n2 10

$ns queue-limit $n1 $n3 10

# Create FTP traffic from n0 to n2

set tcp_ftp [new Agent/TCP]

30
$ns attach-agent $n0 $tcp_ftp

set sink_ftp [new Agent/TCPSink]

$ns attach-agent $n2 $sink_ftp

$ns connect $tcp_ftp $sink_ftp

set ftp [new Application/FTP]

$ftp attach-agent $tcp_ftp

$ftp set type_ FTP

# Schedule FTP traffic

$ns at 1.0 "$ftp start"

$ns at 5.0 "$ftp stop"

# Create TELNET traffic from n3 to n2

set tcp_telnet [new Agent/TCP]

$ns attach-agent $n3 $tcp_telnet

set sink_telnet [new Agent/TCPSink]

$ns attach-agent $n2 $sink_telnet

$ns connect $tcp_telnet $sink_telnet

set telnet [new Application/Telnet]

$telnet attach-agent $tcp_telnet

# Schedule TELNET traffic

$ns at 2.0 "$telnet start"

$ns at 6.0 "$telnet stop"

# Finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

31
close $namfile

exec nam traffic_sim.nam &

exit 0

# Schedule the end of the simulation

$ns at 10.0 "finish"

# Run the simulation

$ns run

32
EXPERIMENT NO.: 10
Title of Experiment: Simulate the different type of internet traffic such as FTP and TELNET over a
wired network and analyze the packet drop and packet delivery ratio in the network.

# Create a simulator instance

set ns [new Simulator]

# Open trace and NAM files

set tracefile [open traffic_analysis.tr w]

$ns trace-all $tracefile

set namfile [open traffic_analysis.nam w]

$ns namtrace-all $namfile

# Create network nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

# Create duplex links between nodes

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n3 1Mb 10ms DropTail

# Set queue limits to allow packet drops

$ns queue-limit $n1 $n2 10

$ns queue-limit $n1 $n3 10

# FTP traffic (TCP-based) from n0 to n2

set tcp_ftp [new Agent/TCP]

$ns attach-agent $n0 $tcp_ftp

33
set sink_ftp [new Agent/TCPSink]

$ns attach-agent $n2 $sink_ftp

$ns connect $tcp_ftp $sink_ftp

set ftp [new Application/FTP]

$ftp attach-agent $tcp_ftp

# Schedule FTP traffic

$ns at 1.0 "$ftp start"

$ns at 5.0 "$ftp stop"

# TELNET traffic (TCP-based) from n3 to n2

set tcp_telnet [new Agent/TCP]

$ns attach-agent $n3 $tcp_telnet

set sink_telnet [new Agent/TCPSink]

$ns attach-agent $n2 $sink_telnet

$ns connect $tcp_telnet $sink_telnet

set telnet [new Application/Telnet]

$telnet attach-agent $tcp_telnet

# Schedule TELNET traffic

$ns at 2.0 "$telnet start"

$ns at 6.0 "$telnet stop"

# Finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam traffic_analysis.nam &

34
exit 0

# Schedule simulation end

$ns at 10.0 "finish"

# Run simulation

$ns run

Run the TCL Script: Save the provided TCL script as ftp_telnet_simulation.tcl. Execute it using NS2:

ns ftp_telnet_simulation.tcl

1. Generate Trace File: The simulation will produce a trace file named traffic_analysis.tr.
2. Open NAM File:

nam traffic_analysis.nam

Analyze Output

Trace File Analysis:

o Count Sent Packets:

grep "s" traffic_analysis.tr | wc -l

o Count Received Packets:

grep "r" traffic_analysis.tr | wc -l

o Count Dropped Packets:

grep "d" traffic_analysis.tr | wc -l

2. Calculate Packet Delivery Ratio (PDR)

35

You might also like