O'Reilly logo

Practical Programming in Tcl & Tk, Third Edition by Brent B. Welch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Echo Service

Example 17-3 The echo service.
proc Echo_Server {port} {
   global echo
   set echo(main) [socket -server EchoAccept $port]
}
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
   }
}

The echo server accepts connections from clients. It reads data from ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required