Ruby TCP聊天服务器

时间:2016-04-23 23:22:15

标签: ruby tcp

最近,我一直在尝试编写一个简单的TCP服务器,以便以后构建到聊天室。但每次我启动服务器(nalcxehwttdttfseeleedsoaxfeahl),然后我尝试使用客户端(server.rb)我得到此错误:

client.rb

我正在使用CMD来运行此操作,并且我已经尝试过短暂关闭防火墙。 这是两个程序的代码......

这是 Sam@ANDERSAMERPC C:\Users\Sam\Documents\Coding > client.rb C:/Users/Sam/Documents/Coding/client.rb:6:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) for "localhost" port 2001 (Errno::ECONNREFUSED) from C:/Users/Sam/Documents/Coding/client.rb:6:in `open' from C:/Users/Sam/Documents/Coding/client.rb:6:in `<main>'

server.rb

此处 require 'socket' server = TCPServer.open(2000) # Socket to listen on port 2000 loop { Thread.start(server.accept) do |client| client.puts(Time.now.ctime) # Send time to the client client.puts "Closing connection. Bye!" client.close end }

client.rb

(此代码来自http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm,您知道。)

1 个答案:

答案 0 :(得分:0)

我猜有两种选择:

  1. server.rb之前运行client.rb
  2. 您确定这些文件与您运行的文件完全相同。因为错误消息表明它无法连接到端口2001,而在两个文件中它们都引用2000。您是否可能正在编辑和运行不同的文件?你会感到惊讶,这在SO上有多常见:)