*prog1.
tcl X
1 #Create Simulator
2 set ns [new Simulator]
3 #Use colorsto differentiate the traffic
4 $ns color 1 Blue
5 ^[Link] 2 Red
6#Open trace and NAM trace file
7 set ntrace [open [Link] W]
8 $ns trace-all $ntrace
9 set namfile [open [Link] w]
10 Sns namtrace-a1l Snamfile
11 #Finish Procedure
12 proc Finish O {
13 global nsntrace namfile
14 #Dump all trace data and close the file
15 Sns flush-trace
16 close $ntrace
17 close Snamfile
18 #Execute the nam animation file
19 exec nam [Link] &
20 #Find the number of ping packets dropped
21 puts"The number of ping packets dropped are
22 exec grep [Link] | cut -d f 5 | grep -c "ping" &
23 exit 0
24
25 #Create six nodes
26 for set i 0} ($i < 6} {incr i} {
27 setn($i) [^ns node]
28
29 #Connect the nodes
30 for {set j 0} ($j < 5) {incr j} {
31 Sns duplex-link ^n($j) n([expr ($j+1)]) 0. 1Mb 10ms DropTail
32 }
33 #Define the recv. function for the class "Agent/Ping
34 Agent/Ping instproc recv {from rtt} {
35 Sself instvar node
36 puts "node [^node id] received ping answer from $fron withrOUnd trip tine Srtt
37 ms
38 }
39 #Create two ping agents and attach them to n(O) and n(5)
40 set pe [new Agent/Ping]
41 $po set class 1
42 $ns attach-agent $n(0) Spo
43
43 set p1 [new Agent/Pingl
*[Link]
33 #Define the recv function for the class
'Agent/Ping'
34 Agent/Ping instproc recv {from rtt) {
35 $self instvar node
puts "node [Snode id] received ping ansWer from $from with round t
36
37 ms
38 }
them to n(0) and n(5)
39 #Create two ping agents and attach
40 set po [new Agent/Ping]
41 Spo set class 1
42 $ns attach-agent $n(0) $p0
43 set p1 [new Agent/Ping]
44 Sp1 set cLass 1
45 $nsattach-agent $n(5) $p1
46 Sns connect $po $p1 queue
47 #Set queUe size and Monitor the
observe the drop in ping packets
48 #Queue size is set to 2 to
49 Sns queuelimit $n(2) $n(3) 2 queuePos 0.5
50 $ns duplexLink-op $n(2) $n(3)
51 #Create congeststion
traffic between n(2) and n(4)
52 #Generatea Huge CBR
Agent/TCP]
53 settcpo [new 2
set class
54Stcp9attach-agent
Sns
55 Sns $n(2) $tcp0
sinko [new Agent/TCPSink]
^n(4) $sinko
57 Sns attachagent $sinko
58 Sns connect $tcp0 over TCP
59 #Apply CBR traffic
Application/Traffic/CBR
60 setcbro[new
packetsize 500
61 Scbra set
$cbro set rate 1Mb
62 Scbroattachagent
63 $tcp0
64 #SCheduleeventS
65Snsat 0.2"Spo send"
66 $nslat o4Sp1send"
67Snsat o4"Scbro start"
68 Snsat .8Sp0 send"
at1.0"Sp1 send"
69 $ns T
1.2l"$cbrostop"
70 $ns at 1.4"Sposend"
71$nsat
1.6"Sp1 send"
72 Snsat 1.8FLnish
74 $ns
73 at
#Run thesimulation
75 ^nsrunl
by Wi