Re: Detecting lost connections in TCPServer connection
Marvin Gülker <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <20190326122216.7o3jetjyumogzut5@atlantis> |
Am 26. März 2019 um 11:38 Uhr +0000 schrieb Peter Hickman:
> s = client.gets
> break if s.index('quit') == 0
> [...]
> 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.
I don't think this is true. A client terminating a session means that
the client closes its side of the TCP stream. This is going to cause an
EOF on the server, and thus IO#gets should return `nil' if the
connection is closed. Thus, in your example code, the thread handling
the terminating client is going to crash with a NoMethodError exception
(`nil' does not support the #index method).
Quote from <https://ruby-doc.org/core-2.6.2/IO.html#method-i-gets>:
> Returns nil if called at end of file.
Alternatively, there's IO#readline: <https://ruby-doc.org/core-2.6.2/IO.html#method-i-readline>:
> Reads a line as with IO#gets, but raises an EOFError on end of file.
If an exception is more convenient for you than just checking for `nil'.
--
Blog: https://mg.guelker.eu
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>