1

我想知道为什么我不能在 YAML 转储后立即加载 YAML

我尝试了以下代码,但控制台上没有“结束”打印

有人能告诉我我有什么问题吗?

感谢您

服务器端红宝石代码

require 'socket'
require 'yaml'
h = []
s = TCPServer.open('localhost',2200)
c = s.accept
loop do
    YAML.dump(h,c)
    YAML.load(c)
    puts "end"
end

客户端红宝石代码

require 'socket'
require 'yaml'
d = []
s = TCPSocket.open('localhost',2200)
loop do
    d = YAML.load(s)
    YAML.dump("client",s)
    puts "end"
end
4

1 回答 1

1

YAML事先不知道要读取多少字节,因此它会尝试尽可能多地读取并永远等待。没有end_of_recordfor TCP/IP

require 'socket'
require 'yaml'
h = []
s = TCPServer.open('localhost',2200)
c = s.accept
loop do
    s = YAML.dump(h)
    c.write([s.length].pack("I"))
    c.write(s)
    length = c.read(4).unpack("I")[0]
    p YAML.load(c.read(length))
end



require 'socket'
require 'yaml'
d = []
c = TCPSocket.open('localhost',2200)
loop do
    length = c.read(4).unpack("I")[0]
    p YAML.load(c.read(length))
    s = YAML.dump("client")
    c.write([s.length].pack("I"))
    c.write(s)
end
于 2012-07-02T14:23:54.233 回答