#! /usr/bin/tclsh8.6

proc accept {chan addr port} {
    fconfigure $chan -buffering none
    zlib push compress $chan
    puts $chan qwertyuiop
    chan configure $chan -flush sync
    after 5000 [list print $chan asdfghjkl]
}

proc print {chan msg} {
    puts $chan $msg
    close $chan
}

proc report {chan stream} {
    set int [expr {[clock seconds] - $::start}]
    set msg [read $chan]
    if {$msg ne ""} {
	puts -nonewline [$stream add $msg]
    }
    flush stdout

    if {[eof $chan]} {
	close $chan
	exit 0
    }
}

socket -server accept 43210

set start [clock seconds]
set chan [socket localhost 43210]
fconfigure $chan -blocking 0 -buffering none
set stream [zlib stream decompress]
fileevent $chan readable [list report $chan $stream]

vwait forever
