From: Brian Candler Date: 2003-05-07T22:37:30+09:00 Subject: Re: disable buffering on sockets On Wed, May 07, 2003 at 09:57:45PM +0900, daniel wrote: > > Probably gets is waiting for a linefeed to return the data. > > I know, but when I'm using read instead of gets, read returns > all the strings when I close the socket because buffering is enabled. > > I want to disable buffering so that ``readå¹¹ retrieved > immediately the data. Ah. I think you'll find it's not buffering, but it's blocking. That is, if you call the Unix read() function on a socket, and pass in a memory buffer of (say) 4096 bytes, it will block until either 4096 bytes have been read, or until the socket has been closed. The simplest approach is to keep calling read(1) and build the results into a string. If you don't want your read(1) to block when there is no data available, you can use 'select' beforehand. This is effectively what 'gets' does in io.c (in 1.6.8 anyway): it gets individual characters using getc(f) until it gets a newline. Perhaps someone else can say if there's a way to query a socket to ask how many bytes are waiting to be read? I can't think off-hand of one. Incidentally, the way DRb handles this is to send a 4-byte length value, followed by that number of bytes. That is, you do read(4) to get the length N, then read(N) to get the actual object. Hence there is no need to read individual bytes. Regards, Brian.