Sunday 13 November 2016

SENDING AND RECEIVING DATA BETWEEN NODES IN NS2

The following procedure shows how to send and receive data between nodes in NS2:

1 The first step is to communicate data between the nodes n0 and n1.Data is always from one agent to another. Thus we have to create agent objects to send and receive data .Following code explains sending data from node n0 to n1.
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

2 The next step is to attach a traffic generator to the agent .Different examples of traffic generators are ftp (file transfer protocol),cbr(constant bit rate), poisson etc.
It can be written as:
set cbr0 [new Application/Traffic/CBR ]
1      $cbr0 set packetsize_500
2      $cbr0 set interval_0.005
3      $cbr0 attach-agent $udp0
The above written code attaches a CBR traffic generator to the UDP agent.
Line 1 sets packetsize to  500 bytes
Line 2 means that a packet will be sent every 0.005 seconds
Line3 attaches agent UDP to cbr0

3 The next step is to create a null agent which acts as traffic sink and attach it to node n1.Its syntax is as:
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
4 Now the next step is to connect two agents to each other and it can be done as:
$ns connect $udp0 $null0
5 The next step is to tell the cbr agent when to send data and when to stop sending .
It can be written as:
$ns at 0.5 “$cbr start”
$ns at 4.5 “$cbr stop”



NOTE: We should put the above lines just before the line ‘$ns at 5.0 “ finish”’

No comments:

Post a Comment