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

CN Print

Uploaded by

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

CN Print

Uploaded by

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

Ex.

No:7(A) Study of Network simulator (NS)

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

PRE LAB DISCUSSION:

NET WORK SIMULATOR (NS2)


Ns Overview

NsStatus

Periodical release (ns-2.26, Feb2003)

Platformsupport

FreeBSD, Linux, Solaris, Windows andMac
Ns functionalities
Routing, Transportation, Traffic sources,Queuing disciplines, QoS
Congestion Control Algorithms
 Slow start
 Additive increase/multiplicativedecrease
 Fast retransmit and Fastrecovery
Case Study: A simple Wireless network.
Ad hoc routing, mobile IP, sensor-MAC Tracing,
visualization and various utilitie NS(Network
Simulators)
Most of the commercial simulators are GUI driven, while some network simulators are CLI driven. The
network model / configuration describes the state of the network (nodes,routers, switches, links) and the events (data
transmissions, packet error etc.). An important output of simulations are the trace files. Trace files log every packet,
every event that occurred in the simulation and are used for analysis. Network simulators can also provide other
tools to facilitate visual analysis of trends and potential trouble spots.
Most network simulators use discrete event simulation, in which a list of pending "events" is stored, and
those events are processed in order, with some events triggering future events—such as the event of the arrival of a
packet at one node triggering the event of the arrival of that packet at a downstreamnode.
Simulation of networks is a very complex task. For example, if congestion is high, then estimation of the
average occupancy is challenging because of high variance. To estimate the likelihood of a buffer overflow in a
network, the time required for an accurate answer can be extremely large. Specialized techniques such as "control
variates" and "importance sampling" have been developed to speed simulation.

Examples of network simulators


There are many both free/open-source and proprietary network simulators. Examples of notable network
simulation software are, ordered after how often they are mentioned in research papers:
1. ns (open source)
2. OPNET (proprietary software)
3. NetSim (proprietary software)
Uses of network simulators
Network simulators serve a variety of needs. Compared to the cost and time involved in setting up an entire
test bed containing multiple networked computers, routers and data links, network simulators are relatively fast and
inexpensive. They allow engineers, researchers to test scenarios that might be particularly difficult or expensive to

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.

Congestion control Algorithms


Slow-start is used in conjunction with other algorithms to avoid sending more data than the network is capable
of transmitting, that is, to avoid causing network congestion. The additive increase/multiplicative decrease (AIMD)
algorithm is a feedback control algorithm. AIMD combines linear growth of the congestion window with an
exponential reduction when a congestion takes place. Multiple flows using AIMD congestion control will eventually
converge to use equal amounts of a contended link. Fast Retransmit is an enhancement to TCP that reduces the time
a sender waits before retransmitting a lost segment.

Program:

Sample.tcl

#Create a simulator object


set ns [new Simulator]

#Define different colors for data flows (for NAM)


$ns color 1 Blue
$ns color 2 Red

#Open the NAM trace file


set nf [open output.nam w]
$ns namtrace-all $nf

#open the trace file


set f [open sample.tr w]
$ns trace-all $f

#Define a 'finish' procedure


proc finish {} {
global ns nf f
5
$ns flush-trace
#Close the NAM trace file
close $nf
close $f
#Execute NAM on the trace file
exec nam output.nam &
exit 0
}

#Create four nodes


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create links between the nodes


$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail

#Set Queue Size of link (n2-n3) to 10


$ns queue-limit $n2 $n3 10

#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

#Monitor the queue for link (n2-n3). (for NAM)


$ns duplex-link-op $n2 $n3 queuePos 0.5

#Setup a TCP connection


set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

#Setup a FTP over TCP connection


set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

#Setup a UDP connection


set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2

#Setup a CBR over UDP connection (1000 bytes every 10ms)


set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

6
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set interval_ 0.010
$cbr set random_ false

#Schedule events for the CBR and FTP agents


$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"

#Detach tcp and sink agents (not really necessary)


$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"

#Print CBR packet size and interval


puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"

#Run the simulation


$ns run

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:

To simulate the performance of TCP/UDP using NS2.

PRE LAB DISCUSSION:

 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.

TCP Performance Algorithm :

1. Create a Simulator object.


2. Set routing as dynamic.
3. Open the trace and nam trace files.
4. Define the finish procedure.
5. Create nodes and the links between them.
6. Create the agents and attach them to the nodes.
7. Create the applications and attach them to the tcp agent.
8. Connect tcp and tcp sink.
9. Run the simulation.

9
PROGRAM:

set ns [new Simulator]


$ns color 0 Blue
$ns color 1 Red
$ns color 2 Yellow
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set f [open tcpout.tr w]
$ns trace-all $f
set nf [open tcpout.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$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 tcp [new Agent/TCP]
$tcp set class_ 1
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"
$ns at 1.35 "$ns detach-agent $n1 $tcp ; $ns detach-agent $n3 $sink"
$ns at 3.0 "finish"
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
puts "Running nam.."
#exec xgraph tcpout.tr -geometry 600x800 &
exec nam tcpout.nam &
exit 0
}
$ns run

