Ruby networking: Errno::ECONNREFUSED: Connection refused -connect(2) on Ubuntu 9.1

B

Bill McLean

[Note: parts of this message were removed to make it a legal post.]

All,



When using the Ruby networking library in two sample programs below I get
the connect refused exception. To validate the code, I am running both the
client and server code in separate terminal windows. The ip address given
is the address that has been assigned to the eth0 device.



The client fails to connect to the server. However if I change the ip
address to localhost or 127.0.0.1 then the connection is made.



It may not have anything to do with ruby, but with the configuration of my
linux machine.



Using ping I can explicitly ping the ip address without fail.



Any help getting this working would be appreciated.



Server.rb



require 'socket'



dts = TCPServer.new('localhost', 9900)



loop do

Thread.start(dts.accept) do |client|



print(client, "is accepted\n")

client.close

end

end



Client.rb



require 'socket'





targetIp = "192.168.0.3"

targetPort = 9900

begin

server = TCPSocket.open(targetIp, targetPort)

puts "Connected to Server" + targetIp + ":" + targetPort

server.close

rescue => ex

puts "Connection to Server Failed:
#{ex.class}: #{ex.message}"

end





Regards



Bill McLean
Technical Director
 
J

Jesús Gabriel y Galán

All,



When using the Ruby networking library in two sample programs below I get
the connect refused exception. =A0To validate the code, I am running both= the
client and server code in separate terminal windows. =A0The ip address gi= ven
is the address that has been assigned to the eth0 device.



The client fails to connect to the server. However if I change the ip
address to localhost or 127.0.0.1 then the connection is made.



It may not have anything to do with ruby, but with the configuration of m= y
linux machine.



Using ping I can explicitly ping the ip =A0address without fail.



Any help getting this working would be appreciated.



Server.rb



require 'socket'



dts =3D TCPServer.new('localhost', 9900)


loop do

=A0Thread.start(dts.accept) do |client|



=A0 =A0print(client, "is accepted\n")

=A0 =A0client.close

=A0end

end

If you run the server and make


$ netstat -ant | grep 9900
tcp 0 0 127.0.0.1:9900 0.0.0.0:* LISTEN

you see that it's bound to the 127.0.0.1 interface (localhost). If
your client uses the other IP, the server is not listening there, as
you have seen, it only works if you change the client to use
localhost. If you change the server to create the TCP socket in that
IP (in my example I'm using 192.168.1.35):


~$ netstat -ant | grep 9900
tcp 0 0 192.168.1.35:9900 0.0.0.0:* LISTEN
jesus@jesus-laptop:~$ telnet localhost 9900
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
jesus@jesus-laptop:~$ telnet 192.168.1.35 9900
Trying 192.168.1.35...
Connected to 192.168.1.35.

You can connect using the IP but not localhost (or 127.0.0.1). If you
want your server to bind to all network interfaces, you can use
0.0.0.0:

~$ netstat -ant | grep 9900
tcp 0 0 0.0.0.0:9900 0.0.0.0:* LISTEN
jesus@jesus-laptop:~$ telnet localhost 9900
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
jesus@jesus-laptop:~$ telnet 192.168.1.35 9900
Trying 192.168.1.35...
Connected to 192.168.1.35.
Escape character is '^]'.
Connection closed by foreign host.

And then you can connect using all IPs that the machine uses.

Jesus.
 
M

Matt Lawrence

When using the Ruby networking library in two sample programs below I get
the connect refused exception. To validate the code, I am running both the
client and server code in separate terminal windows. The ip address given
is the address that has been assigned to the eth0 device.

The client fails to connect to the server. However if I change the ip
address to localhost or 127.0.0.1 then the connection is made.

It may not have anything to do with ruby, but with the configuration of my
linux machine.

Check your firewall settings, it may be blocked by iptables.

-- Matt
It's not what I know that counts.
It's what I can remember in time to use.
 
R

Roger Pack

dts = TCPServer.new('localhost', 9900)

If you want to be able to connect to it on more than just 'localhost' /
'127.0.0.1' then bind to '0.0.0.0' (or to nil or '' -- which are aliases
for the same).
GL.
-rp
 
R

Richard Surname

Thank you sooooo much Jesús for the crystal clear explanation! You were
so on the mark and it saved me!

All the best,
Richard


Jesús Gabriel y Galán said:
You can connect using the IP but not localhost (or 127.0.0.1). If you
want your server to bind to all network interfaces, you can use
0.0.0.0:

~$ netstat -ant | grep 9900
tcp 0 0 0.0.0.0:9900 0.0.0.0:*
LISTEN
jesus@jesus-laptop:~$ telnet localhost 9900
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
jesus@jesus-laptop:~$ telnet 192.168.1.35 9900
Trying 192.168.1.35...
Connected to 192.168.1.35.
Escape character is '^]'.
Connection closed by foreign host.

And then you can connect using all IPs that the machine uses.

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top