Showing posts with label tcp. Show all posts
Showing posts with label tcp. Show all posts

Thursday, 12 May 2016

TCL script for TCP communication between nodes



#-------Event scheduler object creation--------#
set ns [ new Simulator
#------------ CREATING NAM OBJECTS -----------------#
     
set nf [open tcpred5.nam w]
$ns namtrace-all $nf
#Open the trace file
set nt [open tcpred5.tr w]
$ns trace-all $nt

set proto rlm
#------------COLOR DESCRIPTION---------------#
$ns color 1 dodgerblue
$ns color 2 red
$ns color 3 cyan
$ns color 4 green
$ns color 5 yellow
$ns color 6 black
$ns color 7 magenta
$ns color 8 gold
$ns color 9 red

# --------- CREATING SENDER - RECEIVER - ROUTER NODES-----------#
set C1 [$ns node]
set C2 [$ns node]
set C3 [$ns node]
set C4 [$ns node]
set R1 [$ns node]
set R2 [$ns node]
set R3 [$ns node]
set R4 [$ns node]
set ROU1 [$ns node]
set ROU2 [$ns node]
set ROU3 [$ns node]

# --------------CREATING DUPLEX LINK -----------------------#
                       
$ns duplex-link $C1 $ROU1 1Mb 10ms DropTail
$ns duplex-link $C2 $ROU1 500Kb 10ms DropTail
$ns duplex-link $C3 $ROU1 750Kb 10ms DropTail
$ns duplex-link $C4 $ROU2 1Mb 10ms DropTail
$ns duplex-link $R1 $ROU1 1Mb 10ms DropTail
$ns duplex-link $R2 $ROU1 1Mb 10ms DropTail
$ns duplex-link $R3 $ROU1 1Mb 10ms DropTail
$ns duplex-link $R4 $ROU3 1Mb 10ms DropTail
$ns duplex-link $ROU2 $ROU1 1Mb 10ms DropTail
$ns duplex-link $ROU2 $ROU3 1Mb 10ms DropTail
$ns duplex-link $ROU1 $ROU3 1Mb 10ms DropTail
#-------------QUEUE SIZE DESCRIPTION---------------#
$ns queue-limit $ROU1 $ROU2 18
$ns queue-limit $ROU1 $ROU3 18
$ns queue-limit $ROU2 $ROU1 20
$ns queue-limit $ROU3 $ROU1 20
#-----------CREATING ORIENTATION -------------------------#
           
$ns duplex-link-op $C1 $ROU1 orient down
$ns duplex-link-op $C2 $ROU1 orient down-right
$ns duplex-link-op $C3 $ROU1 orient down-left
$ns duplex-link-op $C4 $ROU2 orient up
$ns duplex-link-op $R1 $ROU1 orient up
$ns duplex-link-op $R2 $ROU1 orient up-right
$ns duplex-link-op $R3 $ROU1 orient up-left
$ns duplex-link-op $R4 $ROU3 orient down
$ns duplex-link-op $ROU1 $ROU2 orient down-right
$ns duplex-link-op $ROU3 $ROU2 orient down-right
#$ns queue-limit $ $n1 15

# --------------LABELLING -----------------------------#
$ns at 0.0 "$C1 label CL1"
$ns at 0.0 "$C2 label CL2"
$ns at 0.0 "$C3 label CL3"
$ns at 0.0 "$C4 label CL4"
$ns at 0.0 "$R1 label RC1"
$ns at 0.0 "$R2 label RC2"
$ns at 0.0 "$R3 label RC3"
$ns at 0.0 "$R4 label RC4"
$ns at 0.0 "$ROU1 label ROU1"
$ns at 0.0 "$ROU2 label ROU2"
$ns at 0.0 "$ROU3 label ROU3"
# --------------- CONFIGURING NODES -----------------#
$ROU1 shape square
$ROU2 shape square
$ROU3 shape square
# ----------------QUEUES POSITIONING AND ESTABLISHMENT -------------#
$ns duplex-link-op $ROU2 $ROU1 queuePos 0.1
#$ns duplex-link-op $ROU2 $C5 queuePos 0.1
$ns duplex-link-op $ROU3 $ROU1 queuePos 0.1

#--------SETTING IDENTIFICATION COLORS TO ROUTER-LINKS----------#
     
$ns duplex-link-op $ROU1 $ROU2 color cyan
$ns duplex-link-op $ROU1 $ROU3 color cyan
$ns duplex-link-op $ROU2 $ROU3 color cyan

# ----------------ESTABLISHING COMMUNICATION -------------#
           
#--------------TCP CONNECTION BETWEEN NODES---------------#
      #$tcp0 set fid_ 3
      #$Base1 set fid_ 3
      #$tcp0 set window_ 15
      #$ftp0 set packetSize_ 1000
      #$ftp0 set interval_ .05
     
      set tcp1 [$ns create-connection TCP $C1 TCPSink $R4 1]
      $tcp1 set class_ 1
      $tcp1 set maxcwnd_ 16
      $tcp1 set packetsize_ 4000
      $tcp1 set fid_ 1
      set ftp1 [$tcp1 attach-app FTP]
      $ftp1 set interval_ .005
      $ns at 0.2 "$ftp1 start"
      $ns at 4.0 "$ftp1 stop"
           
      set tcp2 [$ns create-connection TCP $C2 TCPSink $R3 1]
      $tcp2 set class_ 1
      $tcp2 set maxcwnd_ 16
      $tcp2 set packetsize_ 4000
      $tcp2 set fid_ 2
      set ftp2 [$tcp2 attach-app FTP]
      $ftp2 set interval_ .005
      $ns at 0.7 "$ftp2 start"
      $ns at 4.0 "$ftp2 stop"
           
      set tcp3 [$ns create-connection TCP $C3 TCPSink $R2 1]
      $tcp3 set class_ 1
      $tcp3 set maxcwnd_ 16
      $tcp3 set packetsize_ 4000
      $tcp3 set fid_ 3
      set ftp3 [$tcp3 attach-app FTP]
      $ftp3 set interval_ .005
      $ns at 1.2 "$ftp3 start"
      $ns at 4.0 "$ftp3 stop"
     
      set tcp4 [$ns create-connection TCP $C4 TCPSink $R1 1]
      $tcp4 set class_ 1
      $tcp4 set maxcwnd_ 16
      $tcp4 set packetsize_ 4000
      $tcp4 set fid_ 4
      set ftp4 [$tcp4 attach-app FTP]
      $ftp1 set interval_ .005
      $ns at 2.5 "$ftp4 start"
      $ns at 4.0 "$ftp4 stop"
     
                             
      # ---------------- FINISH PROCEDURE -------------#
                 
       proc finish {} {
               global ns nf nt nf1
               $ns flush-trace
               close $nf
               puts "running nam..."
               exec nam Tcpred5.nam &
               exit 0
            }
      #Calling finish procedure
$ns at 20.0 "finish"
$ns ru

data transfer between nodes using TCP in ns2

# Filename: test2.tcl


#-------Event scheduler object creation--------#

                 
set ns [new Simulator]



#----------creating trace objects----------------#

set nt [open test2.tr w]
$ns trace-all $nt


#----------creating nam objects----------------#

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


#----------Setting color ID----------------#


$ns color 1 darkmagenta
$ns color 2 yellow
$ns color 3 blue
$ns color 4 green
$ns color 5 black

#---------- Creating Network----------------#

set totalNodes 3

for {set i 0} {$i < $totalNodes} {incr i} {
set node_($i) [$ns node]
}

set server 0
set router 1
set client 2

#---------- Creating Duplex Link----------------#


$ns duplex-link $node_($server) $node_($router) 2Mb 50ms DropTail
$ns duplex-link $node_($router) $node_($client) 2Mb 50ms DropTail

$ns duplex-link-op $node_($server) $node_($router) orient right
$ns duplex-link-op $node_($router) $node_($client) orient right

#------------Labelling----------------#

$ns at 0.0 "$node_($server) label Server"
$ns at 0.0 "$node_($router) label Router"
$ns at 0.0 "$node_($client) label Client"

$ns at 0.0 "$node_($server) color blue"
$ns at 0.0 "$node_($client) color blue"

$node_($server) shape hexagon
$node_($client) shape hexagon


#------------Data Transfer between Nodes----------------#

# Defining a transport agent for sending
set tcp [new Agent/TCP]

# Attaching transport agent to sender node
$ns attach-agent $node_($server) $tcp

# Defining a transport agent for receiving
set sink [new Agent/TCPSink]

# Attaching transport agent to receiver node
$ns attach-agent $node_($client) $sink

#Connecting sending and receiving transport agents
$ns connect $tcp $sink

#Defining Application instance
set ftp [new Application/FTP]

# Attaching transport agent to application agent
$ftp attach-agent $tcp

# Setting flow color
$tcp set fid_ 4

# data packet generation starting time
$ns at 1.0 "$ftp start"

# data packet generation ending time
$ns at 6.0 "$ftp stop"



#---------finish procedure--------#

proc finish {} {
       global ns nf nt
       $ns flush-trace
       close $nf   
         close $nt          
       puts "running nam..."
       exec nam test2.nam &
       exit 0
}

#Calling finish procedure
$ns at 10.0 "finish"
$ns run



#--------- Execution --------#

ns test2.tcl