[Scott Nichols] I've been working on a Tcl network client that I've wanted to be able to automatically reconnect to the server if the connection was lost or the server process was shut down. In my applicatio the server is sending XML events over a open socket anytime a state changes in the computer system. After much trial and error I finally got it working they I wanted and decided to share the code. The sample code below will try to reconnect to the server every 10 seconds. This sample code also show how to configure a socket for nonblocking operation and configure tdom's expat parser for a situation when the entire XML document may be truncated and arrives during the next socket read. Comments are welcome. set ::host someserver set ::port someport # Event Driven. Called everytime XML data comes in over the socket. proc SockListener {dataRead} { if { [eof $::sock] || [catch {$::parser parse [read $dataRead]}]} { puts "Socket read error, trying to reconnect in 10s" close $::sock after 10000 {EventPubConnect} } } # Connect to the XML server proc EventPubConnect { } { if { [catch { # Connect to the eventpub socket set ::sock [socket $::host $::port] # Configure the socket set fcon [fconfigure $::sock -buffering full -buffersize 64000 -blocking false] # Setup the receiving socket proc fileevent $::sock r [list SockListener $::sock] set ::parser [expat] $::parser configure -final 0 -elementstartcommand HandleStart -elementendcommand HandleEnd puts "Connected to EventPub socket $::host:$::port" } errorString] } { log "proc EventPubConnect ERROR: \"$errorString\"" after 10000 {EventPubConnect} } }