在YAML转储后立即加载ruby YAML

时间:2012-07-02 09:33:05

标签: ruby sockets yaml

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

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

有人能告诉我我的错误吗?

感谢你

服务器端ruby代码

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

1 个答案:

答案 0 :(得分:1)

YAML事先不知道要读取多少字节,因此它会尝试尽可能多地读取并永远等待。 end_of_record没有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