CN Print
CN Print
AIM:
To study the Network Simulator - NS2.
NS-2
Ns-2 is a discrete event simulator targeted at networking research. Ns provides substantial
support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite)
networks.
Downloading/Installing ns&nam
One can build ns either from the various packages (Tcl/Tk, otcl, etc.), or can download an 'all-in- one' package.
web page: http://www.isi.edu/nsnam/ns/ns-build.html
Starting ns
Start ns with the command 'ns <tclscript>', where '<tclscript>' is the name of a Tcl script file which defines the
simulation scenario (i.e. the topology and the events). Just start ns without any arguments and enter the Tcl
commands in the Tcl shell, but that is definitely less comfortable.
Everything else depends on the Tcl script. The script might create some output on stdout, it might write a trace file or
it might start nam to visualize the simulation.
Starting nam
One can either start nam with the command 'nam<nam-file>' where '<nam-file>' is the name of a nam trace file
that was generated by ns, or one can execute it directly out of the Tcl simulation script for the simulation which you
want to visualize.
1
First TCL Script
Now we are going to write a 'template' that you can use for all of the first Tcl scripts. You can write your Tcl scripts in any text
editor like joe or emacs. I suggest that you call this first example 'example1.tcl'.
First of all, you need to create a simulator object. This is done with the command set ns [new Simulator]
Now we open a file for writing that is going to be used for the nam trace data. setnf [open out.nam w]
$ns namtrace-all $nf
The first line opens the file 'out.nam' for writing and gives it the file handle 'nf'. In the second line we tell the simulator object that
we created above to write all simulation data that is going to be relevant for nam into this file.
The next step is to add a 'finish' procedure that closes the trace file and starts nam.
proc finish {} {
global ns nf
$ns flush-trace close $nf execnamout.nam&
exit 0
}
You don't really have to understand all of the above code yet. It will get clearer to you once you see what the code does.
The next line tells the simulator object to execute the 'finish' procedure after 5.0 seconds of simulation time.
$ns at 5.0 "finish"
You probably understand what this line does just by looking at it. ns provides you with a very simple way to schedule events with
the 'at' command. The last line finally starts the simulation.
$ns run
Ns-2 MAIN CONSOLE WITH NETWORK ANIMATOR
Result:
Thus the Network Simulator NS2 is studied successfully.
2
Ex.No:7(B) Simulation of Congestion Control Algorithms using NS
AIM:
Simulation of Congestion Control Algorithms using NS
3
emulate using real hardware - for instance, simulating a scenario with several nodes or experimenting with a new
protocol in the network. Network simulators are particularly useful in allowing researchers to test new networking
protocols or changes to existing protocols in a controlled and reproducible environment. A typical network simulator
encompasses a wide range of networking technologies and can help the users to build complex networks from basic
building blocks such as a variety of nodes and links. With the help of simulators, one can design hierarchical
networks using various types of nodes like computers, hubs, bridges, routers, switches, links, mobile units etc.
Various types of Wide Area Network (WAN) technologies like TCP, ATM, IP etc. and Local Area
Network (LAN) technologies like Ethernet, token rings etc., can all be simulated with a typical simulator and the
user can test, analyze various standard results apart from devising some novel protocol or strategy for routing etc.
Network simulators are also widely used to simulate battlefield networks in Network-centricwarfare.
There are a wide variety of network simulators, ranging from the very simple to the very complex.
Minimally, a network simulator must enable a user to represent a network topology, specifying the nodes on the
network, the links between those nodes and the traffic between the nodes. More complicated systems may allow the
user to specify everything about the protocols used to handle traffic in a network. Graphical applications allow users
to easily visualize the workings of their simulated environment. Text-based applications may provide a less intuitive
interface, but may permit more advanced forms of customization.
Packet loss
Packet loss occurs when one or more packets of data travelling across a computer network fail to reach their
destination. Packet loss is distinguished as one of the three main error types encountered in digital communications;
the other two being bit error and spurious packets caused due to noise.
Packets can be lost in a network because they may be dropped when a queue in the network node
overflows. The amount of packet loss during the steady state is another important property of a congestion control
scheme. The larger the value of packet loss, the more difficult it is for transport layer protocols to maintain high
bandwidths, the sensitivity to loss of individual
4
packets, as well as to frequency and patterns of loss among longer packet sequences is strongly dependent on the
application itself.
Throughput
Throughput is the main performance measure characteristic, and most widely used. In communication
networks, such as Ethernet or packet radio, throughput or network throughput is the average rate of successful
message delivery over a communication channel. Throughput is usually measured inbitsper second (bit/s orbps), and
sometimes in data packets per second or data packets per time slot. This measures how soon the receiver is able to
get a certain amount of data send by the sender. It is determined as the ratio of the total data received to the end to
end delay. Throughput is an important factor which directly impacts the network performance.
Delay
Delay is the time elapsed while a packet travels from one point e.g., source premise or
network ingress to destination premise or network degrees. The larger the value of delay, the more difficult it is for
transport layer protocols to maintain high bandwidths. We will calculate end to end delay.
Queue Length
A queuing system in networks can be described as packets arriving for service, waiting for service if it is not
immediate, and if having waited for service, leaving the system after being served. Thus queue length is very
important characteristic to determine that how well the active queue management of the congestion control
algorithm has been working.
Program:
Sample.tcl
6
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set interval_ 0.010
$cbr set random_ false
OUTPUT :
7
Result:
Thus the simulation of Congestion Control Algorithms using NS was implemented.
8
Ex.No:8 Study of TCP/UDP performance using Simulation tool.
AIM:
TCP is reliable protocol. That is, the receiver always sends either positive or negative acknowledgement
about the data packet to the sender, so that the sender always has bright clue about whether the data packet
is reached the destination or it needs to resend it.
TCP ensures that the data reaches intended destination in the same order it was sent.
TCP is connection oriented. TCP requires that connection between two remote points be established before
sending actual data.
TCP provides error-checking and recovery mechanism.
TCP provides end-to-end communication.
TCP provides flow control and quality of service.
TCP operates in Client/Server point-to-point mode.
TCP provides full duplex server, i.e. it can perform roles of both receiver andsender.
The User Datagram Protocol (UDP) is simplest Transport Layer communication protocol available of the
TCP/IP protocol suite. It involves minimum amount of communication mechanism. UDP is said to be an
unreliable transport protocol but it uses IP services which provides best effort delivery mechanism.UDP is
used when acknowledgement of data does not hold any significance.
UDP is good protocol for data flowing in one direction.
UDP is simple and suitable for query based communications.
UDP is not connection oriented.
UDP does not provide congestion control mechanism.
UDP does not guarantee ordered delivery of data.
UDP is stateless.
UDP is suitable protocol for streaming applications such as VoIP, multimedia streaming.
9
PROGRAM:
10
Output :
UDP Performance
ALGORITHM :
PROGRAM:
11
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
set udp1 [new Agent/UDP]
$ns attach-agent $n3 $udp1
$udp1 set class_ 0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
set null1 [new Agent/Null]
$ns attach-agent $n1 $null1
OUTPUT:
12
RESULT :
AIM:
The cyclic redundancy check, or CRC, is a technique for detecting errors in digital data, but not for
making corrections when errors are detected. It is used primarily in data transmission.
In the CRC method, a certain number of check bits, often called a checksum, are appended to the
message being transmitted. The receiver can determine whether or not the check bits agree with the data, to
ascertain with a certain degree of probability whether or not an error occurred in transmission.
CRC involves binary division of the data bits being sent by a predetermined divisor agreed upon by
the communicating system. The divisor is generated using polynomials. So, CRC is also called polynomial
code checksum.
13
CRC uses Generator Polynomial which is available on both sender and receiver side. An example
generator polynomial is of the form like x3 + x + 1. This generator polynomial represents key 1011. Another
example is x2 + 1 that represents key 101.
Sender Side (Generation of Encoded Data from Data and Generator Polynomial (or Key)):
The binary data is first augmented by adding k-1 zeros in the end of the data
Use modulo-2 binary division to divide binary data by the key and store remainder of division.
Append the remainder at the end of the data to form the encoded data and send the same
Perform modulo-2 division again and if remainder is 0, then there are no errors.
Modulo 2 Division:
The process of modulo-2 binary division is the same as the familiar division process we use for
decimal numbers. Just that instead of subtraction, we use XOR here.
In each step, a copy of the divisor (or data) is XOR with the k bits of the dividend (or key).
The result of the XOR operation (remainder) is (n-1) bits, which is used for the next step after 1 extra
bit is pulled down to make it n bits long.
When there are no bits left to pull down, we have a result. The (n-1)-bit remainder which is appended
at the sender side.
ALGORITHM:
PROGRAM:
import java.io.*;
class crc_gen
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[]data;
int[] div;
int[] divisor;
14
int[]rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
System.out.print("Data bits are : ");
for(int i=0; i<data_bits; i++)
System.out.print(data[i]);
System.out.println();
System.out.print("divisor bits are : ");
for(int i=0; i< divisor_bits; i++)
System.out.print(divisor[i]);
System.out.println();
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("Dividend (after appending 0's) are : ");
for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++)
{
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
for(int i=0;i<div.length;i++)
{
crc[i]=(div[i]^rem[i]);
}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
15
System.out.print("crc bits are : ");
for(int i=0; i< crc.length; i++)
System.out.print(crc[i]);
System.out.println();
for(int j=0; j<crc.length; j++)
{
rem[j] = crc[j];
}
rem=divide(crc, divisor, rem);
for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)
{
System.out.println("Error");
break;
}
if(i==rem.length-1)
System.out.println("No Error");
}
System.out.println("THANKYOU. )");
}
static int[] divide(int div[],int divisor[], int rem[])
{
int cur=0; while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);
while(rem[cur]==0 && cur!=rem.length-1)
cur++;
if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}
OUTPUT :
RESULT:
Thus the above program for error checking code using was executed successfully.
AIM:
To write a ns2 program for implementing unicast
routing protocol.
When a device has multiple paths to reach a destination, it always selects one path by preferring it
over others. This selection process is termed as Routing. Routing is done by special network
devices called routers or it can be done by means of software processes.
The software based routers have limited functionality and limited scope. A router is always
17
configured with some default route. A default route tells the router where to forward a packet if
there is no route found for specific destination.
In case there are multiple path existing to reach the same destination, router can make decision
based on the following information. Routes can be statically configured or dynamically learnt. One
route can be configured to be preferred over others. Most of the traffic on the internet and intranets
known as unicast data or unicast traffic is sent with specified destination. Routing unicast data over
the internet is called unicast routing.
It is the simplest form of routing because the destination is already known. Hence the router just
has to look up the routing table and forward the packet to next hop.
Multicasting in computer network is a group communication, where a sender(s) send data to
multiple receivers simultaneously. It supports one – to – many and many – to – many data
transmission across LANs or WANs. Through the process of multicasting, the communication and
processing overhead of sending the same data packet or data frame in minimized.
Multicast IP Routing protocols are used to distribute data (for example, audio/video streaming
broadcasts) to multiple recipients. Using multicast, a source can send a single copy of data to a
single multicast address, which is then distributed to an entire group of recipients.
The key difference between broadcast and multicast is that in the broadcast the packet is delivered
to all the host connected to the network whereas, in multicast packet is delivered to
intended recipients only.
Multicast Message. Multicasting identifies logical groups of computers. A single message can
then be sent to the group. Multicast Message. Multicasting uses the Internet Group
Management Protocol (IGMP) to identify groups and group members.
ALGORITHM:
PROGRAM:
18
set ns [new Simulator]
proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec nam out.nam &
exit 3
}
# Next line should be commented out to have the static routing
$ns rtproto DV
#Create six 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]
19
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
OUTPUT:
20
RESULT:
Thus the above NS2 program for unicast routing protocol was executed successfully.
B.MULTICASTING ROUTINGPROTOCOL
AIM:
To write a ns2 program for implementing multicasting routing protocol.
ALGORITHM:
1. Start theprogram.
2. Declare the global variables ns for creating a newsimulator.
3. Set the color forpackets.
4. Open the network animator file in the name of file2 in the writemode.
5. Open the trace file in the name of file 1 in the writemode.
21
6. Set the multicast routing protocol to transfer the packets innetwork.
7. Create the multicast capable no ofnodes.
8. Create the duplex-link between the nodes including the delay time,bandwidthand dropping
queuemechanism.
9. Give the position for the links between thenodes.
10. Set a udp connection for sourcenode.
11. Set the destination node ,port and random false for the source and destinationfiles.
12. Setup a traffic generator CBR for the source and destinationfiles.
13. Down the connection between any nodes at a particulartime.
14. Create the receive agent for joining and leaving if the nodes in thegroup.
15. Define the finishprocedure.
16. In the definition of the finish procedure declare the globalvariables.
17. Close the trace file and namefile and execute the network animationfile.
18. At the particular time call the finishprocedure.
19. Stop theprogram.
PROGRAM:
# Create scheduler
#Create an event scheduler wit multicast turned on
#Turn on Tracing
set tf [open output.tr w]
$ns trace-all $tf
# 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]
set n7 [$ns node]
# Create links
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link $n3 $n4 1.5Mb 10ms DropTail
$ns duplex-link $n3 $n7 1.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 1.5Mb 10ms DropTail
$ns duplex-link $n4 $n6 1.5Mb 10ms DropTail
# Create receiver
set rcvr1 [new Agent/Null]
$ns attach-agent $n5 $rcvr1
$ns at 1.0 "$n5 join-group $rcvr1 $group1"
set rcvr2 [new Agent/Null]
$ns attach-agent $n6 $rcvr2
$ns at 1.5 "$n6 join-group $rcvr2 $group1"
set rcvr3 [new Agent/Null]
$ns attach-agent $n7 $rcvr3
$ns at 2.0 "$n7 join-group $rcvr3 $group1"
set rcvr4 [new Agent/Null]
$ns attach-agent $n5 $rcvr1
$ns at 2.5 "$n5 join-group $rcvr4 $group2"
set rcvr5 [new Agent/Null]
$ns attach-agent $n6 $rcvr2
$ns at 3.0 "$n6 join-group $rcvr5 $group2"
set rcvr6 [new Agent/Null]
$ns attach-agent $n7 $rcvr3
$ns at 3.5 "$n7 join-group $rcvr6 $group2"
$ns at 4.0 "$n5 leave-group $rcvr1 $group1"
$ns at 4.5 "$n6 leave-group $rcvr2 $group1"
$ns at 5.0 "$n7 leave-group $rcvr3 $group1"
$ns at 5.5 "$n5 leave-group $rcvr4 $group2"
$ns at 6.0 "$n6 leave-group $rcvr5 $group2"
$ns at 6.5 "$n7 leave-group $rcvr6 $group2"
# Schedule events
$ns at 0.5 "$cbr1 start"
$ns at 9.5 "$cbr1 stop"
$ns at 0.5 "$cbr2 start"
$ns at 9.5 "$cbr2 stop"
23
#post-processing
$ns at 10.0 "finish"
proc finish {} {
global ns tf
$ns flush-trace
close $tf
exec nam mcast.nam &
exit 0
}
# For nam
#Colors for packets from two mcast groups
$ns color 10 red
$ns color 11 green
$ns color 30 purple
$ns color 31 green
# Group 0source
#$udp0 set fid_10
$n0 color red
$n0 label "Source 1"
# Group 1source
#$udp1 set fid_11
$n1 color green
$n1 label "Source 2"
$n5 label "Receiver 1"
$n5 color blue
$n6 label "Receiver 2"
$n6 color blue
$n7 label "Receiver 3"
$n7 color blue
# Animation rate
$ns set-animation-rate 3.0ms
$ns run
OUTPUT :
24
25
RESULT:
Thus the above NS2 program for multicast routing protocol was executed successfully
26
Ex.No:11 Perform a case study about the different routing algorithms to select the network path with its optimum and
economical during data transfer.
a) Link State routing
b) Flooding
c) Distance vector
AIM :
To write a ns2 program for implementing link state routing protocol.
ALGORITHM:
PROGRAM :
proc finish {} {
global nf ns tr
$ns flush-trace
close $tr
exec nam links.nam &
exit 0
}
#$ns rtproto LS
$ns run
OUTPUT :
28
RESULT :
Thus the above NS2 program for link state routing protocol was executed successfully.
29
B) Flooding
AIM :
To write a ns2 program for implementing flooding protocol.
ALGORITHMS :
1.The Flooding algorithm starts with a source node broadcasting a packet to all its neighbours within its broadcast range.
2.The neighbouring nodes that are receiving a broadcast message simply rebroadcast the packet exactly one time.
3.If the same packet is received again by a node, then simply it will drop the packet without rebroadcasting it again.
4.This process continues until the message reaches all the nodes in the network.
PROGRAM :
set MESSAGE_PORT 42
set BROADCAST_ADDR -1
set NamAnimationSpeed 250u ;#in Micro Seconds
set MessageSize 100 ;# in bytes Max 1500
set NodeVeocity 20 ; # meters per second
set val(chan) Channel/WirelessChannel ;#Channel Type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/Simple
set bandwidth_ 1Mb
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(rp) DumbAgent
$ns run
OUTPUT :
RESULT :
Thus the above NS2 program for flooding protocol was executed successfully.
31
C) Distance vector
AIM :
To write a ns2 program for implementing distance vector routing protocol.
ALGORITHM:
32
# Specify link characterestics
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
$ns duplex-link $n5 $n6 1Mb 10ms DropTail
$ns duplex-link $n6 $n7 1Mb 10ms DropTail
$ns duplex-link $n7 $n8 1Mb 10ms DropTail
$ns duplex-link $n8 $n1 1Mb 10ms DropTail
# specify layout as a octagon
$ns duplex-link-op $n1 $n2 orient left-up
$ns duplex-link-op $n2 $n3 orient up
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n4 $n5 orient right
$ns duplex-link-op $n5 $n6 orient right-down
$ns duplex-link-op $n6 $n7 orient down
$ns duplex-link-op $n7 $n8 orient left-down
$ns duplex-link-op $n8 $n1 orient left
#Create a UDP agent and attach it to node n1
set udp0 [new Agent/UDP]
$ns attach-agent $n1 $udp0
#Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n4
set null0 [new Agent/Null]
$ns attach-agent $n4 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent and the network dynamics
$ns at 0.0 "$n1 label Source"
$ns at 0.0 "$n4 label Destination"
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n3 $n4
$ns rtmodel-at 2.0 up $n3 $n4
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
OUTPUT:
33
34
RESULT :
Thus the above NS2 program for distance vector routing protocol was executed successfully.
35