Getting error while making TCP connection in ruby?

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

I think it should be “Port” instead of “port”. Did not try it, though…

Günter Szolderits wrote in post #1170266:

I think it should be “Port” instead of “port”. Did not try it, though…

I did. Changing ‘p’ to ‘P’ changed the client output from:

Users/7stud/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/net
/telnet.rb:350:in `initialize’: Connection >> refused -
connect(2) for “127.0.0.1” port 23 (Errno::ECONNREFUSED)

to:

Received!
Received!
Closing the connection. Bye!

From the docs:

host = Net::Telnet::new(
“Host” => “localhost”, # default: “localhost”
“Port” => 23, # default: 23
“Binmode” => false, # default: false
“Output_log” => “output_log”, # default: nil (no output)
“Dump_log” => “dump_log”, # default: nil (no output)
“Prompt” => /[$%#>] \z/n, # default: /[$%#>] \z/n
“Telnetmode” => true, # default: true
“Timeout” => 10, # default: 10
# if ignore timeout then set “Timeout” to false.
“Waittime” => 0, # default: 0
“Proxy” => proxy # default: nil
# proxy is Net::Telnet or IO object
)

Thank you so much,7stud ! it did work now! How do we usually debug this
kind
issue ? it seems really not easy to see !

Günter Szolderits wrote in post #1170266:

I think it should be “Port” instead of “port”. Did not try it, though…

Thank you so much, it works now! ,Do you have any suggestions to debug
this kind of issue ? it seems really hard to see.