Ns2 Tutorial
Ns2 Tutorial
Ns2 Tutorial
NS2 Tutorial
Tutorials home
Here we are providing a 'providing a 'providing a 'providing a 'template' that we can use it for all tcl scripts.
We will introduce basics of how to create event scheduler, create a simple pair of nodes and make communication
between the two nodes. Then we will introduce a standard template for all tcl scripts with simple examples.
In this page we are going to introduce the concepts of:
Part of introduction to TCP/IP model
Where can you write scripts?
Basic ns2 programming steps
Template
Golden rules
Simple examples
CBR over UDP
CBR over TCP
FTP over TCP
CBR,FTP,telnet..
Transport layer
TCP,UDP
Network layer
rtproto
Top
Transmission mode
Data link Layer
Simplex mode
Duplex mode
Physical layer
TCP/IP model
Application Layer
Applications agents are Telnet, FTP, Rlogin, CBR
Transport Layer
The transport uses two protocols, UDP and TCP. UDP which stands for User Datagram Protocol does not
guarantee packet delivery and applications which use this must provide their own means of verifying delivery.
TCP does guarantee delivery of packets to the applications which use it.
Network Layer
The network layer is concerned with packet routing and used low level protocols such as ICMP, IP, and
IGMP. Here Network protocols are omitted for simple examples
Link Layer
The link layer is concerned with the actual transmittal of packets as well as IP to Ethernet address
translation. Data transfer in two modes i.e. simplex mode and in duplex mode.
All the links are created at data link layer. Network layer next is to data link layer.
Top
1/10
10/15/2014
Ns2 Tutorial
2. save file
Press esc-> colon (shift + semicolon) ->wq (save and quit)
It save the file
3. To run tcl script
ns filename.tcl
Top
Template
Top
Creating link
2/10
10/15/2014
Ns2 Tutorial
Golden rules
Top
Follow the template tcl like Bible.
All communication between 2 agents.
Upper layer to lower layer, we attach agents.
Same layer in two nodes, we connect agents.
Agents are tcp, udp, telnet, cbretc
UDP communication
In UDP communication, data is flows from UDP agent to Null agent.
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basic
3/10
10/15/2014
Ns2 Tutorial
TCP Communication
In TCP communication, data is flows from TCP agent to TCPsink agent.
Simple Examples
Top
So far we are not talking about data flow. Here we will explain with CBR protocol.
Example1:( CBR over UDP)
set ns [new Simulator]
proc finish {}
{
global ns tracefile nf
$ns flush-trace
close $nf
close $tracefile
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basic
'finish' procedure
4/10
10/15/2014
Ns2 Tutorial
Scheduling Events
- $ns at 1.0 start
and at 3.0 finish
$ns run
Result
Before 1.0ms
After 1.0ms
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basic
5/10
10/15/2014
Ns2 Tutorial
Top
proc finish {}
{
global ns tracefile nf
$ns flush-trace
close $nf
close $tracefile
exec nam out.nam &
exit 0
}
'finish' procedure
Scheduling Events
- $ns at 1.0 start
and at 3.0 finish
6/10
10/15/2014
Ns2 Tutorial
$ns run
Result:
Before 1.0ms
After 1.0ms
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basic
7/10
10/15/2014
Ns2 Tutorial
Top
proc finish {}
{
global ns tracefile nf
$ns flush-trace
close $nf
close $tracefile
exec nam out.nam &
exit 0
}
'finish' procedure
Scheduling Events
- $ns at 1.0 start
and at 3.0 finish
$ns run
Result:
Here we are using FTP Application agent as a traffic generator instead of CBR. The difference is CBR traffic
generator will produce constant bit rate where as FTP traffic generator produces maximum available bit rate.
We are writing code
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basic
8/10
10/15/2014
Ns2 Tutorial
After 1.0ms
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basic
9/10
10/15/2014
Ns2 Tutorial
Top
Tutorials home
This text is provided as its basic only, with the intention of providing novices with a quick glance on the topic concerned. The reader is advised to visit
official tutorials available elsewhere for up to date advance topic.
We have made efforts to keep this page W3C XHTML Compliant.
Last Modified: 11th November 2007
Copyright (c) 2007 Murali.P, Ravindra Kr. Jangir, Manasakrishna Reddy. T. All Rights Reserved.
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basic
10/10