我正在尝试通过 Ruby 上的 TCP 发送和“消息”对象,而我的客户端类根本看不到任何东西。我究竟做错了什么?
我的消息类(我正在尝试发送)
class Message
attr_reader :host_type, :command, :params
attr_accessor :host_type, :command, :params
def initialize(host_type, command, params)
@host_type = host_type
@command = command
@params = params
end
end
我的“服务器”类
require 'socket'
require_relative 'message'
class TCP_connection
def start_listening
puts "listening"
socket = TCPServer.open(2000)
loop {
Thread.start(socket.accept) do |message|
puts message.command
end
}
end
def send_message
hostname = 'localhost'
port = 2000
s = TCPSocket.open(hostname, port)
message = Message.new("PARAM A", "PARAM B", "PARAM C")
s.print(message)
s.close
end
end