All pastes #4556 Raw Edit

Anonymous

public text v1 · immutable
#4556 ·published 2005-01-22 00:52 UTC
rendered paste body
proc 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]
}