From: "Jesús Gabriel y Galán" Date: 2010-04-27T00:21:07+09:00 Subject: Re: Ruby networking: Errno::ECONNREFUSED: Connection refused - connect(2) on Ubuntu 9.1 On Mon, Apr 26, 2010 at 5:11 PM, Bill McLean wrote: > 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 > 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.