I have posted on Stackoverflow:
http://stackoverflow.com/questions/29070979/why-i-am-getting-error-while-making-tcp-connection-in-ruby
Hello guys
I’m experimenting TCP client and TCP server in ruby,I received the
following errors :
Snailwalkers-MacBook-Pro:rubybot snailwalker$ ruby tcpclient.rb
/Users/snailwalker/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/net/telnet.rb:350:in
`initialize’: Connection refused - connect(2) (Errno::ECONNREFUSED)
But when i try to telnet directly in the terminal,it works perfectly:
Snailwalkers-MacBook-Pro:rubybot snailwalker$ telnet 127.0.0.1 1234
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
test
Received!
test111
Received!
i also found something on internet, it says something related to hosts
file. So i changed ::1 localhost into 127.0.0.1 localhost,but
errors still occur. I have been stuck here for hours , your help will be
greatly appreciated!
Hosts file:
Host Database
localhost is used to configure the loopback interface
when the system is booting. Do not change this entry.
255.255.255.255 broadcasthost
::1 localhost
Here is my code:
Tcpserver.rb:
require ‘socket’
server = TCPServer.new(1234)
while connection = server.accept
while line = connection.gets
break if line =~ /quit/
puts line
connection.puts “Received!”
end
connection.puts “Closing the connection. Bye!”
connection.close
end
Tcpclient.rb:
require ‘net/telnet’
server = Net::Telnet::new(“Host” => “127.0.0.1”,“port” =>
1234,“Telnetmode” => false)
lines_to_send = [‘Hello!’, ‘This is a test’, ‘quit’]
lines_to_send.each do |line|
server.puts(line)
server.waitfor(/./) do |data|
puts data
end
end