10
Output :

UDP Performance

ALGORITHM :

1. Create a Simulator object.


2. Set routing as dynamic.
3. Open the trace and nam trace files.
4. Define the finish procedure.
5. Create nodes and the links between them.
6. Create the agents and attach them to the nodes.
7. Create the applications and attach them to the UDP agent.
8. Connect udp and null agents.
9. Run the simulation.

PROGRAM:

set ns [new Simulator]


$ns color 0 Blue
$ns color 1 Red
$ns color 2 Yellow

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set f [open udpout.tr w]
$ns trace-all $f
set nf [open udpout.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail

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

$ns connect $udp0 $null0


$ns connect $udp1 $null1
$ns at 1.0 "$cbr0 start"
$ns at 1.1 "$cbr1 start"
puts [$cbr0 set packetSize_]
puts [$cbr0 set interval_]
$ns at 3.0 "finish"
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
puts "Running nam.."
exec nam udpout.nam & exit 0
}
$ns run

OUTPUT:

12
RESULT :

Thus the study of TCP/UDP performance is done successfully.

Ex.No:9 Simulation of error correction code (like CRC)

AIM:

To implement error checking code using java.

PRE LAB DISSCUSSION:

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

Receiver Side (Check if there are errors introduced in transmission)

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:

1. Start the Program


2. Given a bit string, append 0S to the end of it (the number of 0s is the same as the degree of the generator
polynomial) let B(x) be the polynomial corresponding to B.
3. Divide B(x) by some agreed on polynomial G(x) (generator polynomial) and determine the remainder R(x).
This division is to be done using Modulo 2Division.
4. Define T(x) = B(x)–R(x)
5. (T(x)/G(x) => remainder0)
6. Transmit T, the bit string corresponding to T(x).
7. Let T’ represent the bit stream the receiver gets and T’(x) the associated polynomial. The receiver divides
T1(x) by G(x). If there is a 0 remainder, the receiver concludes T = T’ and no error occurred otherwise, the
receiver concludes an error occurred and requires a retransmission
8. Stop the Program

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 :

Enter number of data bits :


7
Enter data bits :
1
0
1
1
16
0
0
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
0
1
Dividend (after appending 0's) are : 101100100 CRC
code :
101100111
Enter CRC code of 9 bits : 1
0
1
1
0
0
1
0
1
crc bits are : 101100101
Error
THANKYOU.............)
BUILD SUCCESSFUL (total time: 1 minute 34 seconds)

RESULT:

Thus the above program for error checking code using was executed successfully.

Ex.No:10 Performance Evaluation of Routing protocols using Simulation tool.

(a) UNICAST ROUTINGPROTOCOL

AIM:
To write a ns2 program for implementing unicast
routing protocol.

PRE LAB DISCUSSION:

 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:

1. Start the program.


2. Declare the global variables ns for creating a new simulator.
3. Set the color for packets.
4. Open the network animator file in the name of file2 in the write mode.
5. Open the trace file in the name of file 1 in the write mode.
6. Set the unicast routing protocol to transfer the packets in network.
7. Create the required no of nodes.
8. Create the duplex-link between the nodes including the delay time, bandwidth and dropping
queue mechanism.
9. Give the position for the links between the nodes.
10. Set a tcp reno connection for source node.
11. Set the destination node using tcp sink.
12. Setup a ftp connection over the tcp connection.
13. Down the connection between any nodes at a particular time.
14. Reconnect the downed connection at a particular time.
15. Define the finish procedure.
16. In the definition of the finish procedure declare the global variables ns, file1, andfile2.
17. Close the trace file and name file and execute the network animation file.
18. At the particular time call the finish procedure.
19. Stop the program.

PROGRAM:

18
set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue


$ns color 2 Red

#Open the Trace file


set file1 [open out.tr w]
$ns trace-all $file1

#Open the NAM trace file


set file2 [open out.nam w]
$ns namtrace-all $file2

#Define a 'finish' procedure

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]

#Create links between the nodes


$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail

#Give node position (for NAM)


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

#Setup a TCP connection


set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

19
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

$ns rtmodel-at 1.0 down $n1 $n4


$ns rtmodel-at 4.5 up $n1 $n4
$ns at 0.1 "$ftp start"
$ns at 6.0 "finish"
$ns run

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

set ns [new Simulator -multicast on]

#Turn on Tracing
set tf [open output.tr w]
$ns trace-all $tf

# Turn on nam Tracing


set fd [open mcast.nam w]
$ns namtrace-all $fd

# 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

# Routing protocol: say distance vector


22
#Protocols: CtrMcast, DM, ST, BST
set mproto DM
set mrthandle [$ns mrtproto $mproto {}]

# Allocate group addresses


set group1 [Node allocaddr]
set group2 [Node allocaddr]

# UDP Transport agent for the traffic source


set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
$udp0 set dst_addr_ $group1
$udp0 set dst_port_ 0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp0

