【问题标题】:ruby YAML load right after YAML dump在 YAML 转储后立即加载 ruby​​ YAML
【发布时间】:2012-07-02 17:07:45
【问题描述】:

我想知道为什么我不能在 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

客户端 ruby​​ 代码

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

【问题讨论】:

    标签: ruby sockets yaml


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2017-02-22
      • 1970-01-01
      • 2015-04-14
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      相关资源
      最近更新 更多