title: Ruby on Rails TCPSocket freezes in Client Application while loop using gets
body: <p>I am following the tutorial: <a href="http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm" rel="nofollow">http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm</a> and using it to set up the "Simple Client".</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>    require 'socket'      # Sockets are in standard library
  
      hostname = 'localhost'
      port = 2000
  
      s = TCPSocket.open(host, port)
  
      while line = s.gets   # Read lines from the socket
        puts line.chop      # And print with platform line terminator
      end
      s.close               # Close the socket when done
  </code></pre>
  
  <p>When I execute a single s.gets statement, I get the first response line from the server. When I do s.gets again, I get the second response line, etc. However, the moment I get to the end of the response and do s.gets then the program goes into a frozen state. (This is if I test the above program from the Rails Console). The same happens when I run the code - the freeze occurs when the while statement does the final s.gets. Any ideas on how to solve this?</p>
  
  <p>For information: The server is a C# server that sends response messages using the following C# code (each line of the response uses the same code and the final line in the response is not treated any differently from the other lines):</p>
  
  <pre><code>    socket.Send(Encoding.ASCII.GetBytes(msg + "\r\n"));
  </code></pre>
  
  <p>So I am thinking the client freezes because it probably doesn't interpret the servers "end of response" properly. Any ideas?</p>