# Transport agent for the trafficsource


set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
$udp1 set dst_addr_ $group2
$udp1 set dst_port_ 0
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp1

# 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

# Manual layout: order of the link is significant!


#$ns duplex-link-op $n0 $n1 orient right
#$ns duplex-link-op $n0 $n2 orient right-up
#$ns duplex-link-op $n0 $n3 orient right-down
# Show queue on simplex link n0->n1
#$ns duplex-link-op $n2 $n3 queuePos 0.5

# 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

#$n2 add-mark m0 red


#$n2 delete-mark m0"

# 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

a) Link State routing

AIM :
To write a ns2 program for implementing link state routing protocol.

ALGORITHM:

1. Define new simualtor


2. Define different colors for data flows (for NAM)
3. Define a new Trace file and open it
4. Define a new NAM Trace file and open it
5. Define a 'finish' procedure - to flush trace record in the `trace and trace output files.
6. Define the routing protocol as Link State (LS)
7. Create six nodes - n0,n1,..n5
8. Create links between the nodes with 0.3Mb and 10 ms Link with DropTail option
9. Give node position (for NAM) to place six nodes in the layout
10. Setup a TCP connection - attach TCP Source Agent to node n0 and TCP sink agent to node n5
11. Setup a FTP over TCP connection
12. Define configuration such that link between nodes n1 and n4 to be failed at 1.0 interval, and up again at 4.5 interval
13. Start the simulation

PROGRAM :

set ns [new Simulator]


set nf [open links.nam w]
$ns namtrace-all $nf

set tr [open trace.tr w]


$ns trace-all $tr

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

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

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


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

$ns duplex-link-op $n0 $n1 orient right-down


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

set tcp [new Agent/TCP]


$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]


$ftp attach-agent $tcp

set sink [new Agent/TCPSink]


$ns attach-agent $n3 $sink

set udp [new Agent/UDP]


$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]


$cbr attach-agent $udp

set null [new Agent/Null]


$ns attach-agent $n3 $null

$ns connect $tcp $sink


$ns connect $udp $null

$ns rtmodel-at 1.0 down $n1 $n3


$ns rtmodel-at 2.0 up $n1 $n3

#$ns rtproto LS

$ns at 0.0 "$ftp start"


$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

$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

set ns [new Simulator]


set f [open FloodingAlgorithm.tr w]
$ns trace-all $f
set nf [open FloodingAlgorithm.nam w]
$ns namtrace-all-wireless $nf $val(x) $val(y)
$ns use-newtrace
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $num_nodes
set chan_1_ [new $val(chan)]

$ns node-config -adhocRouting $val(rp) \


-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace OFF \
30
-channel $chan_1_
# create a bunch of nodes
for {set i 0} {$i < $num_nodes} {incr i} {
set n($i) [$ns node]
$n($i) color “black”
$n($i) set Y_ [expr 150*floor($i/$group_size) + 100*(($i%$group_size)>=($group_size/2))]
$n($i) set X_ [expr (20*$group_size)*($i/$group_size%2) +100*($i%($group_size/2))]
$n($i) set Z_ 0.0
$ns initial_node_pos $n($i) 30
}
# attach a new Agent/MessagePassing/Flooding to each node on port $MESSAGE_PORT
for {set i 0} {$i < $num_nodes} {incr i} {
set a($i) [new Agent/MessagePassing/Flooding]
$n($i) attach $a($i) $MESSAGE_PORT
$a($i) set messages_seen {}
}
$ns at 5.1 “$a(1) send_message $MessageSize 1 {BroadcastMessageI} $MESSAGE_PORT”
$ns at 10.0 “finish”
proc finish {} {
global ns f nf val NamAnimationSpeed
$ns flush-trace
close $f
close $nf
exit 0
}

$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:

1. Define new simulator


2. Define different colors for data flows (for NAM)
3. Define a new Trace file and open it
4. Define a new NAM Trace file and open it
5. Define a 'finish' procedure - to flush trace record in the `trace and trace output files.
6. Define the routing protocol as Distance Vector (DV)
7. Create six nodes - n0,n1,..n5
8. Create links between the nodes with 0.3Mb and 10 ms Link with DropTail option
9. Give node position (for NAM) to place six nodes in the layout
10. Setup a TCP connection - attach TCP Source Agent to node n0 and TCP sink agent to node n5
11. Setup a FTP over TCP connection
12. Define configuration such that link between nodes n1 and n4 to be failed at 1.0 interval, and up again at 4.5 interval
13. Start the simulation
PROGRAM :

#Distance vector routing protocol – distvect.tcl


#Create a simulator object
set ns [new Simulator]
#Use distance vector routing
$ns rtproto DV
#Open the nam trace file
set nf [open DVout.nam w]
$ns namtrace-all $nf
# Open tracefile
set nt [open trace.tr w]
$ns trace-all $nt
#Define 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam -a out.nam &
exit 0
}
# Create 8 nodes
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]
set n8 [$ns node]

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

You might also like