Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Experiment No: 2 Date: 01/08/2022

Simulation and analysis of point to point network using Ns-2

Name : R.NISHIKANTH REDDY


Reg. No : 20BLC1045
Faculty Name : HEMANTH.C

AIM:

a. To simulate a simple point-to-point wired network using NS-2.


b. To analyze the generated trace file for this point-point scenario and measure network
performance in terms of Packet Delivery ratio and end-to-end delay using “grep” command
c. Analyse for two different case i) when packet generation rate (generated traffic) is less
than the capacity of link ii) when packet generation rate (generated traffic) is more than the
capacity of link

SOFTWARE USED:

Network Simulator (NS2), 2.35 version.

PROGRAM:

CODE:
1. First of all, create a TCL script file with its extension (using gedit)

gedit Lab2.tcl &

2. Now, we will create a new simulator object for our network.

set ns [new Simulator]

# We created a variable ns using set and assigned it as new simulator


object. Hash is used for commenting. Note: it won’t work in same line.

3. Now, we will create two files, nam-trace file and trace file. Then, we
will setup in those files. In nam-file, all the data like nodes,
connection, topology etc. is present and in normal trace file, log data,
like delay no. of packets etc. are present.
set nf [open out.nam w]

$ns namtrace-all $nf

set tf [open out.tr w]

$ns trace-all $tf

# We created two variable nf and tf, in which we will create and open
the files out.nam and out.tr with write permisiion. Then we are
setting up respective tracing using suitable commands. Dollar sign is
used to access the value of that variable.

4. Now we will write the finish procedure, which should come in the
ending of script.

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

# We created global variables. Then we flushed all the remaining logs


in respective files. After that we closed the file objects nf and tf.
Finally, we execute the nam file and exit the procedure.

5. Now, we will create two nodes and establish a communication


medium between them.

set n0 [$ns node]


set n1 [$ns node]

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

# We create two nodes n0 and n1 and create a full duplex-link


between them, in which Data-Rate will be 2 Mega-bits per second
(Uppercase B will denote Byte), with a delay of 10 milli-seconds. Also,
we are implementing Drop-Tail mechanism, in which the packets
which will enter in end of queue will be dropped if the queue is full.

6. Now, we will define the protocol to carry-out data transfer between


the two nodes. Here, we will use UDP (User Datagram Protocol),
which is the lightest protocol at Transport Layer.

set udp [new Agent/UDP]

$ns attach-agent $n0 $udp

set null [new Agent/Null]

$ns attach-agent $n1 $null

$ns connect $udp $null

# We create protocol object for udp and assign it to node n0. Now,
we want n1 to be receiver alone, hence create an assign a null object
to it and finally, connect both the objects.

7. Now, we will generate some traffic by using CBR (Constant Bit Rate)

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set type_ CBR

$cbr set packet_size_ 1000

$cbr set rate_ 1mb


# We create cbr object, which is attached to the udp object. Also,
packet size and rate is defined as 100 bits and 1 megabits per second.

8. Now we will schedule the events for cbr object and ending.

$ns at 0.1 “$cbr start”

$ns at 0.5 “$cbr stop”

$ns at 5.0 “finish”

$ns run

# Now, we have defined for how long the traffic will take place. The
finish line should be given after the finish procedure. In the end,
don’t forget to give the run statement.

Additional Commands:

puts “CBR packet size = [$cbr set packet_size_]”

puts “CBR interval = [$cbr set interval_]”

#Will print the respective details. To be given before run statement


RESULT & INFERENCES:
Each trace line starts with an event (+, -, d, r) descriptor followed by the simulation time (in seconds)
of that event, and from and to node, which identify the link on which the event occurred.
So, the total amount of Packets Received = Total number of Packets sent + dropped i.e.:
50=50+0
If the Packet Size is Increased or if the Cbr rate is increased, then there is a lose in number of packets
received (they are dropped).

You might also like