Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First i wanna talk about some basic knowledge, EOF means end of file, it's like signal will send to caller when there is no more data can be read from data source, for example, open a File and after read the entire file will receives an EOF, or just simple close the io stream.</p> <p>Then there are several differences between these 4 methods</p> <ul> <li><p><code>gets</code> reads a line from stream, in ruby it uses <code>$/</code> as the default line delimiter, but you can pass a parameter as line delimiter, because if the client and server are not the same operating system, the line delimiter maybe different, it's a <strong>block</strong> method, if never meet a line delimiter or EOF it will block, and returns nil when receives an EOF, so <strong><code>gets</code> will never meet an <code>EOFError</code></strong>.</p></li> <li><p><code>read(length)</code> reads length bytes from stream, it's a <strong>block</strong> method, if length is omitted then it will block until read EOF, if there is a length then it returns only once has read certain amount of data or meet EOF, and returns empty string when receives an EOF, so <strong><code>read</code> will never meet an <code>EOFError</code></strong>.</p></li> <li><p><code>readpartial(maxlen)</code> reads at most maxlen bytes from stream, it will read available data and return immediately, it's kind like a eager version of <code>read</code>, if the data is too large you can use <code>readpartial</code> instead of <code>read</code> to prevent from blocking, but it's still a <strong>block</strong> method, it blocks if no data available immediately, <strong><code>readpartial</code> will raises an <code>EOFError</code> if receives an EOF</strong>.</p></li> <li><p><code>read_nonblock(maxlen)</code> is kind like <code>readpartial</code>, but like the name said it's a <strong>nonblock</strong> method, even no data available it raise an <code>Errno::EAGAIN</code> immediately it means no data right now, you should care about this error, normally in <code>Errno::EAGAIN</code> rescue clause should call <code>IO.select([conn])</code> first for less unnecessary cycle, it will block until the conn becomes available to read, then <code>retry</code>, <strong><code>read_nonblock</code> will raises an <code>EOFError</code> if receives an EOF</strong>.</p></li> </ul> <p>Now let's see your example, as i see what you are doing is try to read data by "hitting the url" first, it's just a HTTP GET request, some text like "GET / HTTP/1.1\r\n", connection are keep alive in HTTP/1.1 by default, so using <code>readpartial</code> or <code>read_nonblock</code> will never receive an EOF, unless put <code>Connection: close</code> header in your request, or change your gets method as below:</p> <pre><code>buffer = "" if m = @client.gets buffer &lt;&lt; m break if m.strip == "" else break end buffer </code></pre> <p>You can't use <code>read</code> here, because you don't know the exact length of the request package, use large length or just simply omitted will cause block.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload