You are on page 1of 68

COMPUTER NETWORKS LABORATORY (15ECL68)

Introduction to NS-2
NS (Version 2) is an open source network simulation tool. It is an object oriented, discrete
event driven simulator written in C++ and Otcl. The primary use of NS is in network researches
to simulate various types of wired/wireless local and wide area networks; to implement network
protocols such as TCP and UPD, traffic source behavior such as FTP, Telnet, Web, CBR and
VBR, router queue management mechanism such as Drop Tail, RED and CBQ, routing
algorithms such as Dijkstra, and many more.
Ns2 is written in C++ and Otcl to separate the control and data path implementations. The
simulator supports a class hierarchy in C++ (the compiled hierarchy) and a corresponding
hierarchy within the Otcl interpreter (interpreted hierarchy).
The reason why ns2 uses two languages is that different tasks have different requirements:
For example simulation of protocols requires efficient manipulation of bytes and packet headers
making the run-time speed very important. On the other hand, in network studies where the aim
is to vary some parameters and to quickly examine a number of scenarios the time to change the
model and run it again is more important.
In ns2, C++ is used for detailed protocol implementation and in general for such cases where
every packet of a flow has to be processed. For instance, if you want to implement a new
queuing discipline, then C++ is the language of choice. Otcl, on the other hand, is suitable for
configuration and setup. Otcl runs quite slowly, but it can be changed very quickly making the
construction of simulations easier. In ns2, the compiled C++ objects can be made available to the
Otcl interpreter. In this way, the ready-made C++ objects can be controlled from the OTcl level.

Otcl basics
This chapter introduces the syntax and some basic commands of the Otcl language that are
used by ns2. It is important to understand how Otcl works before moving to the part that deals
with the creation of the actual simulation scenario.

Assigning values to variables


In tcl, values can be stored to variables and these values can be further used in commands:

Dept. ECE, ATMECE Page 1


COMPUTER NETWORKS LABORATORY (15ECL68)

set a 5

set b [expr $a/5]

In the first line, the variable a is assigned the value “5”. In the second line, the result of the
command [expr $a/5], which equals 1, is then used as an argument to another command, which
in turn assigns a value to the variable b. The “$” sign is used to obtain a value contained in a
variable and square brackets are an indication of a command substitution.

Procedures

One can define new procedures with the proc command. The first argument to proc is the
name of the procedure and the second argument contains the list of the argument names to that
procedure. For instance a procedure that calculates the sum of two numbers can be defined as
follows:

