The output of the above code is as follows. Run the program by typing ‘ns trafficflow.tcl’ in terminal.
After it gets executed, the following output is displayed in nam.
Now, click on the Play button which is underlined green.
We can see the traffic flow as well as the traffic which are being dropped on node 2 because the limits of the node to hold the packets is limited. Thats why when the queue is full and there is no more capacity available, the packet drops.
Code Explained:
set ns [new Simulator]: generates an NS simulator object instance, and assigns it to variable ns.
The "Simulator" object has member functions that do the following:
- Create compound objects such as nodes and links (described later)
- Connect network component objects created (ex. attach-agent)
- Set network component parameters (mostly for compound objects)
- Create connections between agents (ex. make connection between a "tcp" and "sink")
- Specify NAM display options
Most of member functions are for simulation setup (referred to as plumbing functions in the Overview section) and scheduling, however some of them are for the NAM display. The "Simulator" object member function implementations are located in the "ns-2/tcl/lib/ns-lib.tcl" file.
- $ns color fid color: is to set color of the packets for a flow specified by the flow id (fid). This member function of "Simulator" object is for the NAM display, and has no effect on the actual simulation.
- $ns namtrace-all file-descriptor: This member function tells the simulator to record simulation traces in NAM input format. It also gives the file name that the trace will be written to later by the command $ns flush-trace. Similarly, the member function trace-all is for recording the simulation trace in a general format.
- proc finish {}: is called after this simulation is over by the command $ns at 5.0 "finish". In this function, post-simulation processes are specified.
- set n0 [$ns node]: The member function node creates a node. A node in NS is compound object made of address and port classifiers (described in a later section). Users can create a node by separately creating an address and a port classifier objects and connecting them together. However, this member function of Simulator object makes the job easier. To see how a node is created, look at the files: "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-node.tcl".
- $ns duplex-link node1 node2 bandwidth delay queue-type: creates two simplex links of specified bandwidth and delay, and connects the two specified nodes. In NS, the output queue of a node is implemented as a part of a link, therefore users should specify the queue-type when creating links. In the above simulation script, DropTail queue is used. If the reader wants to use a RED queue, simply replace the word DropTail with RED. The NS implementation of a link is shown in a later section. Like a node, a link is a compound object, and users can create its sub-objects and connect them and the nodes. Link source codes can be found in "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl" files. One thing to note is that you can insert error modules in a link component to simulate a lossy link (actually users can make and insert any network objects). Refer to the NS documentation to find out how to do this.
- $ns queue-limit node1 node2 number: This line sets the queue limit of the two simplex links that connect node1 and node2 to the number specified. At this point, the authors do not know how many of these kinds of member functions of Simulator objects are available and what they are. Please take a look at "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl", or NS documentation for more information.
- $ns duplex-link-op node1 node2 ..: The next couple of lines are used for the NAM display. To see the effects of these lines, users can comment these lines out and try the simulation.
Now that the basic network setup is done, the next thing to do is to setup traffic agents such as TCP and UDP, traffic sources such as FTP and CBR, and attach them to nodes and agents respectively.
- set tcp [new Agent/TCP]: This line shows how to create a TCP agent. But in general, users can create any agent or traffic sources in this way. Agents and traffic sources are in fact basic objects (not compound objects), mostly implemented in C++ and linked to OTcl. Therefore, there are no specific Simulator object member functions that create these object instances. To create agents or traffic sources, a user should know the class names these objects (Agent/TCP, Agnet/TCPSink, Application/FTP and so on). This information can be found in the NS documentation or partly in this documentation. But one shortcut is to look at the "ns-2/tcl/libs/ns-default.tcl" file. This file contains the default configurable parameter value settings for available network objects. Therefore, it works as a good indicator of what kind of network objects are available in NS and what are the configurable parameters.
- $ns attach-agent node agent: The attach-agent member function attaches an agent object created to a node object. Actually, what this function does is call the attach member function of specified node, which attaches the given agent to itself. Therefore, a user can do the same thing by, for example, $n0 attach $tcp. Similarly, each agent object has a member function attach-agent that attaches a traffic source object to itself.
- $ns connect agent1 agent2: After two agents that will communicate with each other are created, the next thing is to establish a logical network connection between them. This line establishes a network connection by setting the destination address to each others' network and port address pair.
Assuming that all the network configuration is done, the next thing to do is write a simulation scenario (i.e. simulation scheduling). The Simulator object has many scheduling member functions. However, the one that is mostly used is the following:
- $ns at time "string": This member function of a Simulator object makes the scheduler (scheduler_ is the variable that points the scheduler object created by [new Scheduler] command at the beginning of the script) to schedule the execution of the specified string at given simulation time. For example, $ns at 0.1 "$cbr start" will make the scheduler call a start member function of the CBR traffic source object, which starts the CBR to transmit data. In NS, usually a traffic source does not transmit actual data, but it notifies the underlying agent that it has some amount of data to transmit, and the agent, just knowing how much of the data to transfer, creates packets and sends them.
After all network configuration, scheduling and post-simulation procedure specifications are done, the only thing left is to run the simulation. This is done by $ns run.
Hi...
ReplyDeleteHow to create color data in wireless...?
thanks
Hi there,
DeleteI didnt understand the question, if you are talking about colors of traffic flow , then it is already in color and if you want to change the color of traffic flow you can always do that by changing colors in the following portion of code.
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
$ns color 3 Black
Hope that helps.
how to give different color to different traffic flow for wireless scenario. in above example you have created links. means it is example of wired scenario. he is asking for wireless
Deletehi usman, thanks for this example. In this example you created wired network. i want to create wireless network and in that i want apply different color for traffic flow between different link
ReplyDeletefor example
nodes connected in wsn (n1,n2,n3,n4)
i want to make the traffic in between n1 to n2 in green color
as well as i want to make the traffic in between n3 to n4 in blue color
please help me to get this
Hi...
ReplyDeleteThis is natarajan. M. How to get the mobile node current position signal frequency from base station antenn in ns2..
anybody help for this problem.
what is the use of changing shape of the nodes in ns2?
ReplyDeletecan you explain how can i display path of packets flow in simulator
ReplyDeletei.e if packets flowing from 1->2->3 i want to display some text on simulator that @1-> node id-1 , @2-> node id -2 route- 1->2 ,@3-> node id-3 route->1->2->3 like that
please its very necessary for my project I am working with aodv protocol
if possible please explain me how to broadcast the packet in same protocol
thanks®ards.....
can you explain how can i display path of packets flow in simulator
ReplyDeletei.e if packets flowing from 1->2->3 i want to display some text on simulator that @1-> node id-1 , @2-> node id -2 route- 1->2 ,@3-> node id-3 route->1->2->3 like that
please its very necessary for my project I am working with aodv protocol
if possible please explain me how to broadcast the packet in same protocol
thanks®ards.....
Your website is really cool and this is a great inspiring article.
ReplyDeletegclub casino
gclub
goldenslot
Can anyone please help me in implementation of 3 application layer protocols for tcp in ns2..
ReplyDeleteHi
ReplyDeleteThank you for helping me
how can i get trace file after running NS2 ?
Thank you
nice explanation and well-written post. .. your blog help me alot to learn NS2
ReplyDeleteDo you want to see your website or blog on Google's first page ??? And want to beat your website or blog ranking ?? Your website and blog will be displayed on the first page of Google through the SEO service, completely and beautifully. And get more visitors.
ReplyDeleteClick this for more details :""SEO services provider""
Very useful article buddy keep it up I came to know a website that are having good motivational contents on sandbag workouts and other motivational contents
ReplyDeletei m working for amelioration dymo protocol in WSN where can i found total maximum interface queue size of node in source code of ns2
ReplyDeleteThank you nice blog
ReplyDeleteTcl Service Center in Hyderabad Contact No.+91-9393538580, 040-66833000. Electronicservicecenter.in provides Reliable Doorstep Repair Services. 100% Customer Satisfaction and Quality Services.