Detecting lost connections in TCPServer connection
Peter Hickman <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CALxYQy4eyAbsMccJL=kgvJwf1-6iN-Rz8TX_NcQGUo3bwf35-Q@mail.gmail.com> |
I have some code similar to the following:
#!/usr/bin/env ruby
require 'socket'
server = TCPServer.new('localhost', 2000)
loop do
Thread.start(server.accept) do |client|
puts 'Client connects'
loop do
s = client.gets
break if s.index('quit') == 0
client.puts "Hello #{s}"
end
puts 'Client disconnects'
client.close
end
end
When this runs a new connection will spawn a new thread. If the user sends
the string starting with 'quit' then the connection will close
But if the use just terminates their session then the thread is left idle
waiting for input from a connection that no one is on the other end of.
Eventually I will end up with 100s of threads with no one to talk to
Is there a way for these threads to detect that the connection has been
lost? As opposed to someone just taking a very long time to communicate
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>