# This is a server that bounces all received lines of text to # all clients (except the one that sent the text). namespace eval bounce { variable main variable clients variable logfile proc start { port } { variable main set main [socket -server bounce::acpt $port] putlog "Listening on port $port" } proc acpt { sock addr port } { variable clients putlog "Accept $sock from $addr port $port" set clients($sock) 1 fconfigure $sock -buffering line fileevent $sock readable [list bounce::recv $sock] } proc recv { sock } { variable main variable clients if { [eof $sock] || [catch {gets $sock line}]} { # end of file or abnormal connection drop close $sock putlog "Closing $sock" unset clients($sock) } else { if {[string compare $line "quit"] == 0} { # prevent new connections # existing connections stay open putlog "Disallowing incoming connections by request of $sock" close $main } send $sock $line } } proc send { sock line } { variable clients foreach client [array name clients] { if { [string compare $sock $client] != 0 } { # don't send to originator # putlog "send '$line' to $client" puts $client $line } } } proc putlog { text } { puts $text return } }