<p>I'm reading lines of input on a TCP socket, similar to this:</p>

<pre><code>class Bla  
  def getcmd
    @sock.gets unless @sock.closed?
  end

  def start     
    srv = TCPServer.new(5000)
    @sock = srv.accept
    while ! @sock.closed?
      ans = getcmd
    end
  end
end
</code></pre>

<p>If the endpoint terminates the connection while getline() is running then gets() hangs. </p>

<p>How can I work around this? Is it necessary to do non-blocking or timed I/O?</p>

If you believe the
rdoc
for ruby sockets, they don't implement
```
gets
```
. This leads me to believe gets is being provided by a higher level of abstraction (maybe the IO libraries?) and probably isn't aware of socket-specific things like 'connection closed.'
Try using
```
recvfrom
```
instead of
```
gets
```