Ruby TCPSocket - 无法建立连接,因为目标计算机主动拒绝它

时间:2014-05-27 06:31:44

标签: ruby sockets tcpserver tcpsocket

无法将TCPSocket连接到TCPServer

客户端

require 'socket'
require 'io/console'

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

服务器

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
}

错误:

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

当连接到IIS是listeninig的端口80时,TCPSocket工作正常,所以我认为TCPServer示例有问题。

关闭防火墙没有用。此外 - 当我停止IIS并将TCPServer设置为端口80时,错误是相同的。

0 个答案:

没有答案