I am following the tutorial: http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm and using it to set up the "Simple Client".
The code is as follows:
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
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?
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):
socket.Send(Encoding.ASCII.GetBytes(msg + "\r\n"));
So I am thinking the client freezes because it probably doesn't interpret the servers "end of response" properly. Any ideas?