From: Ben Armstrong Date: 2004-11-09T05:23:34+09:00 Subject: Re: Socket problem On Sat, 06 Nov 2004 21:32:19 +0900, ts wrote: >>>>>> "B" == Ben Armstrong writes: > > B> We're having problems with the standard Socket module with Ruby. > > Try to use IO#sync= That didn't help. Besides, sync is set to true by default. However, I did pull out a packet sniffer and found out something very interesting. It appears that for each operation, the old contents of the buffer is not being overwritten! Instead, it just keeps getting appended to. Thus, when I send the first line, I am sending "test\r\n". The server receives this, and writes back "test\r\n\TEST\r\n". Since my client only does a gets on the one line expected, it displays "test\r\n". My "fixed" versions (not really fixed, but demonstrate the problem clearly) are as follows: -- client.rb -- require 'socket' s=TCPSocket.new("localhost", ARGV[0]) s.puts("test\r\n") printf("garbage: %s",s.gets) puts s.gets s.puts("Blah, blah\r\n") printf("garbage: %s",s.gets) printf("garbage: %s",s.gets) printf("garbage: %s",s.gets) puts s.gets s.close -- end client.rb -- -- server.rb -- require 'socket' gs = TCPServer.open(0) printf("server is on port %d\n", gs.addr[1]) s=gs.accept line = s.gets s.flush puts("Got line: #{line}") s.puts(line.upcase) bl = s.gets puts ("Got str2 (garbage): #{bl}") bl = s.gets puts ("Got str3 (garbage): #{bl}") bl = s.gets puts ("Got str4: #{bl}") s.puts(bl.upcase) s.close -- end server.rb -- The output on the server side is: server is on port 50507 Got line: test Got str2 (garbage): test Got str3 (garbage): TEST Got str4: Blah, blah The output on the client side is: garbage: test TEST garbage: test garbage: TEST garbage: Blah, blah BLAH, BLAH Ignoring the lines marked "garbage" we can now see the intended output: TEST BLAH, BLAH How strange, eh? OK, how do I discard the buffer between operations? Or do I need the developer of the port to help me with this? Ben -- Ben Armstrong -. Medianet Development Group, BArmstrong@dymaxion.ca `-. Dymaxion Research Limited `- Halifax, Nova Scotia, Canada