proc sum {a b} {

expr $a + $b

The next procedure calculates the factorial of a number:

proc factorial a {

if {$a <= 1} {

return 1

#here the procedure is called again

expr $x * [factorial [expr $x-1]]

It is also possible to give an empty string as an argument list. However, in this case the
variables that are used by the procedure have to be defined as global. For instance:

proc sum {} {

Dept. ECE, ATMECE Page 2


COMPUTER NETWORKS LABORATORY (15ECL68)

global a b

expr $a + $b

Files and lists


In tcl, a file can be opened for reading with the command:

set testfile [open test.dat r]

The first line of the file can be stored to a list with a command:

gets $testfile list

Now it is possible to obtain the elements of the list with commands (numbering of elements
starts from 0) :

set first [lindex $list 0]

set second [lindex $list 1]

Similarly, a file can be written with a puts command:

set testfile [open test.dat w]

puts $testfile “testi”

Calling subprocesses
The command exec creates a subprocess and waits for it to complete. The use of exec is
similar to giving a command line to a shell program. For instance, to remove a file:

exec rm $testfile

The exec command is particularly useful when one wants to call a tcl-script from within
another tclscript. For instance, in order to run the tcl-script example.tcl multiple times with the
value of the parameter “test” ranging from 1 to 10, one can type the following lines to another
tcl-script:

Dept. ECE, ATMECE Page 3


COMPUTER NETWORKS LABORATORY (15ECL68)

for {set ind 1} {$ind <= 10} {incr ind} {

set test $ind

exec ns example.tcl test

Creating the topology


To be able to run a simulation scenario, a network topology must first be created. In ns2, the
topology consists of a collection of nodes and links.
Before the topology can be set up, a new simulator object must be created at the beginning of
the script with the command:

set ns [new Simulator]

The simulator object has member functions that enable creating the nodes and the links,
connecting agents etc. All these basic functions can be found from the class Simulator. When
using functions belonging to this class, the command begins with “$ns”, since ns was defined to
be a handle to the Simulator object.

Nodes
New node objects can be created with the command:

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

The member function of the Simulator class, called “node” creates four nodes and assigns
them to the handles n0, n1, n2 and n3. These handles can later be used when referring to the
nodes. If the node is not a router but an end system, traffic agents (TCP, UDP etc.) and traffic
sources (FTP,CBR etc.) must be set up, i.e, sources need to be attached to the agents and the
agents to the nodes, respectively.

Dept. ECE, ATMECE Page 4


COMPUTER NETWORKS LABORATORY (15ECL68)

Agents, applications and traffic sources

The most common agents used in ns2 are UDP and TCP agents. In case of a TCP agent,
several types are available. The most common agent types are:

 Agent/TCP – a Tahoe TCP sender


 Agent/TCP/Reno – a Reno TCP sender
 Agent/TCP/Sack1 – TCP with selective acknowledgement

The most common applications and traffic sources provided by ns2 are:

 Application/FTP – produces bulk data that TCP will send


 Application/Traffic/CBR – generates packets with a constant bit rate
 Application/Traffic/Exponential – during off-periods, no traffic is sent. During on-periods,
packets are generated with a constant rate. The length of both on and off-periods is
exponentially distributed.
 Application/Traffic/Trace – Traffic is generated from a trace file, where the sizes and interarrival
times of the packets are defined.

In addition to these ready-made applications, it is possible to generate traffic by using the


methods provided by the class Agent. For example, if one wants to send data over UDP, the
method

send(int nbytes)

can be used at the tcl-level provided that the udp-agent is first configured and attached to
some node.
Below is a complete example of how to create a CBR traffic source using UDP as transport
protocol and attach it to node n0:

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

Dept. ECE, ATMECE Page 5


COMPUTER NETWORKS LABORATORY (15ECL68)

$cbr0 set packet_size_ 1000

$udp0 set packet_size_ 1000

$cbr0 set rate_ 1000000

An FTP application using TCP as a transport protocol can be created and attached to node n1
in much the same way:

set tcp1 [new Agent/TCP]

$ns attach-agent $n1 $tcp1

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

$tcp1 set packet_size_ 1000

The UDP and TCP classes are both child-classes of the class Agent. With the expressions
[new Agent/TCP] and [new Agent/UDP] the properties of these classes can be combined to the
new objects udp0 and tcp1. These objects are then attached to nodes n0 and n1. Next, the
application is defined and attached to the transport protocol. Finally, the configuration
parameters of the traffic source are set. In case of CBR, the traffic can be defined by parameters
rate_ (or equivalently interval_, determining the interarrival time of the packets), packetSize_
and random_ . With the random_ parameter it is possible to add some randomness in the
interarrival times of the packets. The default value is 0, meaning that no randomness is added.

Traffic Sinks
If the information flows are to be terminated without processing, the udp and tcp sources
have to be connected with traffic sinks. A TCP sink is defined in the class Agent/TCPSink and
an UDP sink is defined in the class Agent/Null.
A UDP sink can be attached to n2 and connected with udp0 in the following way:

set null [new Agent/Null]

$ns attach-agent $n2 $null

$ns connect $udp0 $null

Dept. ECE, ATMECE Page 6


COMPUTER NETWORKS LABORATORY (15ECL68)

A standard TCP sink that creates one acknowledgement per a received packet can be attached
to n3 and connected with tcp1 with the commands:

set sink [new Agent/Sink]

$ns attach-agent $n3 $sink

$ns connect $tcp1 $sink

There is also a shorter way to define connections between a source and the destination with
the command:

$ns create-connection <srctype> <src> <dsttype> <dst> <pktclass>

For example, to create a standard TCP connection between n1 and n3 with a class ID of 1:

$ns create-connection TCP $n1 TCPSink $n3 1

One can very easily create several tcp-connections by using this command inside a for-loop.

Links

Links are required to complete the topology. In ns2, the output queue of a node is
implemented as part of the link, so when creating links the user also has to define the queue-type.

Figure 2 Link in ns2

Figure 2 shows the construction of a simplex link in ns2. If a duplex-link is created, two
simplex links will be created, one for each direction. In the link, packet is first enqueued at the

Dept. ECE, ATMECE Page 7


COMPUTER NETWORKS LABORATORY (15ECL68)

queue. After this, it is either dropped, passed to the Null Agent and freed there, or dequeued and
passed to the Delay object which simulates the link delay. Finally, the TTL (time to live) value is
calculated and updated.
Links can be created with the following command:

$ns duplex/simplex-link endpoint1 endpoint2 bandwidth delay queue-type

For example, to create a duplex-link with DropTail queue management between n0 and n2:

$ns duplex-link $n0 $n2 15Mb 10ms DropTail

Creating a simplex-link with RED queue management between n1 and n3:

$ns simplex-link $n1 $n3 10Mb 5ms RED

The values for bandwidth can be given as a pure number or by using qualifiers k (kilo), M
(mega), b (bit) and B (byte). The delay can also be expressed in the same manner, by using m
(milli) and u (mikro) as qualifiers. There are several queue management algorithms implemented
in ns2, but in this exercise only DropTail and RED will be needed.

Tracing and monitoring


In order to be able to calculate the results from the simulations, the data has to be collected
somehow. NS2 supports two primary monitoring capabilities: traces and monitors. The traces
enable recording of packets whenever an event such as packet drop or arrival occurs in a queue
or a link. The monitors provide a means for collecting quantities, such as number of packet drops
or number of arrived packets in the queue. The monitor can be used to collect these quantities for
all packets or just for a specified flow (a flow monitor).

Traces

All events from the simulation can be recorded to a file with the following commands:

set trace_all [open all.dat w]

$ns trace-all $trace_all

$ns flush-trace

Dept. ECE, ATMECE Page 8


COMPUTER NETWORKS LABORATORY (15ECL68)

close $trace_all

First, the output file is opened and a handle is attached to it. Then the events are recorded to
the file specified by the handle. Finally, at the end of the simulation the trace buffer has to be
flushed and the file has to be closed. This is usually done with a separate finish procedure. If
links are created after these commands, additional objects for tracing (EnqT, DeqT, DrpT and
RecvT) will be inserted into them.

Figure 3 Link in ns2 when tracing is enabled

These new objects will then write to a trace file whenever they receive a packet. The format
of the trace file is following:

+ 1.84375 0 2 cbr 210 ------- 0 0.0 3.1 225 610

- 1.84375 0 2 cbr 210 ------- 0 0.0 3.1 225 610

r 1.84471 2 1 cbr 210 ------- 1 3.0 1.0 195 600

r 1.84566 2 0 ack 40 ------- 2 3.2 0.1 82 602

+ 1.84566 0 2 tcp 1000 ------- 2 0.1 3.2 102 611

- 1.84566 0 2 tcp 1000 ------- 2 0.1 3.2 102 611

+ : enqueue

- : dequeue

d : drop

r : receive

The fields in the trace file are: type of the event, simulation time when the event occurred,
source and destination nodes, packet type (protocol, action or traffic source), packet size, flags,

Dept. ECE, ATMECE Page 9


COMPUTER NETWORKS LABORATORY (15ECL68)

flow id, source and destination addresses, sequence number and packet id. In addition to tracing
all events of the simulation, it is also possible to create a trace object between a particular source
and a destination with the command:

$ns create-trace <type> <file> <src> <dest>

where the type can be, for instance,

 Enque – a packet arrival (for instance at a queue)


 Deque – a packet departure (for instance at a queue)
 Drop – packet drop
 Recv – packet receive at the destination

Tracing all events from a simulation to a specific file and then calculating the desired
quantities from this file for instance by using perl or awk and Matlab is an easy way and suitable
when the topology is relatively simple and the number of sources is limited. However, with
complex topologies and many sources this way of collecting data can become too slow. The trace
files will also consume a significant amount of disk space.

Monitors
With a queue monitor it is possible to track the statistics of arrivals, departures and drops in
either bytes or packets. Optionally the queue monitor can also keep an integral of the queue size
over time.
For instance, if there is a link between nodes n0 and n1, the queue monitor can be set up as
follows:

set qmon0 [$ns monitor-queue $n0 $n1]

The packet arrivals and byte drops can be tracked with the commands:

set parr [$qmon0 set parrivals_]

set bdrop [$qmon0 set bdrops_]

Dept. ECE, ATMECE Page 10


COMPUTER NETWORKS LABORATORY (15ECL68)

Besides assigning a value to a variable the set command can also be used to get the value of a
variable. For example here the set command is used to get the value of the variable “parrivals”
defined in the queue monitor class.
A flow monitor is similar to the queue monitor but it keeps track of the statistics for a flow
rather than for aggregated traffic. A classifier first determines which flow the packet belongs to
and then passes the packet to the flow monitor.
The flowmonitor can be created and attached to a particular link with the commands:

set fmon [$ns makeflowmon Fid]

$ns attach-fmon [$ns link $n1 $n3] $fmon

Notice that since these commands are related to the creation of the flow-monitor, the
commands are defined in the Simulator class, not in the Flowmonitor class. The variables and
commands in the Flowmonitor class can be used after the monitor is created and attached to a
link. For instance, to dump the contents of the flowmonitor (all flows):

$fmon dump

If you want to track the statistics for a particular flow, a classifier must be defined so that it
selects the flow based on its flow id, which could be for instance 1:

set fclassifier [$fmon classifier]

set flow [$fclassifier lookup auto 0 0 1]

Controlling the simulation


After the simulation topology is created, agents are configured etc., the start and stop of the
simulation and other events have to be scheduled. The simulation can be started and stopped with
the commands

$ns at $simtime “finish”

$ns run

Dept. ECE, ATMECE Page 11


COMPUTER NETWORKS LABORATORY (15ECL68)

The first command schedules the procedure finish at the end of the simulation, and the
second command actually starts the simulation. The finish procedure has to be defined to flush
the trace buffer, close the trace files and terminate the program with the exit routine. It can
optionally start NAM (a graphical network animator), post process information and plot this
information.
The finish procedure has to contain at least the following elements:

proc finish {} {

global ns trace_all

$ns flush-trace

close $trace_all

exit 0

Other events, such as the starting or stopping times of the clients can be scheduled in the
following way:

$ns at 0.0 “cbr0 start”

$ns at 50.0 “ftp1start”

$ns at $simtime “cbr0 stop”

$ns at $simtime “ftp1 stop”

If you have defined your own procedures, you can also schedule the procedure to start for
example every 5 seconds in the following way:

proc example {} {

global ns

set interval 5

….

Dept. ECE, ATMECE Page 12


COMPUTER NETWORKS LABORATORY (15ECL68)

$ns at [expr $now + $interval] “example”

Dept. ECE, ATMECE Page 13


COMPUTER NETWORKS LABORATORY (15ECL68)

PART A: Simulation experiments using NS2/ NS3/ OPNET/


NCTUNS/ NetSim/ QualNet or any other equivalent tool

1. Implement a point to point network with four nodes and duplex links
between them. Analyze the network performance by setting the queue size
and varying the bandwidth.

Objective: Simulate a three node point to point network with duplex links between them. Set
queue size and vary the bandwidth and find number of packets dropped.

#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open lab1.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open lab1.nam w]
$ns namtrace-all $namfile

#===================================
# Nodes Definition
#===================================
#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

Dept. ECE, ATMECE Page 14


COMPUTER NETWORKS LABORATORY (15ECL68)

#==========================================
# These below line helps to define color, label and shape
#==========================================

$ns color 1 "blue"


$ns color 2 "red"

$ns at 0.0 "$n0 color blue"


$ns at 0.0 "$n1 color red"

$ns at 0.0 "$n0 label Source/Tcp0"


$ns at 0.0 "$n1 label Source/Tcp1"
$ns at 0.0 "$n2 label Router"
$ns at 0.0 "$n3 label Destination"

$n0 shape hexagon


$n1 shape hexagon
$n3 shape square

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 100.0Mb 20ms DropTail
$ns queue-limit $n0 $n2 20 #Q1
$ns duplex-link $n1 $n2 100.0Mb 20ms DropTail
$ns queue-limit $n1 $n2 20 #Q2
$ns duplex-link $n2 $n3 100.0Mb 20ms DropTail
$ns queue-limit $n2 $n3 20 #Q3

#Above Highlighted Bandwidth and Queue Size will be changed

#Give node position (for NAM)


$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

#===================================

Dept. ECE, ATMECE Page 15


COMPUTER NETWORKS LABORATORY (15ECL68)

# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n3 $sink2
$ns connect $tcp0 $sink2
$tcp0 set packetSize_ 1500

#Setup a TCP connection


set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp1 $sink3
$tcp1 set packetSize_ 1500

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 8.0 "$ftp0 stop"

#Setup a FTP Application over TCP connection


set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 1.0 "$ftp1 start"
$ns at 8.0 "$ftp1 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile

Dept. ECE, ATMECE Page 16


COMPUTER NETWORKS LABORATORY (15ECL68)

$ns flush-trace
close $tracefile
close $namfile
exec nam lab1.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

Awk Script:

BEGIN{
tcppack=0
tcppack1=0
}
{
if($1=="r"&&$4=="3"&&$5=="tcp"&&$6=="1540")
{
tcppack++;
}
if($1=="d"&&$3=="2"&&$5=="tcp"&&$6=="1540")
{
tcppack1++;
}

}
END{
printf("\n total number of data packets received at Node 3: %d\n", tcppack++);
printf("\n total number of packets dropped at Node 2: %d\n", tcppack1++);
}

Dept. ECE, ATMECE Page 17


COMPUTER NETWORKS LABORATORY (15ECL68)

Trace File (lab1.tr):

Outcome:

Bandwidth(MB) Queue Size Received at Dropped at


N0-N2, N1-N2, N2-N3 Q1, Q2, Q3 Node 3 Node 2
100,100,100 20, 20,20 6820 0
100,100,1 20, 20,2 508 45
300,300,10 50, 50,3 3021 48

Dept. ECE, ATMECE Page 18


COMPUTER NETWORKS LABORATORY (15ECL68)

Execution Steps:

Dept. ECE, ATMECE Page 19


COMPUTER NETWORKS LABORATORY (15ECL68)

NSG 2.1 Generated Network:

Dept. ECE, ATMECE Page 20


COMPUTER NETWORKS LABORATORY (15ECL68)

Network Animation (lab1.nam):

Dept. ECE, ATMECE Page 21


COMPUTER NETWORKS LABORATORY (15ECL68)

2. Implement a four node point to point network with links N0- N2, N1- N2 and
N2-N3. Apply TCP agent between N0-N3 and UDP between N1-N3. Apply
relevant applications over TCP and UDP agents changing the parameter and
determine the number of packets sent by TCP/UDP.

Objective: Simulate a four node point to point network with the links connected as follows: n0
– n2, n1 – n2 and n2 – n3. Apply TCP agent between n0 – n3 and UDP agent between n1 – n3.
Apply relevant applications over TCP and UDP agents changing the parameter and determine the
number of packets sent by TCP / UDP

#===================================
# Simulation parameters setup
#===================================
set val(stop) 50.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open lab2.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open lab2.nam w]
$ns namtrace-all $namfile

#===================================
# Nodes Definition
#===================================
#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#========================================================

Dept. ECE, ATMECE Page 22


COMPUTER NETWORKS LABORATORY (15ECL68)

# These below line helps to define color, label and shape


#========================================================

$ns color 1 "blue"


$ns color 2 "red"

$ns at 0.0 "$n0 color blue"


$ns at 0.0 "$n1 color red"

$ns at 0.0 "$n0 label Source/Tcp0"


$ns at 0.0 "$n1 label Source/Udp1"
$ns at 0.0 "$n2 label Router"
$ns at 0.0 "$n3 label Destination"

$n0 shape hexagon


$n1 shape hexagon
$n3 shape square

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 200.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 50
$ns duplex-link $n2 $n3 200.0Mb 10ms DropTail
$ns queue-limit $n2 $n3 50
$ns duplex-link $n1 $n2 200.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 50

#Give node position (for NAM)


$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 color "green"


$ns duplex-link-op $n0 $n2 label "TCP Packets"
$ns duplex-link-op $n1 $n2 label "UDP Packets"
$ns duplex-link-op $n2 $n3 label "UDP+TCP Packets"

Dept. ECE, ATMECE Page 23


COMPUTER NETWORKS LABORATORY (15ECL68)

#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp0 $sink3
$tcp0 set packetSize_ 1500 # Change the Packet Size
$tcp0 set interval_ 0.1

#Setup a UDP connection


set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set null2 [new Agent/Null]
$ns attach-agent $n3 $null2
$ns connect $udp1 $null2
$udp1 set packetSize_ 1500
$udp1 set interval_ 0.1

$tcp0 set class_ 1


$udp1 set class_ 2

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 9.0 "$ftp0 stop"

#Setup a CBR Application over UDP connection


set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set packetSize_ 1000 # Change the Packet Size

Dept. ECE, ATMECE Page 24


COMPUTER NETWORKS LABORATORY (15ECL68)

$cbr1 set rate_ 1.0Mb


$cbr1 set random_ null
$ns at 1.0 "$cbr1 start"
$ns at 9.0 "$cbr1 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam lab2.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

Awk Script:

BEGIN{
tcppack=0
tcppack1=0
}
{
if($1=="r"&&$4=="2"&&$5=="tcp"&&$6=="1540") #1540, 1040, 1340
{
tcppack++;
}
if($1=="r"&&$4=="2"&&$5=="cbr"&&$6=="1500") #1500, 1500, 1500
{
tcppack1++;
}

Dept. ECE, ATMECE Page 25


COMPUTER NETWORKS LABORATORY (15ECL68)

}
END{
printf("\n total number of TCP data packets sent between Node 0 and Node 2:
%d\n", tcppack++);
printf("\n total number of UDP data packets sent between Node 1 and Node 2:
%d\n", tcppack1++);
}

Trace File (lab2.tr):

Outcome:

Simulation Time (Sec) Packet Size (Byte) Sent at Node 2


Start Time:1 TCP:1500 TCP:3930
Stop Time:9 UDP:1500 UDP:1001
Start Time:1 TCP:1000 TCP:9410
Stop Time:20 UDP:1500 UDP:1584
Start Time:1 TCP:1300 TCP:11910
Stop Time:25 UDP:1500 UDP:2000

Dept. ECE, ATMECE Page 26


COMPUTER NETWORKS LABORATORY (15ECL68)

Execution Steps:

NSG 2.1 Generated Network:

Dept. ECE, ATMECE Page 27


COMPUTER NETWORKS LABORATORY (15ECL68)

Network Animation (lab2.nam):

Dept. ECE, ATMECE Page 28


COMPUTER NETWORKS LABORATORY (15ECL68)

3. Implement Ethernet LAN using N (6-10) nodes. Compare the throughput


by changing the error rate and data rate.

Objective: Simulate an Ethernet LAN using N (6-10) nodes, change error rate and data rate
and compare throughput

set ns [new Simulator]


set tf [open lab3.tr w]
$ns trace-all $tf

set nf [open lab3.nam w]


$ns namtrace-all $nf

$ns color 0 blue

set n0 [$ns node]


$n0 color "red"
set n1 [$ns node]
$n1 color "red"
set n2 [$ns node]
$n2 color "red"
set n3 [$ns node]
$n3 color "red"
set n4 [$ns node]
$n4 color "magenta"
set n5 [$ns node]
$n5 color "magenta"
set n6 [$ns node]
$n6 color "magenta"
set n7 [$ns node]
$n7 color "magenta"

$n1 label "Source/UDP"


$n3 label "Error Node"
$n7 label "Destination/Null"

$ns make-lan "$n0 $n1 $n2 $n3" 100Mb 300ms LL Queue/DropTail Mac/802_3

Dept. ECE, ATMECE Page 29


COMPUTER NETWORKS LABORATORY (15ECL68)

$ns make-lan "$n4 $n5 $n6 $n7" 100Mb 300ms LL Queue/DropTail Mac/802_3

$ns duplex-link $n3 $n4 100Mb 300ms DropTail


$ns duplex-link-op $n3 $n4 color "green"

# set error rate. Here ErrorModel is a class and it is single word and space should
not be given between Error and Model
# lossmodel is a command and it is single word. Space should not be given
between loss and model

set err [new ErrorModel]


$ns lossmodel $err $n3 $n4
$err set rate_ 0.1 #Change the Error Rate 0.1, 0.3, 0.5,

# error rate should be changed for each output like 0.1,0.3,0.5…. */


set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set fid_ 0
$cbr set packetSize_ 1000
$cbr set interval_ 0.001 #Change the Data Rate 0.001, 0.01, 0.1,
set null [new Agent/Null]
$ns attach-agent $n7 $null

$ns connect $udp $null

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab3.nam &
exit 0
}

$ns at 0.1 "$cbr start"


$ns at 3.0 "finish"

Dept. ECE, ATMECE Page 30


COMPUTER NETWORKS LABORATORY (15ECL68)

$ns run

Awk Script:

BEGIN{
tcppack=0
tcppack1=0
}
{
if($1=="r"&&$4=="7"&&$5=="cbr"&&$6=="1000")
{
tcppack++;
}
}
END{
printf("\n total number of data packets at Node 7: %d\n", tcppack++);
}

Outcome:

Error Rate Data Rate Received at Node 7


0.1 0.001 1255
0.3 0.01 95
0.5 0.1 05

Dept. ECE, ATMECE Page 31


COMPUTER NETWORKS LABORATORY (15ECL68)

Trace File (lab3.tr):

Network Animation (lab3.nam):

Dept. ECE, ATMECE Page 32


COMPUTER NETWORKS LABORATORY (15ECL68)

Execution Steps:

Dept. ECE, ATMECE Page 33


COMPUTER NETWORKS LABORATORY (15ECL68)

4. Implement Ethernet LAN using N nodes and assign multiple traffic to the
nodes and obtain congestion window for different sources/ destinations.

Objective: Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and plot
congestion window for different source / destination

set ns [new Simulator]


set tf [open lab4.tr w]
$ns trace-all $tf
set nf [open lab4.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns make-lan "$n0 $n1 $n2 $n3" 10mb 10ms LL Queue/DropTail Mac/802_3

$ns color 1 "blue"


$ns color 2 "red"

$ns at 0.0 "$n3 color blue"


$ns at 0.0 "$n1 color red"

$ns at 0.0 "$n0 label Source/Tcp0"


$ns at 0.0 "$n2 label Source/Tcp2"
$ns at 0.0 "$n1 label Destination/Sink1"
$ns at 0.0 "$n3 label Destination/Sink3"

$n0 shape hexagon


$n2 shape hexagon
$n1 shape square
$n3 shape square

set tcp0 [new Agent/TCP/Reno]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3

Dept. ECE, ATMECE Page 34


COMPUTER NETWORKS LABORATORY (15ECL68)

$ns connect $tcp0 $sink3


set tcp2 [new Agent/TCP]
$ns attach-agent $n2 $tcp2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns connect $tcp2 $sink1

$tcp0 set class_ 1


$tcp2 set class_ 2

######To trace the congestion window##########


set file1 [open file1.tr w]
$tcp0 attach $file1
$tcp0 trace cwnd_
#$tcp0 set maxcwnd_ 10
set file2 [open file2.tr w]
$tcp2 attach $file2
$tcp2 trace cwnd_
proc finish { } {
global nf tf ns
$ns flush-trace
exec nam lab4.nam &
close $nf
close $tf
exit 0
}
$ns at 0.1 "$ftp0 start"
$ns at 1.5 "$ftp0 stop"
$ns at 2 "$ftp0 start"
$ns at 3 "$ftp0 stop"
$ns at 0.2 "$ftp2 start"
$ns at 2 "$ftp2 stop"
$ns at 2.5 "$ftp2 start"
$ns at 4 "$ftp2 stop"
$ns at 5.0 "finish"

$ns run

Dept. ECE, ATMECE Page 35


COMPUTER NETWORKS LABORATORY (15ECL68)

1. File1.tr is the Congestion Window (CWND) value of TCP agent.


2. File2.tr is the Congestion Window (CWND) value of TCPReno agent.

Dept. ECE, ATMECE Page 36


COMPUTER NETWORKS LABORATORY (15ECL68)

Network Animation (lab4.nam):

Congestion Window verus Simulation Time


50

40

30
CGWD
20 TCP
TCPReno
10

0
0.5 1 1.5 2 2.5 3
Simulation Time (Sec.)
.
Outcome:
Time TCP TCPReno
0.5 23.02 21.91
1 29.73 29.53
1.5 34.59 34.71
2 2 41.11
2.5 26.23 2
3 32.84 24.62
Table 1: Above values are taken from file1.tr and file2.tr

Dept. ECE, ATMECE Page 37


COMPUTER NETWORKS LABORATORY (15ECL68)

5. Implement ESS with transmission nodes in Wireless LAN and obtain then
Performance Parameters.

set ns [new Simulator]


set tf [open lab5.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1000 1000
set nf [open lab5.nam w]
$ns namtrace-all-wireless $nf 1000 1000

$ns node-config -adhocRouting AODV \


-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace OFF

create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

$n0 label "tcp0"


$n1 label "sink1/tcp1"
$n2 label "sink2"

$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0

Dept. ECE, ATMECE Page 38


COMPUTER NETWORKS LABORATORY (15ECL68)

$n2 set X_ 600


$n2 set Y_ 600
$n2 set Z_ 0

$ns at 0.1 "$n0 setdest 50 50 15"


$ns at 0.1 "$n1 setdest 100 100 25"
$ns at 0.1 "$n2 setdest 600 600 25"
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns connect $tcp0 $sink1
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp1 $sink2
$ns at 5 "$ftp0 start"
$ns at 5 "$ftp1 start"
$ns at 100 "$n1 setdest 550 550 15"
$ns at 190 "$n1 setdest 70 70 15"
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab5.nam &
close $tf
exit 0
}
$ns at 250 "finish"
$ns run

BEGIN{
tcppack=0
tcppack1=0
}

Dept. ECE, ATMECE Page 39


COMPUTER NETWORKS LABORATORY (15ECL68)

{
if($1=="s"&&$3=="_0_"&&$4=="AGT"&&$8=="1040")
{
tcppack++;
}
if($1=="r"&&$3=="_2_"&&$4=="AGT"&&$8=="1040")
{
tcppack1++;
}
}
END{
printf("\n total number of data packets sent from Node 0: %d\n",
tcppack++);
printf("\n total number of data packets received at Node 2: %d\n",
tcppack++);

Network Animation (lab5.nam):

Dept. ECE, ATMECE Page 40


COMPUTER NETWORKS LABORATORY (15ECL68)

Trace File (lab5.tr):

Dept. ECE, ATMECE Page 41


COMPUTER NETWORKS LABORATORY (15ECL68)

Execution Steps:

Dept. ECE, ATMECE Page 42


COMPUTER NETWORKS LABORATORY (15ECL68)

6. Implementation of Link State Routing Algorithm.

Objective: Simulate simple ESS and with transmitting nodes in wireless LAN by simulation and
determine the performance with respect to transmission of packets.

#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open lab6.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open lab6.nam w]
$ns namtrace-all $namfile

#===================================
# Nodes Definition
#===================================
#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n1 100.0Mb 10ms DropTail

Dept. ECE, ATMECE Page 43


COMPUTER NETWORKS LABORATORY (15ECL68)

$ns queue-limit $n0 $n1 50


$ns duplex-link $n0 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 50
$ns duplex-link $n2 $n3 100.0Mb 10ms DropTail
$ns queue-limit $n2 $n3 50
$ns duplex-link $n1 $n3 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n3 50
$ns duplex-link $n3 $n4 100.0Mb 10ms DropTail
$ns queue-limit $n3 $n4 50
$ns duplex-link $n0 $n3 100.0Mb 10ms DropTail
$ns queue-limit $n0 $n3 50
$ns duplex-link $n1 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 50

#Give node position (for NAM)


$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n1 $n3 orient left-down
$ns duplex-link-op $n3 $n4 orient left-down
$ns duplex-link-op $n0 $n3 orient right-down
$ns duplex-link-op $n1 $n2 orient left-down

#Set the link costs. All link costs are symmetric

$ns cost $n0 $n1 2


$ns cost $n0 $n2 1
$ns cost $n0 $n3 3

$ns cost $n1 $n0 2


$ns cost $n1 $n2 2
$ns cost $n1 $n3 3

$ns cost $n2 $n1 2


$ns cost $n2 $n0 1
$ns cost $n2 $n3 1

$ns cost $n3 $n2 1


$ns cost $n3 $n1 3

Dept. ECE, ATMECE Page 44


COMPUTER NETWORKS LABORATORY (15ECL68)

$ns cost $n3 $n0 3


$ns cost $n3 $n4 2

$ns cost $n4 $n3 2

#===================================
# Agents Definition
#===================================
#Setup a UDP connection
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set null1 [new Agent/Null]
$ns attach-agent $n4 $null1
$ns connect $udp0 $null1
$udp0 set packetSize_ 1500

#===================================
# Applications Definition
#===================================
#Setup a CBR Application over UDP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 1.0Mb
$cbr0 set random_ null
$ns at 1.0 "$cbr0 start"
$ns at 5.0 "$cbr0 stop"

$ns rtproto LS
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam lab6.nam &

Dept. ECE, ATMECE Page 45


COMPUTER NETWORKS LABORATORY (15ECL68)

exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

Network Animation (lab6.nam):

Outcome:
 Total Number of Routing Paths: 4
1. N0-N1-N2-N3-N4: Total Cost is of 7
2. N0-N2- N1 -N3-N4 : Total Cost is of 8
3. N0-N2-N3-N4 : Total Cost is of 4
4. N0-N3-N4 : Total Cost is of 5

 Shortest according to Link State algorithm is N0-N2-N3-N4 having Total


Cost is of 4

Dept. ECE, ATMECE Page 46


COMPUTER NETWORKS LABORATORY (15ECL68)

Dept. ECE, ATMECE Page 47


COMPUTER NETWORKS LABORATORY (15ECL68)

PART-B: Implement the following in C/C++


1. Write a C/C++ program for distance vector algorithm to find suitable path for
transmission.

Objective: To write a program for distance vector algorithm to find suitable path for transmission
#include<stdio.h>
#include<stdlib.h>
#define nul 1000
#define nodes 10
int no;
struct node
{
int a[nodes][3];
}
router[nodes];
void init(int r)
{
int i;
for(i=1;i<=no;i++)
{
router[r].a[i][1]=i;
router[r].a[i][2]=999;
router[r].a[i][3]=nul;
}
router[r].a[r][2]=0;
router[r].a[r][3]=r;
}
void inp(int r)
{
int i;
printf("\n enter dist to the node %d to other nodes",r);
printf("\n please enter 999 if there is no direct route\n");
for(i=1;i<=no;i++)
{
if(i!=r)
{
printf("\n enter dist to the node %d:",i);
scanf("%d",&router[r].a[i][2]);
router[r].a[i][3]=i;
}
}
}
void display(int r)
{

Dept. ECE, ATMECE Page 48


COMPUTER NETWORKS LABORATORY (15ECL68)

int i;
printf("\n\n the routimg table for node %d is as follows",r);
for(i=1;i<=no;i++)
{
if(router[r].a[i][2]>=999)
printf("\n\t\t\t %d \t no link \t no hop",router[r].a[i][1]);
else
printf("\n\t\t\t %d \t %d \t\t %d",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
}
}
void dv_algo(int r)
{
int i,j,z;
for(i=1;i<=no;i++)
{
if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0)
{
for(j=1;j<=no;j++)
{
z=router[r].a[i][2]+router[i].a[j][2];
if(router[r].a[j][2]>z)
{
router[r].a[j][2]=z;
router[r].a[j][3]=i;
}
}
}
}
}
void find(int x,int y)
{
if(router[x].a[y][3]!=y)
{
find(x,router[x].a[y][3]);
printf("%d-->",router[x].a[y][3]);
find(router[x].a[y][3],y);
return;
}
}
int main()
{
int i,j,x,y;
int choice;
printf("enter the no of nodes required(less than 10 pls):");
scanf("%d",&no);

Dept. ECE, ATMECE Page 49


COMPUTER NETWORKS LABORATORY (15ECL68)

for(i=1;i<=no;i++)
{
init(i);
inp(i);
}
printf("\n the configuration of the nodes after initilization is as follows:");
for(i=i;i<=no;i++)
display(i);
for(j=1;j<=no;j++)
for(i=1;i<=no;i++)
dv_algo(i);
printf("\n the configuration of the nodes after computation of path is as follows:");
for(i=1;i<=no;i++)
display(i);
while(1)
{
printf("\n\n enter 1 to continue 0 to quit:");
scanf("\%d",&choice);
if(choice!=1)
break;
printf("\n enter the nodes btn which shortest path is to be found:\n");
scanf("%d%d",&x,&y);
printf("\n the shortest path is:");
printf("%d-->",x);
find(x,y);
printf("%d",y);
printf("\n the length of the shortest path is%d",router[x].a[y][2]);
}
return 0;
}

/* output
enter the no of nodes required(less than 10 pls):6

enter dist to the node 1 to other nodes


please enter 999 if there is no direct route

enter dist to the node 2:3

enter dist to the node 3:2

enter dist to the node 4:999

Dept. ECE, ATMECE Page 50


COMPUTER NETWORKS LABORATORY (15ECL68)

enter dist to the node 5:2

enter dist to the node 6:999

enter dist to the node 2 to other nodes


please enter 999 if there is no direct route

enter dist to the node 1:2

enter dist to the node 3:1

enter dist to the node 4:999

enter dist to the node 5:1

enter dist to the node 6:3

enter dist to the node 3 to other nodes


please enter 999 if there is no direct route

enter dist to the node 1:1

enter dist to the node 2:1

enter dist to the node 4:999

enter dist to the node 5:3

enter dist to the node 6:3

enter dist to the node 4 to other nodes


please enter 999 if there is no direct route

enter dist to the node 1:999

enter dist to the node 2:999

enter dist to the node 3:999

enter dist to the node 5:3

enter dist to the node 6:1

enter dist to the node 5 to other nodes


please enter 999 if there is no direct route

Dept. ECE, ATMECE Page 51


COMPUTER NETWORKS LABORATORY (15ECL68)

enter dist to the node 1:3

enter dist to the node 2:999

enter dist to the node 3:2

enter dist to the node 4:1

enter dist to the node 6:999

enter dist to the node 6 to other nodes


please enter 999 if there is no direct route

enter dist to the node 1:999

enter dist to the node 2:2

enter dist to the node 3:3

enter dist to the node 4:2

enter dist to the node 5:999

the configuration of the nodes after initilization is as follows:


the configuration of the nodes after computation of path is as follows:

the routimg table for node 1 is as follows


1 0 1
2 3 2
3 2 3
4 3 5
5 2 5
6 4 4

the routimg table for node 2 is as follows


1 2 1
2 0 2
3 1 3
4 2 5
5 1 5
6 3 6

the routimg table for node 3 is as follows


1 1 1

Dept. ECE, ATMECE Page 52


COMPUTER NETWORKS LABORATORY (15ECL68)

2 1 2
3 0 3
4 3 2
5 2 2
6 3 6

the routimg table for node 4 is as follows


1 5 2
2 3 6
3 4 6
4 0 4
5 3 5
6 1 6

the routimg table for node 5 is as follows


1 3 1
2 3 3
3 2 3
4 1 4
5 0 5
6 2 4

the routimg table for node 6 is as follows


1 4 2
2 2 2
3 3 3
4 2 4
5 3 2
6 0 6

enter 1 to continue 0 to quit:1

Outcome:
enter the nodes btn which shortest path is to be found:
1
6

the shortest path is:1-->5-->4-->6


the length of the shortest path is4

enter 1 to continue 0 to quit:*/

Dept. ECE, ATMECE Page 53


COMPUTER NETWORKS LABORATORY (15ECL68)

2. Write a program for error detecting code using CRC-CCITT (16-bits).

Objective: To Write a C program for error detecting code using CRC-CCITT (16-bits)

#include<stdio.h>
int a[100],b[100],i,j,len,k,count=0;

//Generator Polynomial:g(x)=x^16+x^12+x^5+1
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,};

int main()
{
void div();
printf("\nEnter the length of Data Frame :");
scanf("%d",&len);
printf("\nEnter the Message :");
for(i=0;i<len;i++)
scanf("%d",&a[i]);

//Append r(16) degree Zeros to Msg bits


for(i=0;i<16;i++)
a[len++]=0;
//Xr.M(x) (ie. Msg+16 Zeros)
for(i=0;i<len;i++)
b[i]=a[i];

//No of times to be divided ie. Msg Length


k=len-16;
div();
for(i=0;i<len;i++)
b[i]=b[i]^a[i]; //MOD 2 Substraction
printf("\nData to be transmitted : ");
for(i=0;i<len;i++)
printf("%2d",b[i]);

printf("\n\nEnter the Reveived Data : ");


for(i=0;i<len;i++)
scanf("%d",&a[i]);

div();

for(i=0;i<len;i++)
if(a[i]!=0)
{
printf("\nERROR in Recived Data");

Dept. ECE, ATMECE Page 54


COMPUTER NETWORKS LABORATORY (15ECL68)

return 0;
}
printf("\nData Recived is ERROR FREE");
}

void div()
{
for(i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(j=i;j<17+i;j++)
a[j]=a[j]^gp[count++];
}
count=0;
}
}

Outcome:

*****************************************************************

Dept. ECE, ATMECE Page 55


COMPUTER NETWORKS LABORATORY (15ECL68)

3. Write a program for byte stuffing

Objective: 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.
#include<string.h>
#include<stdio.h>

main()
{
char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3];
int i,j,p=0,q=0;

printf("Enter characters to be stuffed : ");


scanf("%s",a);
printf("\nEnter a character that represents starting delimiter : ");
scanf(" %c",&sd);
printf("\nEnter a character that represents ending delimiter : ");
scanf(" %c",&ed);
x[0]=s[0]=s[1]=sd;
x[1]=s[2]='\0';
y[0]=d[0]=d[1]=ed;
d[2]=y[1]='\0';
strcat(fs,x);

for(i=0;i<strlen(a);i++)
{
t[0]=a[i];
t[1]='\0';

Dept. ECE, ATMECE Page 56


COMPUTER NETWORKS LABORATORY (15ECL68)

if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("\nAfter stuffing : %s",fs);

getch();

Outcome:

Dept. ECE, ATMECE Page 57


COMPUTER NETWORKS LABORATORY (15ECL68)

4. Write a program for bit stuffing

#include<string.h>
#include<stdio.h>
main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
printf("enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a)-4;i++)
{
for(j=i;j<i+5;j++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");

Dept. ECE, ATMECE Page 58


COMPUTER NETWORKS LABORATORY (15ECL68)

i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<strlen(a);q++)
{
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}

Dept. ECE, ATMECE Page 59


COMPUTER NETWORKS LABORATORY (15ECL68)

Outcome

Dept. ECE, ATMECE Page 60


COMPUTER NETWORKS LABORATORY (15ECL68)

5. Write a program for congestion control using Leaky bucket algorithm.

Objective: Write a program for congestion control using Leaky bucket algorithm.

#include<stdio.h>
#include<stdlib.h>
#define MIN(x,y) (x>y)?y:x
int main()
{
int orate,drop=0,cap,x,count=0,inp[10]={0},i=0,nsec,ch;
printf("\n enter bucket size : ");
scanf("%d",&cap);
printf("\n enter output rate :");
scanf("%d",&orate);
do
{
printf("\n enter number of packets coming at second %d :",i+1);
scanf("%d",&inp[i]);
i++;
printf("\n enter 1 to contiue or 0 to quit..........");
scanf("%d",&ch);
}
while(ch);
nsec=i;
printf("\n second \t recieved \t sent \t dropped \t remained \n");
for(i=0;count || i<nsec;i++)
{
printf(" %d",i+1);
printf(" \t%d\t ",inp[i]);
printf(" \t %d\t ",MIN((inp[i]+count),orate));
if((x=inp[i]+count-orate)>0)
{
if(x>cap)
{
count=cap;
drop=x-cap;
}
else
{
count=x;
drop=0;
}
}
else
{

Dept. ECE, ATMECE Page 61


COMPUTER NETWORKS LABORATORY (15ECL68)

drop=0;
count=0;
}
printf(" \t %d \t %d \n",drop,count);
}
return 0;
}

Outcome:

Dept. ECE, ATMECE Page 62


COMPUTER NETWORKS LABORATORY (15ECL68)

6. Write a program for stop and wait protocol

Objective: To write a program for stop and wait protocol.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

int i,j,noframes,x,x1=10,x2;

// clrscr();

for(i=0;i<200;i++)

rand();

noframes=rand()/200;

i=1;

j=1;

noframes=noframes/4;

printf("number of frames is %d ",noframes);

// scanf("%d",&noframes);

getch();

while(noframes>0)

printf("\nsending frames is %d",i);

srand(x1++);

x=rand()%15;

if(x%5==0)

Dept. ECE, ATMECE Page 63


COMPUTER NETWORKS LABORATORY (15ECL68)

for(x2=1;x2<2;x2++)

printf("\n waiting for %d seconds\n",x2);

sleep(x2);

printf("\n sending frames %d\n",i);

srand(x1++);

x=rand()%10;

printf("\n ack for frame %d\n",j);

noframes=noframes-1;

i++;

j++;

printf("\n end of stop and wait protocol\n");

Dept. ECE, ATMECE Page 64


COMPUTER NETWORKS LABORATORY (15ECL68)

Outcome:

Dept. ECE, ATMECE Page 65


COMPUTER NETWORKS LABORATORY (15ECL68)

7. Write a program for sliding window protocol

Objective: To write a C program for sliding window protocol

#include<stdio.h>

int main()

int w,i,f,frames[50];

printf("Enter window size: ");

scanf("%d",&w);

printf("\nEnter number of frames to transmit: ");

scanf("%d",&f);

printf("\nEnter %d frames: ",f);

for(i=1;i<=f;i++)

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

printf("\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");

printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);

Dept. ECE, ATMECE Page 66


COMPUTER NETWORKS LABORATORY (15ECL68)

for(i=1;i<=f;i++)

if(i%w==0)

printf("%d\n",frames[i]);

printf("Acknowledgement of above frames sent is received by sender\n\n");

else

printf("%d ",frames[i]);

if(f%w!=0)

printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;

Dept. ECE, ATMECE Page 67


COMPUTER NETWORKS LABORATORY (15ECL68)

Outcome

Dept. ECE, ATMECE Page 68

You might also like