简体   繁体   English

Ruby TCPServer有时无法工作

[英]Ruby TCPServer fails to work sometimes

I've implemented a very simple kind of server in Ruby, using TCPServer . 我已经在Ruby中使用TCPServer实现了一种非常简单的服务器。 I have a Server class with serve method: 我有一个带有serve方法的Server类:

def serve
    # Do the actual serving in a child process
    @pid = fork do
      # Trap signal sent by #stop or by pressing ^C
      Signal.trap('INT') { exit }

      # Create a new server on port 2835 (1 ounce = 28.35 grams)
      server = TCPServer.new('localhost', 2835)
      @logger.info 'Listening on http://localhost:2835...'

      loop do
        socket = server.accept
        request_line = socket.gets

        @logger.info "* #{request_line}"

        socket.print "message"

        socket.close
      end
    end
  end

and a stop method: stop方法:

def stop
    @logger.info 'Shutting down'
    Process.kill('INT', @pid)
    Process.wait
    @pid = nil
end

I run my server from the command line, using: 我使用以下命令从命令行运行服务器:

if __FILE__ == $0
  server = Server.new
  server.logger = Logger.new(STDOUT)
  server.logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
  begin
    server.serve
    Process.wait
  rescue Interrupt
    server.stop
  end
end

The problem is that, sometimes, when I do ruby server.rb from my terminal, the server starts, but when I try to make a request on localhost:2835 , it fails. 问题是,有时,当我从终端执行ruby server.rb ,服务器会启动,但是当我尝试在localhost:2835上发出请求时,它将失败。 Only after several requests it starts serving some pages. 仅在几次请求后,它才开始提供某些页面。 In other cases, I need to stop/start the server again for it to properly serve pages. 在其他情况下,我需要再次停止/启动服务器以使其正确地提供页面。 Why is this happening? 为什么会这样呢? Am I doing something wrong? 难道我做错了什么? I find this very weird... 我觉得这很奇怪...

The same things applies to my specs: I have some specs defined, and some Capybara specs. 同样的情况适用于我的规格:我定义了一些规格,还有一些水豚规格。 Before each test I create a server and start it and after each test I stop the server. 在每次测试之前,我创建一个服务器并启动它,而在每次测试之后,我都停止服务器。 And the problem persists: tests sometimes pass, sometimes fail because the requested page could not be found. 问题仍然存在:由于找不到请求的页面,测试有时通过,有时失败。

Is there something fishy going on with my fork ing? 我的fork出事了吗?

Would appreciate any answer because I have no more place to look... 希望得到任何答案,因为我没有更多的地方可以看...

Your code is not an HTTP server. 您的代码不是HTTP服务器。 It is a TCP server that sends the string "message" over the socket after receiving a newline. 它是一个TCP服务器,在接收到换行符后通过套接字发送字符串“ message”。

The reason that your code isn't a valid HTTP server is that it doesn't conform to the HTTP protocol . 您的代码不是有效的HTTP服务器的原因是,它不符合HTTP协议 One of the many requirements of the HTTP protocol is that the server respond with a message of the form HTTP协议的许多要求之一是服务器以以下形式的消息响应

HTTP/1.1 <code> <reason>

Where <code> is a number and <reason> is a human-readable "status", like "OK" or "Server Error" or something along those lines. 其中<code>是数字,而<reason>是人类可读的“状态”,例如“ OK”或“ Server Error”或类似的内容。 The string message obviously does not conform to this requirement. 字符串message显然不符合此要求。

Here is a simple introduction to how you might build a HTTP server in ruby: https://practicingruby.com/articles/implementing-an-http-file-server 以下是有关如何在ruby中构建HTTP服务器的简单介绍: https : //practicingruby.com/articles/implementing-an-http-file-server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM