[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ns] User data transfer between applications



On Wed, 2001-11-07 at 22:25, Max wrote:

> Does somebody know about data transfer between
> applications in ns? 
> I need to couple at least two applications to transfer
> simple char data and I
> really don't know how it makes.


TcpApp is probably the easiest way to do what you want.  I've attached a
short sample program to demonstrate how it can be used from TCL.

By default, TcpApp tries to interpret the received character data as TCL
code.  You can connect it to an application to change this behavior,
however.


-- 
Tim Buchheim
#  This is a simple demonstration of how to use the TcpApp application
#  to send data over a TCP connection.


set nfn out.nam
set fn out.tr

set ns [new Simulator]


# open trace files and enable tracing
set nf [open $nfn w]
$ns namtrace-all $nf
set f [open $fn w]
$ns trace-all $f


# create two nodes
set n0 [$ns node]
set n1 [$ns node]

$ns duplex-link $n0 $n1 1.5Mb 10ms 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 app-recv {data} {
	global ns
	$ns trace-annotate "$self received data \"$data\""
}

proc finish {} {
	global ns nf f
	$ns flush-trace
	close $nf
	close $f

	puts "running nam..."
	exec nam out.nam &
	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 app-recv {my mesage}}"


$ns at 1.0 "finish"

$ns run