2012-07-02 62 views
1

我不知道爲什麼我不能YAML負載之後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 

客戶端Ruby代碼

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

回答

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 
+0

非常感謝你現在的作品! –