# Monitoring Socket Disconnect during gets or readline

**URL:** <https://www.ruby-forum.com/t/monitoring-socket-disconnect-during-gets-or-readline/70750>\
**Category:** Ruby\
**Created:** [October 2, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/monitoring-socket-disconnect-during-gets-or-readline/70750 "2006-10-02T16:46:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratchetcat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ratchetcat](https://www.ruby-forum.com/u/ratchetcat)\
**Post date:** [October 2, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/monitoring-socket-disconnect-during-gets-or-readline/70750/1 "2006-10-02T16:46:36Z")

</div>

Hello… I am writing a simple chat program, but i have encountered a  
problem. I ask for a client to provide a handle, but it he disconnects  
during a gets call, it does weird things.

```auto
  def get_handle(session)
    for attempt in (0..2)
      session.print "Please Login\n"

      response=session.gets.strip # The problem

      re=/(Login)\s(.+)/
      md=re.match(response)
      response=md[2]
      if(!@socketHash.has_value?(response)) # This just checks to make 
sure the handle isn't in use.
        return response
      else
        session.print "Handle alerady in use... "
      end
    end
  end
```

If the client disconnects at this point, ruby seems to be still waiting  
for a response. Also, sometimes it sends ruby’s processor usage to 100%.

Is there a better way to do this? Or is there a way to give gets a  
timeout? Thanks!  
-Jon

---

<div class="post-metadata">

**Author:** ![ratchetcat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ratchetcat](https://www.ruby-forum.com/u/ratchetcat)\
**Post date:** [October 3, 2006, 2:50am UTC](https://www.ruby-forum.com/t/monitoring-socket-disconnect-during-gets-or-readline/70750/2 "2006-10-03T02:50:25Z")

</div>

Jon Fi wrote:

> Hello… I am writing a simple chat program, but i have encountered a  
> problem. I ask for a client to provide a handle, but it he disconnects  
> during a gets call, it does weird things.

/…

> If the client disconnects at this point, ruby seems to be still waiting  
> for a response. Also, sometimes it sends ruby’s processor usage to 100%.
> 
> Is there a better way to do this? Or is there a way to give gets a  
> timeout? Thanks!

IO#gets blocks forever if the input goes away, while IO#readline throws  
an  
exception on EOF. I think your situation qualifies as an EOF. Why not  
try  
it?

---

<div class="post-metadata">

**Author:** ![ratchetcat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ratchetcat](https://www.ruby-forum.com/u/ratchetcat)\
**Post date:** [October 3, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/monitoring-socket-disconnect-during-gets-or-readline/70750/3 "2006-10-03T14:45:38Z")

</div>

On 10/3/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> IO#gets blocks forever if the input goes away, while IO#readline throws an  
> exception on EOF. I think your situation qualifies as an EOF. Why not try  
> it?

Use Eventmachine ([http://rubyforge.org/projects/eventmachine](http://rubyforge.org/projects/eventmachine)).

The thing…you need for painless network programming in ruby.
