【问题标题】:Ruby TCPServer not listening to port on Google Cloud PlatformRuby TCPServer 未侦听 Google Cloud Platform 上的端口
【发布时间】:2019-05-01 11:34:49
【问题描述】:

我的学校作业要求我们练习一些套接字编程,所以我的计划是在 Google Cloud Platform 提供的 VM 上运行我的后端,并从我的本地计算机向它发送一些消息。 但是当我运行客户端代码时,它说连接被拒绝。后来检查了云外壳上的netstat -a,它说我的程序甚至没有在听(当它在后台运行时)。

谁能帮我搞定这个工作?

My Google Cloud Platform Firewall rules here

服务器端代码:

require 'socket'

module WebsocketTest
  module_function

  def init
    @server = TCPServer.new('0.0.0.0', 8080)
    @running = false
  end

  def run
    STDERR.puts 'Server is running'
    @running = true

    while @running
      socket = @server.accept
      STDERR.puts 'Incoming Request'
      http_request = ''
      while (line = socket.gets) && (line != "\r\n")
        http_request += line
      end
      STDERR.puts http_request
      socket.close
    end
  end
end

WebsocketTest.init
WebsocketTest.run 

客户端代码:

require 'socket'

s = TCPSocket.new('External IP of the VM', 8080)
s.send('Hello World!', 0)
s.close

错误信息:

client.rb:3:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) for "xxx.xxx.xxx.xxx" port 8080 (Errno::ECONNREFUSED)

【问题讨论】:

    标签: ruby google-cloud-platform


    【解决方案1】:

    好的,我想通了,这听起来可能很愚蠢...我在 GCP 网页中使用 Active Cloud Shell 并连接到我的项目,这似乎是一个临时环境。稍后我在“VM 实例”面板中通过 SSH 连接到实例,让 OP 中的代码运行,一切正常。

    【讨论】:

      最近更新 更多