buffer i/o using read(2) on BSD sockets

King Ables ables at lot.ACA.MCC.COM
Tue Mar 6 06:30:21 AEST 1990


I've been using read(2) to read data from a socket and am having
problems when the buffers get large.  I'm hoping someone has run
into this before and knows what I am doing wrong.

I want to read an arbitrarily large block of data and not have to worry
about message boundaries (hence read/write rather than send/recv).

I use write(2) to send it to the connected socket, and get back a
return code indicating all characters were "written" to the socket.

I've been using the following in my readmsg function:

int	hd;		/* host descriptor	*/
char	*buf;		/* ptr to buffer	*/
int	n;		/* size of input buffer	*/

	count = read(hd, buf, n);

hd is the socket.  buf is my data buffer.  n is max number of characters
I want to read.  count will return number of characters read.

This works fine on a socket connected between two processes on my Sun up
to a buffer size of 4096.  Anything after that doesn't seem to make it.
I get (for example) 8000 back from write(hd, buf, n) where n=8000, but
when I read(hd, buf, 8000) I get 4096 and a successive read(2) returns -1.

On sockets connected between two different machines, the successful buffer
size varies between 512 and 2048, but acts the same way.  I've fired up an
ethernet monitor, and most of the data is appearing in packets, but not all
of it even seems to get sent.  However, more is showing up in packets (getting
sent) than what winds up being read, anyway.

So I'm guessing there's a timing problem.  I tried setting SO_LINGER with
setsockopt(2) on both ends, but that didn't seem to have any effect.

I decided to try doing reads like I've seen in some source code, one character
at a time.  When I changed my code to do (approximately) this:

	while (read(hd, &c, 1) == 1) buf[count++] = c;

Then it gets all 8000 characters!

For several reasons, I'd really like to do it the first way.  Namely I
would rather not have to look for some trailing key character to know when
to stop reading (or have to specify a length at the front end).

Can anybody tell me what I'm missing about buffered reads that's causing
me grief?  Or is the answer just do single character reads?

Thanks.

King Ables                    Micro Electronics and Computer Technology Corp.
ables at mcc.com                 3500 W. Balcones Center Drive
+1 512 338 3749               Austin, TX  78759



More information about the Comp.unix.wizards mailing list