Re: Detecting lost connections in TCPServer connection
John W Higgins <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAPhAwGzqgHhs4CV5WTBNtt4RhJax64tHopdanK7tBXor+JcGaw@mail.gmail.com> |
pry [1] is your friend Add require 'pry' at the top of your code - add a binding.pry line just after your s = client.gets Fire it up - connect and disconnect a client and see what happens - see what the thread thinks about your disconnecting client and what it provides you for information. There is at least one specific error with your code and technically a better option than the loop ( while (s = client.gets) .... ) - but the best option is to work your way through it by examining what the code does. John [1] - https://github.com/pry/pry On Tue, Mar 26, 2019 at 4:39 AM Peter Hickman < [email protected]> wrote: > 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> > Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>