From: Bill McLean on
[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







From: Jesús Gabriel y Galán on
On Mon, Apr 26, 2010 at 5:11 PM, Bill McLean <bill(a)mcleansoftware.com> 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(a)jesus-laptop:~$ telnet localhost 9900
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
jesus(a)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(a)jesus-laptop:~$ telnet localhost 9900
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
jesus(a)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.

From: Matt Lawrence on
On Tue, 27 Apr 2010, Bill McLean wrote:

>
> 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.

From: Roger Pack on
> 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
--
Posted via http://www.ruby-forum.com/.