Saturday 21 September 2013

ns working


Contd 2
1
Lol packets are moving like Bullet


So here is Question? How i did this :p. it’s really simple i made a small script in TCL language, which is basically design to stimulate UDP packet’s from node 0 to node 1 via one Link. Let me explain you..but Before check Below
Fundamental’s for writing tcl script for Network simulator-2
I have devided this codes into 6 parts with each explanation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
set ns [new Simulator]
set nf [open out.nam w]
$ns namtrace-all $nf
proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
}
$ns at 3.0 "finish"
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns run
1. we need to create a simulator object something like this.
1
set ns [new Simulator]
2.Now we need to open a file for writing that is going to be used for the nam trace data.First line open a file ‘out.nam’ for writing and give it to file handle ‘nf’
1
set nf [open out.nam w]
In this code we are telling simulator object that we created above to write all simulation data that is going to be relevant for nam into this file
1
$ns namtrace-all $nf
3.This is to add a ‘finish’ procedure that closes the trace file and start nam
1
2
3
4
5
6
7
proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
}
4.This one tell simulator object to execute the ‘finish’ procedure after 3.0 seconds of simulation time
1
$ns at 3.0 "finish"
5. This code tells the simulator object to connects the nodes n0 and n1 with duplex link with a bandwidth 1 Megabit,a delay of 10ms Drop tail (A simple queue management algorithm used by internet routers to decides when to drop packets :) )
1
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
6.This is a last line finally starts the simulation
1
$ns run
Now save the above script with .tcl extansion, and Open a terminal type
1
ns filename.tcl
You will se something like this
NOW to send a data on two nodes via one link, we need to do certaine things
1.Create a UDP agent.
2.Create a CBR traffic source.
3.Create NULL agent which act as a traffic sink
4.Connect the two agent with each other.
5.Fixe the time when to send and stop a data.
1.Create a UDP agent and attach it to node n0
1
2
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
2.Create a CBR traffic source and attach it to udp0
1
2
3
4
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetsize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
3.The next lines creates a null agent which acts as traffic sink and attach it to node n1
1
2
set null0 [new Agent/null]
$ns attach-agent $n1 $null0
4. Now we are going to connect two agents with each other
1
$ns connect $udp0 $null0
5 NOw to fix a time by telling CBR agent when to send data and when to stop sending a data by following code
1
2
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
So here is a complete code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
    #Close the trace file
        close $nf
    #Execute nam on the trace file
        exec nam out.nam &
        exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $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.05
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0 
#Schedule events for the CBR agent
$ns at 0.5 "$cbr0 start"
$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
Now save it with some name with .tcl extension,and again Open a terminal Type
1
ns filename.tcl

No comments:

Post a Comment