tcp客户端无法从eventmachine接收数据

时间:2011-04-29 06:54:00

标签: ruby tcp eventmachine

有代码,客户端:

require 'rubygems'
require 'benchmark'
require 'socket'

i=0
TCPSocket.open "127.0.0.1", 8080 do |s|
  s.send "#{i}th sending", 0        
   if line = s.gets
     puts line
   end
end

服务器:

require 'rubygems'
require 'benchmark'
require 'eventmachine'
class Handler  < EventMachine::Connection
  def receive_data(data)
    sleep 2 # simulate a long running request
    send_data "send_response"
    puts data
  end
end

EventMachine::run {
  EventMachine::start_server("0.0.0.0", 8080, Handler)
  puts "Listening..."
}

客户端无法打印任何内容

1 个答案:

答案 0 :(得分:2)

这是客户端中s.gets与服务器中send_data "send_response"之间的互动。

当我改变时,你的小测试对我来说很好:

send_data "send_response"

send_data "send_response\n"

s.gets正在等待来自远程客户端的换行符。没有来。