title: How to read from a TCPServer socket in ruby using read, readpartial and read_nonblock
body: <p>I have a 2 part question on reading from sockets and how is it managed on Ruby servers like Unicorn or Mongrel</p>
  
  <ol>
  <li>I've learnt that to read from a socket is different from reading a file and that there are no distinct EOF message sent and the data is an endless stream.  So how do you know when to stop reading? My TCPServer for example in this case when I hit my server by accessing <code>http://localhost:9799</code> from a browser, it hangs after there is no more data to read and it won't throw the EOFError either.</li>
  </ol>
  
  <pre>
  <code>
  require 'socket'
  
  READ_CHUNK = 1024
  socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)
  addr = Socket.pack_sockaddr_in(9799, '127.0.0.1')
  socket.bind(addr)
  socket.listen(Socket::SOMAXCONN)
  socket.setsockopt(:SOCKET, :REUSEADDR, true)
  
  puts "Server is listening on port = 9799"
  loop do 
      connection, addr_info = socket.accept
      data_buffer = ""
  
      loop do
          begin
              connection.read_nonblock(READ_CHUNK, data_buffer)
              puts "Buffer = #{data_buffer}"
          rescue Errno::EAGAIN => e
              IO.select([connection])         
              retry
          rescue EOFError
              break
          end
      end
      connection.write("HTTP/1.1 200 \r\n")
      connection.write("Content-Type: text/html\r\n")
      connection.write("Status 200 \r\n")
      connection.write("Connection: close \r\n")
      connection.write("Hello World \r\n")
      connection.close
  end
  </code>
  </pre>
  
  <p>I'd like to know whats the best practice/standard approach used by Ruby servers. I see the Unicorn uses read_nonblock from <a href="http://bogomips.org/kgio.git/tree/ext/kgio/read_write.c" rel="noreferrer">kgio</a> library and mongrel uses readpartial (I'm not sure about these but going through the code this is what I feel is the approach adopted.) Even with checks for \r\n how does the server know the input is complete.
  Could explain how this should be done (and I think <code>gets</code> is not the approach - its with <code>read</code>, <code>readpartial</code>, <code>read_nonblock</code>).</p>
  
  <p>2). I would really appreciate a few lines on how this is achieved in servers like unicorn or passenger</p>
  
  <p>Thank you. </p>