【发布时间】: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