0

Can't make TCPSocket connect to TCPServer:

Client

require 'socket'
require 'io/console'

TCPSocket.open('127.0.0.1', '2000') { |socket|
    #socket.puts('abcdefghijklmn')
    puts socket.read
}

Server

require 'socket'                # Get sockets from stdlib

server = TCPServer.open(2000)   # Socket to listen on port 2000
loop {                          # Servers run forever
    Thread.start(server.accept) do |client|
        client.puts(Time.now.ctime) # Send the time to the client
        client.puts "Closing the connection. Bye!"
        client.close                # Disconnect from the client
    end
}

Error:

send_socket.rb:4:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) (Errno::ECONNREFUSED)
        from send_socket.rb:4:in `open'
        from send_socket.rb:4:in `<main>'

When connecting to port 80 where IIS is listeninig, TCPSocket works fine, so I suppose there is something wrong with TCPServer example.

Switching the firewall off doesn't help. Moreover - when I stop IIS and set TCPServer to port 80 error is the same.

Paul
  • 25,812
  • 38
  • 124
  • 247
  • Your code is fine (i.e. works for me) - both client and server. – Amadan May 27 '14 at 06:36
  • 2
    To see if you normally can use the port, try this in two terminals: one, `nc -l 2000`; two, `echo foo | nc localhost 2000`. "foo" should appear in terminal one. If you get problems, you'll know for sure it's not Ruby. (you will need to install netcat if you don't have `nc`. – Amadan May 27 '14 at 06:41
  • Couldn't find netcat on windows machine, so checked the connection in Ubuntu - it worked fine. But the above mentioned client script returns an error: `send_socket.rb:5:in 'write': Broken pipe (Errno::EPIPE) from send_socket.rb:5:in 'puts' from send_socket.rb:5:in 'block in
    ' from send_socket.rb:4:in 'open' from send_socket.rb:4:in '
    '`
    – Paul May 27 '14 at 06:53
  • Installed NetCat for Windows. `nc -l 2000` exits with `local listen fuxored: INVAL` because the right syntax is `nc -l -p 2000`. And yes, I see 'foo' in listener's output. – Paul May 27 '14 at 07:18

0 Answers0