DCCN Record
DCCN Record
Point-to-Point Network
Ex.1: Simulate a three nodes point-to-point network with duplex links between them. Set the
queue size vary the bandwidth and find the number of packets dropped.
Program:
TCL file:
set ns [ new Simulator ]
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab1.nam &
close $tf w
close $nf w
exit 0
}
1
Lab Manual DCCN – 4BCS502
2
Lab Manual DCCN – 4BCS502
3
Lab Manual DCCN – 4BCS502
Awk file:
Note: Create the file using gedit command and save it with the extension of. awk. We can
name it as lab1.awk.
BEGIN{
count=0;
}{
if($1=="d")
count++
}
END{
printf("The Total no of Packets Drop is :%d\n\n", count)
}
4
Lab Manual DCCN – 4BCS502
5
Lab Manual DCCN – 4BCS502
6
Lab Manual DCCN – 4BCS502
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
● Schedules events to start and stop the CBR traffic sources.
5. Finish Procedure and Simulation Execution:
$ns at 5.0 "finish"
$ns run
● Schedules the finish procedure to be called after 5 seconds of simulation
time.
● Executes the simulation using $ns run.
6. Finish Procedure:
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab1.nam &
close $tf w
close $nf w
exit 0
}
● Clears the trace output buffer.
● Launches NAM for visualization.
● Closes the trace and NAM files.
● Exits the simulation.
This script sets up a simple network scenario with two traffic sources, a router, and a traffic
sink. It uses UDP agents and CBR traffic sources to simulate data transmission through the
network, and the results are visualized using NAM. The finish procedure is responsible for
finalizing and terminating the simulation.
7
Lab Manual DCCN – 4BCS502
● The BEGIN block is executed before processing any lines from the input. In
this case, it initializes a variable count to zero. This variable will be used to
keep track of the number of dropped packets.
2. Main Block:
● The main block processes each line of the input file. The condition if ($1 ==
"d") checks if the first field (column) of the line is equal to "d". In NS-2 trace
files, the first character "d" often indicates a dropped packet.
● If the condition is true, it increments the count variable, indicating the
detection of a dropped packet.
3. END Block:
● The END block is executed after processing all lines from the input. It prints
the total count of dropped packets using the printf statement.
● The output message displays the total number of dropped packets detected
during the simulation.
8
Lab Manual DCCN – 4BCS502
Output
9
Lab Manual DCCN – 4BCS502
TCP and UDP Traffic Simulation
Ex.2: Simulate a four-node point-to-point network, and connect the links as follows:
no-n2, n1-n2 and n2-n3. Apply TCP agent between no-n3 and UDP n1-n3. Apply relevant
applications over TCP and UDP agents changing the parameter and determine the number of
packets by TCP/UDP.
Program:
TCL file:
proc finish {} {
global ns tf nf
$ns flush-trace
close $tf
10
Lab Manual DCCN – 4BCS502
close Snf
exit 0
11
Lab Manual DCCN – 4BCS502
12
Lab Manual DCCN – 4BCS502
Sns at 4.5 "Sns detach-agent Sno Stcp; $ns detach-agent Sn3 Ssink"
13
Lab Manual DCCN – 4BCS502
$ns run
Awk file:
Note: Create the file using gedit command and save it with the extension of. Awk in order to find
the number of packets drop in the trace file. We can name it as out1.awk.
BEGIN{
count=0;
}{
if($1=="d")
count++
END{
14
Lab Manual DCCN – 4BCS502
# Define different colors for data flows (for NAM) $ns color 1 Blue
$ns color 2 Red
# Open the trace file
set tf [open out1.tr w]
$ns trace-all $tf
# Open the NAM trace file
set nf [open out1.nam w]
$ns namtrace-all $nf
Here, colors for data flows in the NAM visualizer are defined, and two trace files (out1.tr and
out1.nam) are opened to store simulation data.
A procedure named finish is defined. This procedure is responsible for flushing traces, closing
trace files, executing NAM on the trace file, and then exiting the simulation.
Four nodes (n0, n1, n2, and n3) are created using the $ns node command.
Links with specified characteristics (bandwidth, delay, and queuing discipline) are created
between the nodes.
15
Lab Manual DCCN – 4BCS502
These lines set the queue size for a specific link, give node positions for NAM visualization, and
monitor the queue for a specific link in the visualization.
A TCP connection is set up between nodes n0 and n3. An FTP application is attached to this TCP
connection.
Events are scheduled to start and stop the CBR and FTP applications at specific simulation times.
This line detaches the TCP and sink agents at a specific simulation time.
The script prints the packet size and interval settings for the CBR application.
17
Lab Manual DCCN – 4BCS502
Output
18
Lab Manual DCCN – 4BCS502
Congestion Monitoring
19
Lab Manual DCCN – 4BCS502
20
Lab Manual DCCN – 4BCS502
21
Lab Manual DCCN – 4BCS502
22
Lab Manual DCCN – 4BCS502
23
Lab Manual DCCN – 4BCS502
Output
24
Lab Manual DCCN – 4BCS502
Error Rate in Ethernet LAN
Ex.4: Simulate an Ethernet LAN using n nodes (6-10), change errorrate and data rate and
compare the throughput.
# Take value of error rate and data rate from std inputputs "Enter error rate (<1) : "
gets stdin erate
# Create nodes set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns
node]set n5 [$ns node]set n6 [$ns node]
# Setup Links
$ns duplex-link $n3 $n6 10Mb 10ms DropTail
$ns duplex-link-op $n3 $n6 orient right-down
25
Lab Manual DCCN – 4BCS502
set count 0
set tr [open out.tr r]
while {[gets $tr line] != -1} {
# 8 denotes LAN at destination side and 5 denotes destinationnode
if {[string match "* 8 5 *" $line]} {set count [expr $count+1]
}
26
Lab Manual DCCN – 4BCS502
Output
27
Lab Manual DCCN – 4BCS502
28
Lab Manual DCCN – 4BCS502
Congestion Monitoring in LAN
Ex.5: Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and plot
congestion window for different source/destination.
#Make a NS simulator
set ns [new Simulator]
set tf [open lab3.tr w]
$ns trace-all $tf
29
Lab Manual DCCN – 4BCS502
# Add a TCP receiving module to node n5
set sink0 [new Agent/TCPSink]
$ns attach-agent $n5 $sink0
# Direct traffic from "tcp0" to "sink1"
$ns connect $tcp0 $sink0
AWK code:
BEGIN {
}
{
if($6=="cwnd_")
printf("%f\t%f\t\n",$1,$7);
}
END {
}
How to run:
This TCL script simulates an Ethernet LAN with multiple nodes and sets up TCP traffic between
them to monitor congestion windows. Let's break down the script line by line:
1. set ns [new Simulator]: This line initializes the NS (Network Simulator) object.
2. set tf [open lab3.tr w]: This line opens a file named lab3.tr in write mode for storing trace
information.
3. $ns trace-all $tf: This line instructs the simulator to trace all events and output them to the
lab3.tr file.
4. set nf [open lab3.nam w]: This line opens a file named lab3.nam in write mode for
generating network animation (nam) trace.
5. $ns namtrace-all $nf: This line instructs the simulator to trace all events for network
animation and output them to the lab3.nam file.
6. Node creation and configuration:
Nodes are created using $ns node command, and their properties like color and label
are set.
Each node represents a source or destination in the simulated LAN.
7. make-lan command:
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 50Mb 100ms LL Queue/DropTail Mac/802_3:
This command creates a LAN topology with specified nodes, bandwidth (50Mb),
delay (100ms), link-layer type (LL), queue type (Queue/DropTail), and MAC layer
type (Mac/802_3).
8. duplex-link and duplex-link-op commands:
These commands create duplex links between nodes n4 and n5 with specified
bandwidth, delay, and queue type. duplex-link-op sets the orientation of the link for
visualization.
9. Traffic generation and routing setup:
TCP agents and applications (FTP) are attached to nodes to generate traffic.
Traffic between nodes n0 and n5 is set up using TCP.
Similarly, traffic between nodes n2 and n3 is set up using TCP.
31
Lab Manual DCCN – 4BCS502
AWK script:
This script extracts congestion window (cwnd) information from the trace files generated during
the simulation (file1.tr and file2.tr), and formats it for plotting.
1. Save the AWK script with the same filename and. awk extension.
2. Run the AWK script using awk -f filename.awk file1.tr > a1 and awk -f filename.awk
file2.tr > a2 to process the trace files and redirect the output to files a1 and a2.
3. Finally, plot the congestion windows using xgraph with a1 and a2 files as inputs.
This setup allows you to simulate LAN congestion and monitor congestion window dynamics
between different source-destination pairs in the network.
32
Lab Manual DCCN – 4BCS502
Output
33
Lab Manual DCCN – 4BCS502
34
Lab Manual DCCN – 4BCS502
Routing Algorithms
Ex. 6: Write a program for distance vector algorithm to find suitable path for transmission.
#include<stdio.h>
#include<stdlib.h>
void rout_table();
int d[10][10],via[10][10];
int i,j,k,l,m,n,g[10][10],temp[10][10],ch,cost;
int main()
{
printf("enter the value of no. of nodes\n");
scanf("%d",&n);
rout_table();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
temp[i][j]=g[i][j];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
via[i][j]=i;
while(1)
{
for(i=0;i<n;i++) for(j=0;j<n;j++) if(d[i][j])
for(k=0;k<n;k++)
if(gli+g][k]<g[i][k])
{
g[i][k]=g[i][j]+gj][k];
via[i][k]-j;
}
for(i=0;i<n;i++)
{
printf("table for router %c\n",i+97);
for(j=0;j<n;j++)
printf("%c:: %d via %c\n",j+97,g[i][j],via[i][j]+97);
}
break;
}
}
void rout_table()
{
printf("\nEnter the routing table : \n");
printf("\t");
for(i=1;i<=n;i++)
printf("%c\t",i+96);
printf("\n");
for(i=0;i<=n;i++)
printf("--------");
35
Lab Manual DCCN – 4BCS502
printf("\n");
for(i=0;i<n;i++)
{
printf("%c |",i+97);
for(j=0;j<n;j++)
{
scanf("%d",&g[i]]);
if(g[i][j]!=999)
d[i][j]=1;
}
}
}
Output
36
Lab Manual DCCN – 4BCS502
Error Detection Techniques
#include<stdio.h>
void main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;
c1=dataatrec[6]^dataatrec[4]^dataatrec[2] dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+cl;
if(c=0) {
printf("\nNo error while transmission of data\n");
}
else {
printf("\nError on position %d",c);
37
Lab Manual DCCN – 4BCS502
printf("\nCorrect message is\n");
for (i=0;i<7;i++) {
printf("%d", dataatrec[i]);
}
}
}
Output
38
Lab Manual DCCN – 4BCS502
// Include headers
#include<stdio.h>
#include<string.h>
// CRC value
char check_value[28];
// generator polynomial
char gen_poly[10];
// variables
int data_length,i,j;
39
Lab Manual DCCN – 4BCS502
void crc(){
// initializing check_value
for(i=0;i<N;i++)
check_value[i]=data[i];
do{
} while(i<=data_length+N-1);
int main()
{
40
Lab Manual DCCN – 4BCS502
// Append data with check_value(CRC)
for(i=data_length;i<data_length+N-1;i++)
data[i]=check_value[i-data_length];
printf("\n---------------------------------------");
Output
41
Lab Manual DCCN – 4BCS502
Socket Programming
Ex.8: Using TCP/IP sockets, write a client server program to make client sending the file name
and the server to send back the contents of the requested file if present.
Client Program:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int sock, n;
char buffer[1024], fname[50];
printf("----------------\n");
printf("------------\n");
return 0;
42
Lab Manual DCCN – 4BCS502
Server Program:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int sersock, sock, fd, n, reuse = 1;
char buffer[1024], fname[50];
else
{
while ((n = read(fd, buffer, sizeof(buffer))) > 0)
{
send(sock, buffer, n, 0);
}
}
}
43
Lab Manual DCCN – 4BCS502
Output
44
Lab Manual DCCN – 4BCS502
Congestion Control
Ex.9: Write a program for congestion control using leaky bucket algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NOF_PACKETS 10
int my_rand(int a) {
int n = (random() % 10) % a;
return rn == 0 ? 1 : rn;
}
int main() {
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
else {
p_sz_rm += packet_sz[i];
printf("\n\nIncoming Packet size: %d", packet_sz[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_m);
p_time = my_rand(4)*10;
printf("\nTime left for transmission: %d units", p_time);
45
Lab Manual DCCN – 4BCS502
for (clk = 10; clk <= p_time; clk += 10) {
sleep(1);
if (p_sz_rm) {
if (p_sz_rm <= o_rate)
else
op = o_rate, p_sz_rm -= o_rate;
else {
Explanation:
What is Leaky-Bucket algorithm?
The leaky bucket algorithm is a method of managing traffic in a network by smoothing the rate at
which data is transmitted. It is a congestion control mechanism used to regulate the flow of data
and to prevent bursts of traffic from overwhelming a network.
The concept of the leaky bucket can be understood through an analogy with a physical bucket
that has a leak at the bottom. Here's how it works:
1. Bucket: Imagine a bucket that can hold a certain amount of water. This bucket represents
a buffer or queue in the network.
2. Water: Incoming data packets are represented as water pouring into the bucket.
3. Leak: The bucket has a small hole at the bottom, causing water to leak out at a constant
rate. This leak represents the maximum rate at which data can be transmitted from the
buffer onto the network.
4. Overflow: If water pours into the bucket too quickly, it will eventually overflow.
Similarly, if data packets arrive at a rate faster than the leak rate, they will overflow the
buffer and may be dropped or delayed.
5. Smoothed Traffic: By limiting the rate at which data can be transmitted (the leak rate),
the leaky bucket algorithm ensures that the traffic leaving the bucket is smooth and
controlled, even if the incoming traffic is bursty.
46
Lab Manual DCCN – 4BCS502
In summary, the leaky bucket algorithm provides a way to regulate the flow of data in a network
by controlling the rate at which data is transmitted. It helps in managing congestion and ensuring
that the network operates efficiently without being overwhelmed by sudden bursts of traffic.
1. Include Libraries: The program includes necessary header files such as <stdio.h>,
<stdlib.h>, and <unistd.h> for input/output, standard library functions, and system calls
respectively.
3. Custom rand Function: There's a custom rand function defined which takes an integer
argument a and generates a random number between 0 and a-1.
4. Main Function:
Packet Size Initialization: It initializes an array packet_sz with random sizes for
each packet. Packet sizes are multiples of 10 and generated using the custom rand
function.
Input Parameters: It prompts the user to input the output rate (o_rate) and bucket
size (b_size) for the token bucket algorithm.
Check Bucket Capacity: It checks if adding the size of the current packet to the
remaining size in the bucket exceeds the bucket size. If so, it rejects the packet.
Update Bucket Size: It updates the remaining size in the bucket after adding
the current packet size.
This program helps to understand how token bucket algorithm works for controlling the rate of
data transmission in a network by regulating the flow of packets. It demonstrates the concept of
token bucket algorithm in a simplified manner.
47
Lab Manual DCCN – 4BCS502
Output
48
Lab Manual DCCN – 4BCS502
49