rendered paste bodyproc readFromChannel {chan} {
if {[eof $chan]} {
puts "$chan reached eof and is therefore closed"
close $chan
return
}
gets $chan line
puts "$chan has the line: $line"
}
proc send {chan data} {
catch {
puts $chan $data
flush $chan
}
}
proc connectToServer {host port} {
set sock [socket $host $port]
send $sock "SHOW DB"
fileevent $sock readable [list readFromChannel $sock]
}
proc connectToServerWithTimeout {host port {timeout 2000}} {
after $timeout {
global sock
if {[eof $sock]} {
# or should I use {[fconfigure $sock -peer] == ""} as the condition?
puts "$sock timeout"
close $sock
}
}
global sock
set sock [socket $host $port]
send $sock "SHOW DB"
fileevent $sock readable [list readFromChannel $sock]
}