[Shootout-list] Tcl: tcp-echo
Randy Melton
rmelton@atmel.com
Wed, 16 Mar 2005 15:27:56 -0500
--------------Boundary-00=_KUOGTZCPQ9ZWBVE123ER
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
I posted a similar one yesterday that was held up in moderation.
While I like yours a little better, My prior attempt clocks in slightly faster on my system.
Maybe the fake fork vs the pipe?
or setting the io to buffered and setting buffer size?
Randy Melton
--------------Boundary-00=_KUOGTZCPQ9ZWBVE123ER
Content-Type: text/plain;
charset="iso-8859-1";
name="tcp-echo.tcl"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="tcp-echo.tcl"
#!/usr/bin/tclsh
set M 6400 ;# multiplier
set port 11000
set host 127.0.0.1
set REPLY_SIZE 64
set reply [string repeat "*" $REPLY_SIZE]
set REQUEST_SIZE 64
set request [string repeat "*" $REQUEST_SIZE]
proc setupServer {channel address port} {
fconfigure $channel -buffersize $::REQUEST_SIZE -encoding binary
while {[read $channel $::REQUEST_SIZE] != {}} {
puts -nonewline $channel $::reply ;# flush $channel
}
exit
}
proc client {n} {
set bytes 0
set replies 0
while {[catch {set channel [socket $::host $::port]}]} {}
fconfigure $channel -buffersize $::REQUEST_SIZE -encoding binary
while {$n} {
puts -nonewline $channel $::request;
set reply [read $channel $::REPLY_SIZE]
incr bytes [string length $reply]
incr replies
incr n -1
}
puts stdout "replies: $replies bytes: $bytes"
exit
}
## Fake a fork by execing ourselves
if {[llength $argv] == 0} { # no args, so assume I am a server
socket -server setupServer $port
set forever 1
vwait forever
} elseif {[llength $argv] == 1} {
exec $argv0 &
set n [expr {[lindex $argv 0] * $M}]
client $n
} else {puts stdout "Usage: $argv0 N - where N is number of itterations"}
## Local Variables:
## mode: tcl
## tcl-indent-level: 3
## indent-tabs-mode: nil
## End:
--------------Boundary-00=_KUOGTZCPQ9ZWBVE123ER--