From: Joel VanderWerf Date: 2006-02-02T01:21:08+09:00 Subject: Re: IO and non-strings 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? > 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? > > -- > R. Mark Volkmann > Partner, Object Computing, Inc. > I am in the habit of using send/recv, which are unbuffered, and also sending a length field before the data. If you're interested, I've got a couple of classes (subclasses of TCPSocket and TCPServer) that encapsulate this and are pretty well tested. (Also a C version of the same thing.) -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407