NS2 Program for Using Xgraph

How to Install:
sudo apt-get install xgraph

NS2 Program Based on UDP

NS2 Program Based on TCP

You will need both source code to plot ns2 simulations in this example.

Additionally we have to use these perl scripts to extract data from output trace generated from NS2

# type: perl throughput.pl <trace file> <required node> <granlarity>   >    output file

$infile=$ARGV[0];
$tonode=$ARGV[1];
$granularity=$ARGV[2];

#we compute how many bytes were transmitted during time interval specified
#by granularity parameter in seconds
$sum=0;
$clock=0;

      open (DATA,"<$infile")
        || die "Can't open $infile $!";
 
    while (<DATA>) {
             @x = split(' ');

#column 1 is time
if ($x[1]-$clock <= $granularity)
{
#checking if the event corresponds to a reception
if ($x[0] eq 'r')
{
#checking if the destination corresponds to arg 1
if ($x[3] eq $tonode)
{
#checking if the packet type is TCP
if ($x[4] eq 'tcp')
{
    $sum=$sum+$x[5];#number of bytes in the period
}
}
}
}
else
{   $throughput=$sum/$granularity;
    print STDOUT "$x[1] $throughput\n";
    $clock=$clock+$granularity;
    $sum=0;

}#end while

   $throughput=$sum/$granularity;
    print STDOUT "$x[1] $throughput\n";
    $clock=$clock+$granularity;
    $sum=0;

    close DATA;
exit(0);



# type: perl throughput.pl <trace file> <required node> <granlarity>   >    output file

$infile=$ARGV[0];
$tonode=$ARGV[1];
$granularity=$ARGV[2];

#we compute how many bytes were transmitted during time interval specified
#by granularity parameter in seconds
$sum=0;
$clock=0;

      open (DATA,"<$infile")
        || die "Can't open $infile $!";
 
    while (<DATA>) {
             @x = split(' ');

#column 1 is time
if ($x[1]-$clock <= $granularity)
{
#checking if the event corresponds to a reception
if ($x[0] eq 'r')
{
#checking if the destination corresponds to arg 1
if ($x[3] eq $tonode)
{
#checking if the packet type is TCP
if ($x[4] eq 'cbr')
{
    $sum=$sum+$x[5];#number of bytes in the period
}
}
}
}
else
{   $throughput=$sum/$granularity;
    print STDOUT "$x[1] $throughput\n";
    $clock=$clock+$granularity;
    $sum=0;

}#end while

   $throughput=$sum/$granularity;
    print STDOUT "$x[1] $throughput\n";
    $clock=$clock+$granularity;
    $sum=0;

    close DATA;
exit(0);




We have written a shell script to do our job quickly.

#!bin/bash

perl Perltcp.pl outudp.tr 4  0.5 > tcp-udp-one.tr
perl Perlcbr.pl outudp.tr 5  0.5 > tcp-udp-two.tr

xgraph -bg white tcp-udp-one.tr tcp-udp-two.tr &

perl Perltcp.pl outtcp.tr 4 0.5 > tcp-tcp-one.tr
perl Perltcp.pl outtcp.tr 5 0.5 > tcp-tcp-two.tr

xgraph -bg white tcp-tcp-one.tr tcp-tcp-two.tr &


sh genGraph.sh


OUTPUT




Contact:
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

NS2 Program Based on TCP

CODE

# Create a ns object
set ns [new Simulator]

$ns color 1 Blue
$ns color 2 Red

# Open the Trace files
set TraceFile [open outtcp.tr w]
$ns trace-all $TraceFile

# Open the NAM trace file
set NamFile [open outtcp.nam w]
$ns namtrace-all $NamFile

# Create six 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]

$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.25Mb 100ms DropTail # bottleneck link
$ns duplex-link $n3 $n4 2Mb 10ms DropTail
$ns duplex-link $n3 $n5 2Mb 10ms DropTail

$ns queue-limit $n2 $n3 20

$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down

#TCP N0 and N4
set tcp1 [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp1

set sink1 [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink1

$ns connect $tcp1 $sink1

$tcp1 set fid_ 1
$tcp1 set window_ 8000
$tcp1 set packetSize_ 600

#TCP N0 and N4
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP

#TCP N1 and N5
set tcp2 [new Agent/TCP/Newreno]
$ns attach-agent $n1 $tcp2

set sink2 [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink2

$ns connect $tcp2 $sink2

$tcp2 set fid_ 2
$tcp2 set window_ 8000
$tcp2 set packetSize_ 600

#FTP TCP N1 and N5
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set type_ FTP

$ns at 0.1 "$ftp1 start"
$ns at 10.0 "$ftp2 start"
$ns at 50.0 "$ftp1 stop"
$ns at 45.0 "$ftp2 stop"

proc finish {} {
        global ns TraceFile NamFile
        $ns flush-trace
        close $TraceFile
        close $NamFile
        exec nam outtcp.nam &
        exit 0
}
$ns at 60.0 "finish"
$ns run

OUTPUT


Contact:
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

NS2 Program Based on UDP

CODE:

# Create a ns object
set ns [new Simulator]

$ns color 1 Blue
$ns color 2 Red

# Open the Trace files
set TraceFile [open outudp.tr w]
$ns trace-all $TraceFile

# Open the NAM trace file
set NamFile [open outudp.nam w]
$ns namtrace-all $NamFile

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.25Mb 100ms DropTail # bottleneck link
$ns duplex-link $n3 $n4 2Mb 10ms DropTail
$ns duplex-link $n3 $n5 2Mb 10ms DropTail

$ns queue-limit $n2 $n3 20

$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down

#TCP N0 and N4
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink

$ns connect $tcp $sink
$tcp set fid_ 1

#FTP TCP N0 and N4
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

#UDP N1 and N5
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2

#CBR N1 and N5
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 500
$cbr set interval_ 0.005

$ns duplex-link-op $n2 $n3 queuePos 0.5

$ns at 0.1 "$ftp start"
$ns at 10.0 "$cbr start"
$ns at 40.0 "$cbr stop"
$ns at 50.0 "$ftp stop"

proc finish {} {
        global ns TraceFile NamFile
        $ns flush-trace
        close $TraceFile
        close $NamFile
        exec nam outudp.nam &
        exit 0
}

$ns at 60.0 "finish"
$ns run

OUTPUT



Contact:
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

NS2 Program for Traffic Flow on Nodes


We are going to create some nodes and then enable the flow of traffic on those nodes. There are two ways to flow the traffic i.e. through TCP and UDP. We will use both here.

CODE:
#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
$ns color 3 Black

#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 NAM trace file
        close $nf
        #Execute NAM on the trace file
        exec nam out.nam &
        exit 0
}

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$n3 shape "square"
$n3 color "black"

$n0 shape "square"
$n0 color "blue"

#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-op $n0 $n2 color "blue"
$ns duplex-link-op $n0 $n2 label "cs-study"

$ns duplex-link-op $n1 $n2 color "green"
$ns duplex-link-op $n1 $n2 label "easylearning"

#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10

#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5


#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 3
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP


#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2

#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false


#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"

#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"

#Run the simulation
$ns run

OUTPUT


Contact:
Mobile: +91-7276355704
WhatsApp: +91-7276355704

Email: roshanphelonde@rediffmail.com
Share:

NS2 Program for Dynamic Nodes Generation and Traffic Flow

CODE:

#Create a simulator object
set ns [new Simulator]

#Tell the simulator to use dynamic routing
$ns rtproto DV

#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 seven nodes
for {set i 0} {$i < 7} {incr i} {
        set n($i) [$ns node]
}


#Create links between the nodes
for {set i 0} {$i < 7} {incr i} {
        $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}

#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $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.005
$cbr0 attach-agent $udp0


#Create a Null agent (a traffic sink) and attach it to node n(3)
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0

#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0

#Schedule events for the CBR agent and the network dynamics
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$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

OUTPUT:






Contact:
Mobile: +91-7276355704
WhatsApp: +91-7276355704

Email: roshanphelonde@rediffmail.com
Share:

C Program for Implementation of Stack Using Array

CODE:
#include "stdio.h"
#include "conio.h"

#define size 5

int stack[size] = {0};

int top = -1;

void push(int value)
{
 if(top < size)
 {
  top++;
  stack[top] = value;
 }
 else
 {
  printf("Stack overflow \n");
 }
}

void pop()
{
 if(top >= 0)
 {
  printf("The popped element is:\t %d \n", stack[top]);
  stack[top] = 0;
  top--;
 }

 else
 {
  printf("Stack underflow \n");
 }


}
void display()
{

                 int i;

  for(i=size - 1;i>=0;i--)
  {
   printf(" %d \n",stack[i]);
  }
}

void tos()
 {
  printf("The value at top is %d", stack[top]);
 }

int main()
{

 int i,j, ch=0 ,value = 0;

 while(1)
 {
  printf("\n Please Enter the choice. \n");
  printf("1 for push\n 2 for pop \n 3 for display\n 4 for top \t  \n 5 for exit");
  scanf("%d",&ch);
 
  switch(ch)
  {
   case 1:
    printf("\n Please Enter the number to be inserted: \t");
    scanf("%d", &value);
    push(value);
    break;
   
   case 2:
   
    pop();
   
    break;
 
   case 3:
   
    display();
   
    break;
   
 
   case 4:
    tos();
   
    break;
   
   case 5:
   
    exit(0);
   
 
 
  }
 }
 

}


Contact Us:
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Program To Implementation Sorted Linked List

PROGRAM

include "stdio.h"
include "stdlib.h"
include "conio.h"

void del(int data);
void insert(int value);
void display();

struct node
{
    int data;
    struct node *link;
};

struct node *top=NULL,*temp, *temp1, *temp2, *temp3;

int main()
{
    int choice,data;

 
    while(1) //infinite loop is used to insert/delete infinite number of elements in linked list
    {
     
        printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
        printf("\nEnter ur choice:");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
         
         
            printf("Enter a new element :");
            scanf("%d",&data);
            insert(data);
            break;
         
        case 2:
     
        printf("Enter the value to be deleted from sorted linked list :");
            scanf("%d",&data);
         
            del(data);
            break;
         
        case 3:
            display();
            break;
        case 4:
            exit(0);
        }
     
    } 
getch();
return 0;
}

void insert(int data)
{

 temp=(struct node *)malloc(sizeof(struct node));
 temp->data=data;
 
  if(top == NULL)
  {
 
            temp->link=NULL;
            top=temp;
         
  }
  else            // top not null
  {
   temp1 = top ;
   while(temp1 != NULL)
   {
    if(temp1->data >= data)   // list element is smaller ...
   
    {
     if(temp1 == top)   // list element is head ...
     {
        temp->link = temp1;
     
     top = temp;
     break;
   
     }
     else // list element is not head ..
     {
     
      temp->link = temp1;
      temp2->link = temp;
     break;
     }
     
    }
    else
    {
   
     if(temp1->link == NULL)
     {
     temp->link = NULL;
     temp1->link = temp;
     break;
   
     }
     else
     {
      temp2 = temp1;
      temp1 = temp1->link;
     }
   
    }
 
   }
 
  }
          // creating a space for the new element.
                 
}

void del(int data)
{
     struct node *temp,*var;
     temp=top;
   int i=0;
     while(temp!=NULL)
     {
          if(temp->data == data)
          {      i = 1;   // Flag ..
                if(temp==top)
                {
                     top=temp->link;
                     free(temp);
                   break;
                }
                else
                {
                     var->link=temp->link;
                     free(temp);
                     break;
                 
                }
          }
          else
          {
               var=temp;
               temp=temp->link;
          }
     }
     if(i == 1)
     {
  printf("data deleted from list is %d",data);
 
     }
     else
     {
      printf("\n The required data, %d is not found in the list. go look somewhere else",data);

     }
}


void display()
{
         temp=top;
            if(temp==NULL)
            {
                printf("\nStack is empty\n");
            }
         
            while(temp!=NULL)
            {
                printf(" %d ->",temp->data);
                temp=temp->link;
            }
             

}


Contact:
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

NS2 Program for To Study Routing in Wireless Network

CODE:

#===================================
#     Simulation parameters setup
#===================================
set val(chan)   Channel/WirelessChannel    ;# channel type
set val(prop)   Propagation/TwoRayGround   ;# radio-propagation model
set val(netif)  Phy/WirelessPhy            ;# network interface type
set val(mac)    Mac/802_11                 ;# MAC type
set val(ifq)    Queue/DropTail/PriQueue    ;# interface queue type
set val(ll)     LL                         ;# link layer type
set val(ant)    Antenna/OmniAntenna        ;# antenna model
set val(ifqlen) 50                         ;# max packet in ifq
set val(nn)     6                          ;# number of mobilenodes
set val(rp)     AODV                       ;# routing protocol
set val(x)      908                      ;# X dimension of topography
set val(y)      739                      ;# Y dimension of topography
set val(stop)   10.0                         ;# time of simulation end

#===================================
#        Initialization       
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object
set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

#Open the NS trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile

#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
#     Mobile node parameter setup
#===================================
$ns node-config -adhocRouting  $val(rp)
                -llType        $val(ll)
                -macType       $val(mac)
                -ifqType       $val(ifq)
                -ifqLen        $val(ifqlen)
                -antType       $val(ant)
                -propType      $val(prop)
                -phyType       $val(netif)
                -channel       $chan
                -topoInstance  $topo
                -agentTrace    ON
                -routerTrace   ON
                -macTrace      ON
                -movementTrace ON

#===================================
#        Nodes Definition       
#===================================
#Create 6 nodes
set n0 [$ns node]
$n0 set X_ 338
$n0 set Y_ 476
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 582
$n1 set Y_ 475
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 347
$n2 set Y_ 147
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 588
$n3 set Y_ 149
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 172
$n4 set Y_ 333
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 773
$n5 set Y_ 314
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20

#===================================
#        Generate movement         
#===================================
$ns at 3 " $n1 setdest 808 639 200 "
$ns at 0.1 " $n4 setdest 468 310 200 "

#===================================
#        Agents Definition       
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n4 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500


#===================================
#        Applications Definition       
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 9.0 "$ftp0 stop"


#===================================
#        Termination       
#===================================
#Define a 'finish' procedure
proc finish {} {
    global ns tracefile namfile
    $ns flush-trace
    close $tracefile
    close $namfile
    exec nam out.nam &
    exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
    $ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

OUTPUT


For more details Contact Us

Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

Total Pageviews

CONTACT US

Prof. Roshan P. Helonde
Mobile: +917276355704
WhatsApp: +917276355704
Email: roshanphelonde@rediffmail.com

Enter Project Title

Popular Projects

All Archive

Contact Form

Name

Email *

Message *