#!/bin/sh # Copyright for the Uniform Repository Service (c) 1995, 1996, 1997, # by Gerald Banon. All rights reserved. # Version 1.1 # startServer \ exec ../../../../../dpi.inpe.br/banon/1997/10.21.17.24/doc/tcl8.0/unix/tclsh "$0" ${1+"$@"} proc Echo_Server {port} { global echo if [catch {socket -server EchoAccept $port} echo(main)] { puts inUse return } } proc EchoAccept {sock addr port} { global echo puts "Accept $sock from $addr port $port" set echo(addr,$sock) [list $addr $port] fconfigure $sock -buffering line fileevent $sock readable [list Echo $sock] } proc Echo {sock} { global echo if {[eof $sock] || [catch {gets $sock line}]} { # end of file or abnormal connection drop close $sock puts "Close $echo(addr,$sock)" unset echo(addr,$sock) } else { if {[string compare $line "quit"] == 0} { # Prevent new connections. # Existing connections stay open. close $echo(main) } puts $sock $line } } console show Echo_Server 1905 vwait forever