The thing is that I am very beginner in Ruby programming and I have some troubles with understanding Ruby's behavior.
Here is the code:
require 'socket'
server = TCPServer.new 2000
loop do
Thread.start(server.accept) do |client|
puts 'Client connected: ' + client.gets
line = "-1"
while (line != "/close ")
line = client.gets();
puts line
end
puts 'client closed'
client.close
end
end
As you see, it's a simple socket server waiting for some input information.
The problem is that when it gets "/close ", it exits the while loop, but never goes to
puts 'client closed'
and client.close
. So why is it so, or what am I doing wrong?
I suspect that if you change puts line
to puts line.inspect
you will see the problem.
Found it? gets
reads by lines, which includes the line-terminating \n
, so no line you get will ever equal "/close "
. Changing the comparison to use line.strip
will work, for example.