From: ara.t.howard@... Date: 2006-02-02T01:31:32+09:00 Subject: Re: IO and non-strings On Thu, 2 Feb 2006, Mark Volkmann wrote: > On 2/1/06, Joel VanderWerf wrote: >> Mark Volkmann wrote: >> ... >>> require 'socket' >>> >>> # Create socket. >>> host, port = 'localhost', 1919 >>> socket = TCPSocket.new(host, port) >>> >>> # Send request message. >>> binary_string = [19, 10, 8].pack('N*') >>> socket.puts binary_string >> >> You want to avoid using gets/puts with binary strings. Those functions >> are for text, and will detect/insert line ends. In fact, 10 is the same >> as a \n character, so that's probably where it's choking. > > What do you recommend I use instead of gets and puts? > If I use write, will I need to also call flush? yes. yes. > Will I have to first send a number to the server that indicates the number > of bytes I'm going to send next so it will know how many to read? that certainly makes it easier in some cases. be careful about the length of the lenght though; for instance, if you decide to send something like 'length:data' to the client then 42:foobar # length length == 2 4242:foobar # length length == 4 might want the client to read chars, based on return value of select, into a re-sizeable buf until ':' is seen and then convert to int to do a read(n). of course, if this were ruby to ruby you would be using drb right? cheers. -a -- happiness is not something ready-made. it comes from your own actions. - h.h. the 14th dali lama