我怀疑这是因为浏览器期望带有标头 &c 的 HTTP 响应。奇怪的是,如果你在“确保”之前放置一秒钟的睡眠,那么每次都会发生“重置”错误。
如何解决它取决于你所追求的。如果这不是 HTTP 服务器,则不要使用浏览器对其进行测试。相反,使用 telnet 或编写一个小程序。如果它是一个 HTTP 服务器,那么看看webrick,它内置在 Ruby MRI >= 1.8 中。就是这样:
#!/usr/bin/ruby1.8
require 'webrick'
# This class handles time requests
class TimeServer < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
response.status = 200
response['Content-Type'] = 'text/plain'
response.body = Time.now.to_s
end
end
# Create the server. There are many other options, if you need them.
server = WEBrick::HTTPServer.new(:Port=>8080)
# Whenever a request comes in for the root page, use TimeServer to handle it
server.mount('/', TimeServer)
# Finally, start the server. Does not normally return.
server.start