David Brady
7/7/2005 10:09:00 PM
Yohanes Santoso wrote:
>It is not straight forward for TCP connection. Other transport-level
>protocol may have other mechanism to reliably detect and indicate
>connection termination.
>
>
I use some undocumented side-effects to detect if the remote has closed
the connection. On a TCPSocket, I find that select will immediately
return the socket once the remote has closed, but socket#gets will
return nil. In any valid transmit condition, if select returns the
socket, then gets will return valid data.
A simple reader loop, then, might look like this:
------------------------------------------------------------
sock = TCPSocket.new(server, port)
while true
if select( [sock], nil, nil, 0 )
data = sock.gets
break unless data
puts data
end
sleep 0.1
end
puts "Remote closed connection."
sock.close
------------------------------------------------------------
I'm not a Ruby expert by any means. I'm posting this for two reasons:
1. It seems to work for me and may help the OP.
2. I'm relying on undocumented behavior, and don't know of a good way to
robustly test this. I'm curious to know if anyone is aware of problems
with my solution, and to hear any ideas on how to unit test this.
Cheers,
-dB
--
David Brady
ruby-talk@shinybit.com
I'm having a really surreal day... OR AM I?