From: Tom Gilbert Date: 2002-08-15T01:09:40+09:00 Subject: Re: Problems with Marshal * Berger, Daniel (djberge@qwest.com) wrote: > Ok, I've narrowed it down partially to a newline issue with Marshal. Here > are a sample client and server to demonstrate. Just start the server in one > terminal and the client in another. Uncomment the appropriate lines to view > the different behavior. More comments at end... > > # SAMPLE SERVER > > require 'socket' > a = ["one","two","three"] # -> This gets wonky results > #a = ["one","two","three\n"] # -> This works > #a.push("\n") # -> This causes "marshal data too short" > x = Marshal.dump(a) > > tcpServer = TCPServer.open('localhost',8877) > $tcpServer = tcpServer > > addr = tcpServer.addr > addr.shift # remove "AF_INET" > > #puts "Server is on port: " + addr.shift.to_s > > while true > Thread.start(tcpServer.accept) do |s| > puts "Accepted connection: #{s}" > r = s.gets > puts "Got #{r} from client" > s.puts(x) > v = s.gets > > a = Marshal.load(v) The problem is that you're using gets and puts, which aren't appropriate for this kind of data, e.g: irb(main):004:0> Marshal.dump(["hello", "world\n", "\n"]) "\004\006[\010\"\nhello\"\vworld\n\"\006\n" irb(main):005:0> puts Marshal.dump(["hello", "world\n", "\n"]) " hello"world " You can imagine the problem doing a gets on a socket containing that last string, you aren't getting your marshalled data back :) You need to be reading all the data the client sends you, not line-wise. Anyway, isn't there DRuby - designed specifically for this kind of task? Tom. -- .^. .-------------------------------------------------------. /V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk | /( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk | ^^-^^ `-------------------------------------------------------'