Monitoring Socket Disconnect during gets or readline

Discussion in 'Ruby' started by Jon Fi, Oct 2, 2006.

  1. Jon Fi

    Jon Fi Guest

    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.

    Code:
    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Jon Fi, Oct 2, 2006
    #1
    1. Advertising

  2. Jon Fi

    hemant Guest

    On 10/3/06, Paul Lutus <> 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).

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

    --
    There was only one Road; that it was like a great river: its springs
    were at every doorstep, and every path was its tributary.
     
    hemant, Oct 3, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Socket & Disconnect

    Guest, Jan 10, 2006, in forum: Java
    Replies:
    10
    Views:
    18,094
    Chris Uppal
    Jan 14, 2006
  2. Bryan
    Replies:
    6
    Views:
    897
    Bryan
    Dec 20, 2006
  3. Shanmugavel D
    Replies:
    1
    Views:
    467
  4. James Edward Gray II

    Socket#gets and Socket#puts

    James Edward Gray II, Nov 25, 2004, in forum: Ruby
    Replies:
    1
    Views:
    126
    James Edward Gray II
    Nov 25, 2004
  5. bob smith

    detecting Socket disconnect

    bob smith, Oct 17, 2012, in forum: Java
    Replies:
    6
    Views:
    595
    Steven Simpson
    Oct 18, 2012
Loading...

Share This Page