Nodes creation in NS2


Add the following code to the new file and named it with the extension of tcl. i.e. filename.tcl . To create the file go to the terminal.
Write
gedit filename.tcl

This above command will open a file with the name filename.tcl. Add the following code to the file and save the file.

Now, in order  to run the file go to the Terminal and write the following command.

ns filename.tcl



Code:


#Create a simulator object
set ns [new Simulator]


#Open the NAM trace file

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

# 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 n7 [$ns node]
set n8 [$ns node]
set n10 [$ns node]


#Create links between the nodes

$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms RED
$ns duplex-link $n2 $n3 1.7Mb 20ms RED
$ns duplex-link $n4 $n5 2Mb 10ms RED
$ns duplex-link $n2 $n8 2Mb 10ms RED
$ns duplex-link $n4 $n10 2Mb 5ms DropTail

#Define a 'finish' procedure

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



#Call the finish procedure

$ns at 1.0 "finish"

#Run the simulation

$ns run


The following output will be generated after running the code.
Type the following in terminal.

ns filename.tcl




Reactions:

19 comments:

  1. please help us in executing this program in cygwin window .. actually i am not aware of the correct command instruction

    ReplyDelete
    Replies
    1. sorry for late reply. though there are ways available to execute it using cygwin, but they are not reliable, i wouldnt recommend using those, ways. There are easier ways to install ns2 on linux based environment just go through the following tutorials.

      http://cs-study.blogspot.com/2013/09/ubuntu-installation-using-windows.html

      http://cs-study.blogspot.com/2012/12/ns2-installation-and-ubuntu.html

      Delete
  2. hi sir,

    can i have the tcl syntax to know the node position and energy...,

    for ex: position of node 1 is: x:____ y:_____
    energy of node 1 is: ___________
    .
    .
    .
    .

    for all or for the required nodes...,

    ReplyDelete
  3. sir ha bht talented...par install abitak kch ni kia

    ReplyDelete
  4. sir ke apke knowledge ne to anni dal di ha mjhe bht pasnd aye han apke notes

    ReplyDelete
    Replies
    1. sir ha bht payare..>3

      Delete

  5. sir a msg is displayed while executing the progam
    can't read "val(x)": no such variable
    while executing
    "$ns namtrace-all-wireless $namtrace $val(x) $val(y)"
    (file "wireless1.tcl" line 15)
    can help me.....

    ReplyDelete
  6. did you have any experience on MANETs? I need code for this network. I'm working on wormhole attacks on MANETs. can you give me some suggestion how to do it? if please send email to maka.enzo@gmail.com

    ReplyDelete
  7. I tried installing TKN's 802.11e EDCA on ns 2.35(ubuntu 13.04)...followed all the steps in read me file..but on executing the example file getting error
    "invalid command name "Mac/802_11e".

    Please help.

    ReplyDelete
  8. i need code for this network.am working on blackhole attacks on manet .so can u give some suggestion? please

    ReplyDelete
  9. hello everyone
    please someone tell me how to change traffic like frst i have find results using CBR traffic now i want to use PARETO bt i dnt knw how to change traffic and where, if someone knws then please help me ..........thnx

    ReplyDelete
  10. any one can help me the ns2 source code or tcl,awk and xgraph for dynamic redundancy management of multipath routing using heterogenous wireless sensor networks.
    mail id:sweetrathicse@gmail.com

    ReplyDelete
  11. Sir plz help in creating a wormhole code for the network in ns2

    ReplyDelete
  12. Sir plz help in creating a wormhole code for the network in ns2

    ReplyDelete
  13. how to make a node execute some command and send the result to other node..? can someone help me please.. It'll be very helpful

    ReplyDelete
  14. why I got an error on this ? help me please ! -_-

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

    ReplyDelete
  15. ns: finish: couldn't execute "nam": no such file or directory
    while executing
    "exec nam o.nam &"
    (procedure "finish" line 7)
    invoked from within
    "finish"



    ^^ anyone can help me on this error ??

    ReplyDelete
  16. sir please send me tcl file of wormhole attack in my email neerajsharma3027@gmail.com

    ReplyDelete