1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#
# Copyright (C) 2001 by USC/ISI
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation, advertising
# materials, and other materials related to such distribution and use
# acknowledge that the software was developed by the University of
# Southern California, Information Sciences Institute. The name of the
# University may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# @(#) $Header: /cvsroot/nsnam/ns-2/tcl/ex/tcpapp.tcl,v 1.1 2001/11/08 19:43:43 buchheim Exp $ (USC/ISI)
# This is a simple demonstration of how to use the TcpApp application
# to send data over a TCP connection.
set namfile out.nam
set tracefile out.tr
set ns [new Simulator]
# open trace files and enable tracing
set nf [open $namfile w]
$ns namtrace-all $nf
set f [open $tracefile w]
$ns trace-all $f
# create two nodes
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 2Mb 5ms DropTail
$ns duplex-link-op $n0 $n1 orient right
# create FullTcp agents for the nodes
# TcpApp needs a two-way implementation of TCP
set tcp0 [new Agent/TCP/FullTcp]
set tcp1 [new Agent/TCP/FullTcp]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n1 $tcp1
$ns connect $tcp0 $tcp1
$tcp1 listen
# set up TcpApp
set app0 [new Application/TcpApp $tcp0]
set app1 [new Application/TcpApp $tcp1]
$app0 connect $app1
# install a procedure to print out the received data
Application/TcpApp instproc recv {data} {
global ns
$ns trace-annotate "$self received data \"$data\""
}
proc finish {} {
global ns nf f namfile
$ns flush-trace
close $nf
close $f
puts "running nam..."
exec nam $namfile &
exit 0
}
# send a message via TcpApp
# The string will be interpreted by the receiver as Tcl code.
$ns at 0.5 "$app0 send 100 {$app1 recv {my mesage}}"
$ns at 1.0 "finish"
$ns run
|