Recovering from a broken TCP socket in Ruby when in gets()

前端 未结 5 782
花落未央
花落未央 2021-01-05 08:40

I\'m reading lines of input on a TCP socket, similar to this:

class Bla  
  def getcmd
    @sock.gets unless @sock.closed?
  end

  def start     
    srv =          


        
5条回答
  •  礼貌的吻别
    2021-01-05 08:49

    The IO#closed? returns true when both reader and writer are closed. In your case, the @sock.gets returns nil, and then you call the getcmd again, and this runs in a never ending loop. You can either use select, or close the socket when gets returns nil.

提交回复
热议问题